如何使用 `ggplot2` 显示绘图边缘内的误差线部分?

How to show the part of the errorbar lines which are within the plot margins using `ggplot2`?

我有一个绘图网格,所有绘图都具有相同的 y 轴和 x 轴刻度。这些图在 x 轴上表示时间,在 y 轴上表示平均值及其标准误差。我的问题是一些误差线并不完全在绘图边距内,我想知道是否有某种方法可以表示绘图边距内的误差线部分。下面我给出了一个假的例子和代码来玩:

df <- data.frame(time=seq(-15,15,1),
                 mean=c(0.49,0.5,0.53,0.55,0.57,0.59,0.61,0.63,0.65,0.67,0.69,0.71,0.73,0.75,0.77,0.79,0.77,0.75,0.73,0.71,0.69,0.67,0.65,0.63,0.61,0.59,0.57,0.55,0.53,0.51,0.49),
                 sd=c(0.09,0.087,0.082,0.08,0.023,0.011,0.010,0.009,0.008,0.007,0.006,0.005,0.004,0.003,0.002,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.010,0.011,0.023,0.08,0.084,0.087,0.09))

Plot <-  ggplot(df, aes(x=time, y=mean)) + 
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) + 
  geom_point(size=1) + 
  geom_line () +
  theme_bw() + 
  scale_y_continuous(limits = c(0.49, 0.85), breaks = c(0.5, 0.65,0.8))

Plot  

您需要设置 coord_cartesian 个限制而不是 scale_y_continuous 个限制:

ggplot(df, aes(x=time, y=mean)) + 
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) + 
  geom_point(size=1) + 
  geom_line () +
  theme_bw() + 
  scale_y_continuous(breaks = c(0.5, 0.65,0.8)) +
  coord_cartesian(ylim = c(0.49, 0.85))