首页 > 解决方案 > ggplot2 geom_errrorbar 不显示图形中一点的完整误差条

问题描述

我正在根据此数据框使用 ggplot2 进行绘图:

      Control    Stress days  sd_control
X21 0.9702100 0.9343627  X21 0.001900535
X28 0.9666619 0.8595523  X28 0.014946893
X35 0.9165654 0.7160598  X35 0.072655343
X42 0.9208237 0.6668044  X42 0.050870831
X49 0.8766547 0.7660685  X49 0.073588197
X56 0.9599553 0.7937444  X56 0.041559836
X63 0.9736297 0.8188934  X63 0.003817743

情节的代码是:

ggplot(my_data, aes(x=days,y=Control,group=1)) +
  geom_point(shape=22,color='grey',fill='grey',size=3) + 
  geom_line(color='grey') +
  xlab('DAT') +
  ylab('RWC') + 
  scale_y_continuous(labels = percent,limits = c(0.5,1), expand = c(0,0)) +
  scale_x_discrete(expand = c(0.07, 0)) +
  ggtitle('Relative Water Content') +
  geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control))

结果图是: 在此处输入图像描述

x56 处绘图的误差条不完整。关于如何解决这种行为的任何想法?

标签: rggplot2

解决方案


问题在于您对 y 轴的限制。尝试limits修改scale_y_continuous

ggplot(my_data, aes(x=days,y=Control,group=1)) +
    geom_point(shape=22,color='grey',fill='grey',size=3) + 
    geom_line(color='grey') +
    xlab('DAT') +
    ylab('RWC') + 
    scale_y_continuous(labels = percent,limits = c(0.5,1.01), expand = c(0,0)) +
    scale_x_discrete(expand = c(0.07, 0)) +
    ggtitle('Relative Water Content') +
    geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control))

在此处输入图像描述


推荐阅读