【问题标题】:Cannot alter geom_errorbar width inside multiplot无法更改多图中的 geom_errorbar 宽度
【发布时间】:2017-09-13 10:47:42
【问题描述】:

我打算将四个图表放在一个页面中。每个图都显示了单个统计量的点估计值及其置信区间。我正在努力改变每个图中 geom_errorbar 晶须的宽度。它似乎没有改变,即使我改变了 geom_errorbar() 中的宽度参数。

对我来说,分别绘制这四个统计数据很重要,因为点估计和置信区间都在每个统计数据的不同范围内定义,如下图所示。我用来绘制多个图形的 multiplot 函数在 http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ 中定义。

#creates data.frame where point estimates and confidence intervals will be 
#stored
#the numbers inputed in df are similar to the ones I get from previously 
#performed regressions
w<-c(1:4)
x<-c(0.68,0.87,2.93,4.66)
y<-c(0.47,0.57,0.97,3.38)
z<-c(0.83,1.34,4.17,7.46)
df<-data.frame(w,x,y,z)

#plot each statistic
#(each row from df is a statistic: w for index, x for point estimate,
#y for ci lower bound and z for ci upper bound)

p1 <- ggplot(df[1,], aes(x = w, y = x)) +
      geom_point(size = 4) +
      geom_errorbar(aes(ymax = y, ymin = z),width=.1) +
      labs(x="",y="")

p2 <- ggplot(df[2,], aes(x = w, y = x)) +
      geom_point(size = 4) +
      geom_errorbar(aes(ymax = y, ymin = z),width=.1) +
      labs(x="",y="")

p3 <- ggplot(df[3,], aes(x = w, y = x)) +
      geom_point(size = 4) +
      geom_errorbar(aes(ymax = y, ymin = z),width=.1) +
      labs(x="",y="")

p4 <- ggplot(df[4,], aes(x = w, y = x)) +
      geom_point(size = 4) +
      geom_errorbar(aes(ymax = y, ymin = z),width=.1) +
      labs(x="",y="")

multiplot(p1, p2, p3, p4, cols=2)

非常感谢任何帮助和建议。

谢谢, 加布里埃尔

EXAMPLE PLOT HERE. How can I change errorbar whisker width for each graph separately?

【问题讨论】:

    标签: r plot graph ggplot2


    【解决方案1】:

    宽度正在发生变化,但 x 轴正在缩放以匹配误差线的宽度。您需要手动设置 x 轴,例如,xlim

    p1,你可以试试+ xlim(0.8, 1.2)

    或者,您可以使用expand 参数到scale_x_continuous,例如scale_x_continuous(expand = c(0, 0.1))

    【讨论】:

    • 非常感谢@NickKennedy!我排除了 x 轴(图中唯一相关的组件是 y 轴),宽度确实发生了变化。
    • @GabrielJarda 不用担心。请考虑将答案标记为已接受。我也会考虑使用 faceting,因为它可能比 multiplot 更适合您的需求。请注意,您需要 y 上的刻度是免费的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2012-11-19
    相关资源
    最近更新 更多