2014-09-19 65 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

相關問題