ggplot geom_errorbar width when faceting (and scale=“free”)

后端 未结 1 1923
Happy的楠姐
Happy的楠姐 2021-02-13 15:42

I\'m trying to create a faceted plot using ggplot and geom_errorbar. However, each different facet may have have vastly different x ranges, and so the width of the error bar is

1条回答
  •  长发绾君心
    2021-02-13 16:39

    Workaround for this problem would be to add to your data frame new column wd that contains width of the errorbars for each level.

    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)
    

    Then use this new column to set width= in geom_errorbar(). It should be set inside the aes() call.

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

    enter image description here

    0 讨论(0)
提交回复
热议问题