有没有办法在ggplot中保留箱线图的宽度?默认情况下,宽度将根据图中包含的框数而变化。
答案 0 :(得分:1)
手动设置x轴怎么样?
# Problem: different widths between these two plots
p1 <- ggplot(mtcars, aes(x=factor(cyl), y=mpg)) +
geom_boxplot()
p2 <- ggplot(mtcars[mtcars$cyl<8,], aes(x=factor(cyl), y=mpg)) +
geom_boxplot()
# Solution: fix x axis
p3 <- ggplot(mtcars[mtcars$cyl<8,], aes(x=factor(cyl), y=mpg)) +
geom_boxplot() +
scale_x_discrete(limits=c('4', '6', '8'))
library(gridExtra)
grid.arrange(p1, p2, p1, p3, ncol=2, main='Before', sub='After')