调整diffplot以在y轴上显示直方图

时间:2017-12-01 15:10:43

标签: r ggplot2

我一直在意识到http://labrtorian.com/tag/difference-plot/(数据本身可能没有必要,所以我只坚持绘图线):

diffplot <- ggplot(my.data, aes(baseline, diff)) + 
  geom_point(size=2, colour = rgb(0,0,0, alpha = 0.5)) + 
  theme_bw() + 
  #when the +/- 2SD lines will fall outside the default plot limits 
  #they need to be pre-stated explicitly to make the histogram line up properly. 
  #Thanks to commenter for noticing this.
  ylim(mean(my.data$diff) - 3*sd.diff, mean(my.data$diff) + 3*sd.diff) +
  geom_hline(yintercept = 0, linetype = 3) +
  geom_hline(yintercept = mean(my.data$diff)) +
  geom_hline(yintercept = mean(my.data$diff) + 2*sd.diff, linetype = 2) +
  geom_hline(yintercept = mean(my.data$diff) - 2*sd.diff, linetype = 2) +
  ylab("Difference pre and post Storage (mg/L)") +
  xlab("Baseline Concentration (mg/L)")

#And now for the magic - we'll use 25 bins
ggMarginal(diffplot, type="histogram", bins = 25)

我遇到的问题是,当我把它绘制成我得到的时候

Warning messages:
1: Removed 11 rows containing missing values (geom_point). 
2: Removed 11 rows containing non-finite values (stat_bin). 
3: Removed 18 rows containing missing values (geom_bar). 

ylim(mean(my.data$diff) - 3*sd.diff, mean(my.data$diff) + 3*sd.diff)更改为(在我的情况下)ylim(mean(my.data$diff) - 2000*sd.diff, mean(my.data$diff) + 2000*sd.diff)时,这是可以解决的,所以显然范围起作用。 问题是现在:当我调整范围时,情节几乎没有意义,因为你几乎看不到任何东西。当我坚持使用上面的常规范围时,除了右侧y上的直方图外,它很好:

Plot

没有警告的情节:

Plot2

作者和。评论者也面临着这种情况,并描述了在这种情况下该怎么做。不幸的是我不明白:

  

当+/- 2SD线超出默认的绘图限制时   需要明确预先说明它们以使直方图正确排列。

如何明确说明导致范围问题的数据点?换句话说:这是什么意思?

1 个答案:

答案 0 :(得分:1)

不幸的是,使用ylim()参数会导致ggmargin出现问题。因此,您必须明确预先说明上限和下限。在你的情况下:删除ylim行

  ylim(mean(my.data$diff) - 3*sd.diff, mean(my.data$diff) + 3*sd.diff) +

并保留预先说明的行

 geom_hline(yintercept = mean(my.data$diff) + 3*sd.diff, linetype = 2) +
 geom_hline(yintercept = mean(my.data$diff) - 3*sd.diff, linetype = 2) +