开发者

save align.plot()

开发者 https://www.devze.com 2023-03-08 06:59 出处:网络
Have to plots: pl1 <- qplot(x=trade.date, y=price1, data=finalfutff, geom=\'line\')+scale_y_continuous(name=\'\')+scale_x_date(name=\'\', expand=c(0,0))+opts(panel.background=theme_rect(),panel.gr

Have to plots:

pl1 <- qplot(x=trade.date, y=price1, data=finalfutff, geom='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(), plot.margin = unit(c(1,0,0,0), "lines"))

and

pl2 <- qplot(x=trade.date, y=adj1, data=finalfutff, ge开发者_如何转开发om='line')+scale_y_continuous(name='')+scale_x_date(name='', expand=c(0,0))+opts(panel.background=theme_rect(),panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(), plot.margin = unit(c(1,0,0,0), "lines"))

Use align.plot (from ggExtra) to align the plots (instead of facet_grid because I like to have ticks and text on the x axis on all plots), but when I try to save the file with ggsave:

ggsave(plot=align.plot(pl1, pl2), "file.png", width=9.8, height=6.9)

i get:

Error in ggsave(plot = align.plots(pl1, pl2), filename = "file.png",  : 
  plot should be a ggplot2 plot

How to save a align.plot?


Can't you just use grid.arrange() (instead of align.plot()), and use standard png device instead of ggsave() which expect ggplot objects?

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
library(gridExtra)
png("a.png")
grid.arrange(p1, p2) # add ncol=2 to arrange as two-column
dev.off()

save align.plot()


Edited

The call to ggsave with align.plots doesn't work because align.plots doesn't return an object of class ggplot.

This means you will have to make a call to one of the plot devices, e.g. png or pdf. For example:

png("filename.png")
align.plots(pl1, pl2)
dev.off()

Refer to ?png for options to specify height, width, dpi, etc. You will probably want to specify height and width explicitly, unless you want to use the default settings of 7 inches.

For a list and links to all the plot devices supported by R (i.e. png, pdf, etc.) see ?Devices

0

精彩评论

暂无评论...
验证码 换一张
取 消