相关疑难解决方法(0)

ggplot2 + gridExtra:如何确保不同大小的geog_bar绘图grob导致完全相同的条宽

这个问题的动机是进一步探索这个问题.当每个小平面的条数存在较大差异时,接受的解决方案的问题变得更加明显.使用该解决方案查看此数据和结果图:

# create slightly contrived data to better highlight width problems
data <- data.frame(ID=factor(c(rep(1,9), rep(2,6), rep(3,6), rep(4,3), rep(5,3))),
                   TYPE=factor(rep(1:3,length(ID)/3)),
                   TIME=factor(c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,1,1,1,2,2,2,1,1,1,1,1,1)),
                   VAL=runif(27))

# implement previously suggested solution
base.width <- 0.9
data$w <- base.width
# facet two has 3 bars compared to facet one's 5 bars
data$w[data$TIME==2] <- base.width * 3/5
# facet 3 has 1 bar compared to facet one's 5 bars
data$w[data$TIME==3] <- base.width * 1/5
ggplot(data, aes(x=ID, y=VAL, fill=TYPE)) +
  facet_wrap(~TIME, ncol=1, scale="free") +
  geom_bar(position="stack", aes(width = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

10
推荐指数
2
解决办法
3646
查看次数

标签 统计

ggplot2 ×1

r ×1