2017-09-02 205 views
1

我试图从R中的一篇研究文章重新创建一些图并且遇到了将对数坐标应用于y轴的问题。我试图重新创建可视化是这样的: reference plot with y log scale将对数坐标应用于y轴,用ggplot2对比例进行可视化

我现在有没有应用到y轴的对数刻度工作版本:

Proportion_Mean_Plot <- ggplot(proportions, aes(days2, 
           proportion_mean, group = observation)) + 
    geom_point(aes(shape = observation)) + 
    geom_line() + 
    scale_x_continuous(breaks = seq(0,335,20)) + 
    scale_y_continuous(breaks = seq(0,6,.5)) + 
    theme_tufte() + 
    geom_rangeframe() + 
    theme(legend.position="none") + 
    theme(axis.line.x = element_line(colour = "black", size = 0.5, linetype = 1), 
     axis.line.y = element_line(colour = "black", size = 0.5, linetype = 1)) + 
    labs(title = "Proportion of Baseline Mean", 
     subtitle = "Daily steps within each intervention phase", 
     x = "DAYS", 
     y = "PROPORTION OF BASELINE \n(MEAN)") + 
    geom_vline(xintercept = 164.5) + 
    geom_hline(yintercept = 1) + 
    annotate("text", x = c(82, 246), y = 5, 
      label = c("Intervention 1", "Intervention 2")) + 
    geom_segment(aes(x = 0, y = mean, xend = end, yend = mean), 
       data = proportion_intervention1_data) + 
    geom_segment(aes(x = start, y = mean, xend = end, yend = mean), 
       data = proportion_intervention2_data, linetype = 4) 

这产生了原始的体面表示: normally scaled y-axis plot

我想尝试应用对数标度来更贴近地匹配它。任何帮助表示赞赏。

+1

除非你添加一些样本数据可能你不会得到太多的答案。否则,你不能玩弄选项来改善你的答案 – 5th

+1

'scale_y_log10'? –

回答

3

按照理查德的建议,这里是一个简单的例子,你如何使用scale_y_log10

suppressPackageStartupMessages(library(tidyverse)) 
set.seed(123) 
# generate some data 
proportions <- tibble(interv_1 = pmax(0.4, rnorm(160, mean = 1.3, sd = 0.2)), 
         interv_2 = pmax(0.01, rnorm(160, mean = 1.6, sd = 0.5))) 

proportions <- proportions %>% 
    gather(key = observation, value = proportion_mean) %>% 
    mutate(days2 = 1:320) 

# create the plot 
ggplot(proportions, aes(days2, proportion_mean, group = observation)) + 
    geom_point(aes(shape = observation)) + 
    geom_line() + 
    scale_x_continuous(breaks = seq(0,335,20), expand = c(0, 0)) + 
    scale_y_log10(breaks = c(0.1, 0.5, 1, 2, 3, 4, 5), limits = c(0.1, 5)) + 
    # theme_tufte() + 
    # geom_rangeframe() + 
    theme(legend.position="none") + 
    theme(axis.line.x = element_line(colour = "black", size = 0.5, linetype = 1), 
     axis.line.y = element_line(colour = "black", size = 0.5, linetype = 1)) + 
    labs(title = "Proportion of Baseline Mean", 
     subtitle = "Daily steps within each intervention phase", 
     x = "DAYS", 
     y = "PROPORTION OF BASELINE \n(MEAN)") + 
    geom_vline(xintercept = 164.5) + 
    geom_hline(yintercept = 1) + 
    annotate("text", x = c(82, 246), y = 5, 
      label = c("Intervention 1", "Intervention 2")) + 
    # plugged the values for the means of the two distributions 
    geom_segment(aes(x = 0, y = 1.3, xend = 164.5, yend = 1.3)) + 
    geom_segment(aes(x = 164.5, y = 1.6, xend = 320, yend = 1.6), linetype = 4) 

+0

这效果很好@ LVG77。我最终使用'scale_y_log10(breaks = c(0.1,1,10),limits = c(0.1,10))'和'annotation_logticks(sides =“l”)'来实现原始文章的外观。 另外,感谢您在'scale_x_continuous'中使用'expand'的提示。摆弄那使情节看起来好多了。 –