Change plot dimensions in ggplot2

2019-08-15 16:56发布

问题:

I want to create a single pdf file and put several plots in it using package ggplot2. However I need to reduce the width of one specific plot, among all. Here is the code:

invisible(pdf("foo.pdf"))
foo <- data.frame(x=rnorm(100), y=rnorm(100), class=factor(sample(2,1000,T)))
ggplot(foo, aes(x=x,y=y))+geom_point() # first plot

# For next plot I want to reduce the width
ggplot(foo, aes(x=class,y=y)) + geom_boxplot()
invisible(dev.off())

How to do it?

回答1:

You can print in a viewport smaller than the full page,

library(grid) 
grid.rect(width=unit(0.8, "npc"), gp=gpar(lty=2))
print(qplot(1,1), vp=viewport(width=unit(0.8, "npc")))


标签: r ggplot2