Reputation: 617
I am using R, and I am sourcing another script within one master script. Within the sourced script, I have code that looks like the following:
pdf("Figs/bar_gni.pdf")
m1table$Country1 <- reorder(m1table$Country, m1table$GNIpc2005)
ggplot(m1table, aes(y=GNIpc2005, fill=Level)) +
geom_bar(aes(x=Country1), data=m1table, stat="identity") +
coord_flip() +
ggtitle("GNI Per Capita, 2005") +
xlab("Country") +
ylab("GNI per capita, Atlas method (current US$)")
dev.off()
The important part, if I'm correct, is that I'm opening a pdf graphics device, making a plot, and then closing the device.
When I run the source script itself (by opening the script), this all works just fine. However, when I source it, none of my graphs are outputted. It seems to create the files, but it just creates blank files.
Any feedback would be greatly appreciated.
Upvotes: 1
Views: 175
Reputation: 94202
ggplot graphics are only plotted when their objects are printed.
On the command line this happens when you type it in. Just as typing sqrt(2)
prints the answer because the command line automatically calls print
, doing ggplot(.)+geom_line(.)
calls print
and that makes the plot.
IN a script, the results aren't auto-printed.
So wrap all your ggplot
calls with print(ggplot(whatever))
.
This is an R FAQ. http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f
Upvotes: 3