【问题标题】:ggplot2: unable to adjust width of error barsggplot2:无法调整误差线的宽度
【发布时间】:2017-10-22 20:23:05
【问题描述】:

我正在使用Rmisc 包中summarySE() 函数的输出在我的数据上绘制误差线。我想更改每个误差条上水平线的宽度。由于某种原因,width 无法正常工作。

我查看了解决方案,但没有一个有效:

Width of error bars in ggplot2

http://www.sthda.com/english/wiki/ggplot2-error-bars-quick-start-guide-r-software-and-data-visualization

ggplot2 position_dodge affects error bar width

没有任何效果。这是我的数据的一个片段:

df <- structure(list(yrmonth = structure(c(1456790400, 1456790400, 
                                 1456790400, 1459468800, 1459468800, 1459468800, 1462060800, 1462060800, 
                                 1462060800, 1464739200, 1464739200, 1464739200), class = c("POSIXct", 
                                                                                            "POSIXt"), tzone = "UTC"), index = structure(c(1L, 4L, 5L, 1L, 
                                                                                                                                           4L, 5L, 1L, 4L, 5L, 1L, 4L, 5L), .Label = c("N-S", "N-S", "E-W", 
                                                                                                                                                                                       "E-W", "OS"), class = "factor"), N = c(2, 1, 1, 2, 1, 1, 2, 1, 
                                                                                                                                                                                                                              1, 2, 1, 1), GDDTomatoes = c(151, 136, 61, 221.5, 211, 151, 273, 
                                                                                                                                                                                                                                                           253, 207, 376, 386, 362), sd = c(7.07106781186548, NA, NA, 3.53553390593274, 
                                                                                                                                                                                                                                                                                            NA, NA, 0, NA, NA, 5.65685424949238, NA, NA), se = c(5, NA, NA, 
                                                                                                                                                                                                                                                                                                                                                 2.5, NA, NA, 0, NA, NA, 4, NA, NA), ci = c(63.5310236808735, 
                                                                                                                                                                                                                                                                                                                                                                                            NA, NA, 31.7655118404367, NA, NA, 0, NA, NA, 50.8248189446988, 
                                                                                                                                                                                                                                                                                                                                                                                            NA, NA)), .Names = c("yrmonth", "index", "N", "value", 
                                                                                                                                                                                                                                                                                                                                                                                                                 "sd", "se", "ci"), row.names = c(NA, 12L), class = "data.frame")

这是我的 ggplot 尝试之一,但没有成功。当我使用width 参数时,无论我在width 中输入什么数字,水平线都会完全消失。我只是想把它们缩短一点。

ggplot(df, aes(x=yrmonth,y=value,colour=factor(index))) + 
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=0.5) +
  geom_line() 

【问题讨论】:

  • @AdamQuek 除非我不理解该答案中的某些内容,否则我无法解决我的问题。
  • geom_errorbar width 根据 x 的因子水平数调整相对宽度。在您的数据中,x 值是一个连续梯度。将其更改为一个因子,您将能够操纵geom_errorbar 的宽度。或者,使用geom_ribbon
  • 查看yrmonth 的数值,然后查看您正在尝试的width 参数的数值。您可能应该尝试类似width = 1e6 的方法,让它们以这种规模出现。
  • yrmonth 被编码为自 1970 年以来的秒数。这意味着 x 轴上两个值之间的间隔为几百万秒。如果您的错误栏只有半秒宽,它将是不可见的。

标签: r ggplot2


【解决方案1】:
ggplot(df, aes(x=as.factor(yrmonth),y=value)) + 
  geom_point() +
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.5) +
  geom_line(aes(x=as.numeric(as.factor(yrmonth)))) +
  facet_wrap(~index)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2022-11-11
    • 2018-02-05
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多