【问题标题】:R ggplot2 - issue with errorbar widthR ggplot2 - 错误栏宽度问题
【发布时间】:2020-08-01 20:38:33
【问题描述】:

我一直在尝试使用 ggplot2 绘制以下数据,但遇到了错误栏问题:

data1<-c(0.04, 0.5, 1.0, 1.5 ,2.0, 2.6, 3.1, 3.6,  0.9,0.56,0.4,0.33,0.27,0.2,0.15,0.1, 0.12, 0.17, 0.22 ,0.33, 0.45,  0.57, 0.74, 0.85)

sym<-as.data.frame(matrix(data = data1, ncol = 3, byrow = FALSE))

ggplot(data=sym, mapping = aes(x=sym[,1], y=sym[,2]))+
  theme(plot.background = element_rect(fill = "white"), 
        panel.background = element_rect(fill = "white", color="black",linetype = 1),
        panel.grid = element_blank(),
        plot.title = element_text(hjust = 0.5, size =30),
        axis.title = element_blank(),
        axis.ticks = element_line(color="black"),
        axis.ticks.length=unit(-0.25, "cm"),
        axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), 
        axis.text.y = element_text(size=25, margin=unit(c(0.5,0.5,0.5,0.5), "cm")))+
  ggtitle("variable, symmetric error")+
  ylim(-1.0, 1.5)+
  xlim(0.0,4.5)+
  geom_line(color="blue",size=1.2)+
  geom_point(color="blue", size=4)+
  geom_errorbar(aes(ymin = sym[,2]-sym[,3], ymax = sym[,2]+sym[,3], x= sym[,1]),
                width=0.1, color="blue", size =1.2)

结果(主要)符合预期,但我无法弄清楚为什么第一个错误栏没有响应“宽度”设置。 Plot showing the issue with the first errorbar, which is not responding to the set width parameter

我的第一个猜测是,更改边距可能会导致某种重叠,这导致 R 在第一个点上没有按宽度绘制。但是,在 x 轴上向内移动数据没有任何区别。因此,我假设我一定错过了其他东西?

非常感谢任何输入!

【问题讨论】:

  • 原因是您将x轴的下限设置为零。试试xlim(-0.1, 4.5)
  • ...如果你想增加宽度,你可以相应地调整限制
  • @stefan 或者一起删除对xlim 的调用。另外,在ggplot2 图形中不要使用sym[,1] 等,使用列名V1 等。
  • 我明白了,非常感谢您的及时回复! :)

标签: r ggplot2 graphics width errorbar


【解决方案1】:

指出@stefan 和@RuiBarradas 关于变量名称和限制的好建议,您也可以尝试scale_x_continuous()scale_y_continuous() 来获取限制,因为它在下一个代码中使用:

ggplot(data=sym, mapping = aes(x=V1, y=V2))+
  theme(plot.background = element_rect(fill = "white"), 
        panel.background = element_rect(fill = "white", color="black",linetype = 1),
        panel.grid = element_blank(),
        plot.title = element_text(hjust = 0.5, size =30),
        axis.title = element_blank(),
        axis.ticks = element_line(color="black"),
        axis.ticks.length=unit(-0.25, "cm"),
        axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), 
        axis.text.y = element_text(size=25, margin=unit(c(0.5,0.5,0.5,0.5), "cm")))+
  ggtitle("variable, symmetric error")+
  scale_y_continuous(limits = c(NA,1.5))+
  scale_x_continuous(limits = c(NA,4.5))+
  geom_line(color="blue",size=1.2)+
  geom_point(color="blue", size=4)+
  geom_errorbar(aes(ymin = V2-V3, ymax = V2+V3, x= V1),
                width=0.1, color="blue", size =1.2)

【讨论】:

    猜你喜欢
    • 2013-10-16
    • 2013-05-06
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多