如何在具有一个x轴和两个y轴的图中将误差线缩放到右y轴的尺寸

时间:2019-07-13 08:23:01

标签: r ggplot2

我有一个数据框df1,如下所示。

df1<-data.frame(Hour=c(0,3,6,9,12,15,18,21),
                n=c(18426,19345,20123,21450,23456,23510,21453,18456),
                mean=c(23.9234, 34.9456,28.9891,44.6452,47.4567,38.9483,34.9632,29.8765),
                ci=c(4.2345,6.3345,12.1345,17.3445,13.1545,12.1745,10.1945,28.2445))
df1$Hour<-as.factor(df1$Hour)
df1

  Hour     n    mean      ci
1    0 18426 23.9234  4.2345
2    3 19345 34.9456  6.3345
3    6 20123 28.9891 12.1345
4    9 21450 44.6452 17.3445
5   12 23456 47.4567 13.1545
6   15 23510 38.9483 12.1745
7   18 21453 34.9632 10.1945
8   21 18456 29.8765 28.2445

在Internet上进行一些搜索后,我创建了下面显示的图表,其中有一个x轴和两个y轴。左y轴显示变量n,右y轴显示变量mean。 “橙色”线表示平均值(右y轴)的置信区间(ci)。

ggplot() + 
  geom_bar(mapping = aes(x = df1$Hour, y = df1$n), stat = "identity", fill = "grey") +
  geom_line(mapping = aes(x = df1$Hour, y = df1$mean*494.2611, group=1 ), size = 1, color = "blue") + # 525.3868 result from the division of 23456/44.6452
  geom_point(mapping = aes(x = df1$Hour, y = df1$mean*494.2611 )) + 
  scale_y_continuous(name = "n", 
                     sec.axis = sec_axis(~./494.2611, name = "Mean")) + 
  theme(
    axis.title.y = element_text(color = "darkgrey"),
    axis.title.y.right = element_text(color = "blue")) + 
  labs(x = "Hour") +
  geom_errorbar(mapping= aes(x = df1$Hour, ymin = df1$mean*494.2611 - df1$ci, ymax = df1$mean*494.2611 + df1$ci),
                position = position_dodge(0.9),
                width = 0.4, colour = "orange", 
                alpha = 0.9, size = 0.5)

enter image description here

问题在于,与变量mean的比例相比,误差条显得太小,因此它们的外观都相同。我认为误差线被缩放到左侧的y轴。

有人知道错误在哪里吗?

1 个答案:

答案 0 :(得分:1)

您忘记了括号。代替

 ymin = df1$mean*494.2611 - df1$ci

使用

 ymin = (df1$mean - df1$ci) * 494.2611

(与ymax相同)

说明:ggplot中的双重缩放是一种技巧。您必须手动重新缩放数据以适合主要比例。对于误差线,您需要ymin和ymax,这些是您需要缩放的值。但是ymin=df1$mean - df1$ci,因此您需要乘以整个值,而不仅仅是平均值。