2014-09-19 33 views

回答

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') 

enter image description here

相关问题