【问题标题】:ggplot geom_errorbar width when faceting (and scale="free")切面时的ggplot geom_errorbar宽度(和scale =“free”)
【发布时间】:2013-01-24 16:27:15
【问题描述】:

我正在尝试使用 ggplot 和 geom_errorbar 创建多面图。但是,每个不同的方面可能有很大不同的 x 范围,因此误差条的宽度看起来并不“好”。这是一个 MWE:

library(ggplot2)
test <- data.frame( group=rep(c(1,2,3),each=10), ymin=rnorm(30), ymax=rnorm(30))
test$x <- rnorm(30) * (1+(test$group==1)*20)
ggplot( test, aes(x=x, ymin=ymin, ymax=ymax) ) +
  geom_errorbar(width=5) + facet_wrap( ~ group, scale="free_x" )
ggplot( test, aes(x=x, ymin=ymin, ymax=ymax) ) +
  geom_errorbar(width=.2) + facet_wrap( ~ group, scale="free_x" )

在第一个图中,第 1 组的误差线看起来不错,但第 2 组和第 3 组的误差线太宽了。在第二个图中,误差条对于第 1 组来说太小了。有没有简单的方法来解决这个问题?我想我可能只需要使用 width=0,但我想避免这种情况。

【问题讨论】:

  • 在较新的软件包版本中是否有针对此问题的更新答案? @Didzis Elferts。

标签: r plot ggplot2


【解决方案1】:

解决此问题的方法是向您的数据框中添加新列wd,其中包含每个级别的错误栏宽度。

test <- data.frame( group=rep(c(1,2,3),each=10), ymin=rnorm(30), ymax=rnorm(30))
test$x <- rnorm(30) * (1+(test$group==1)*20)
test$wd<-rep(c(10,0.5,0.5),each=10)

然后使用这个新列在geom_errorbar() 中设置width=。它应该在aes() 调用中设置。

ggplot( test, aes(x=x, ymin=ymin, ymax=ymax) ) +
  geom_errorbar(aes(width=wd)) + facet_wrap( ~ group, scale="free_x" )

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    相关资源
    最近更新 更多