R plotting multiple histograms on single plot to .pdf as a part of R batch script
Posted
by
Bryce Thomas
on Stack Overflow
See other posts from Stack Overflow
or by Bryce Thomas
Published on 2012-12-18T05:01:38Z
Indexed on
2012/12/18
5:02 UTC
Read the original article
Hit count: 236
I am writing R
scripts which play just a small role in a chain of commands I am executing from a terminal. Basically, I do much of my data manipulation in a Python script and then pipe the output to my R script for plotting. So, from the terminal I execute commands which look something like $python whatever.py | R CMD BATCH do_some_plotting.R
. This workflow has been working well for me so far, though I have now reached a point where I want to overlay multiple histograms on the same plot, inspired by this answer to another user's question on Stackoverflow.
Inside my R script, my plotting code looks like this:
pdf("my_output.pdf")
plot(hist(d$original,breaks="FD",prob=TRUE), col=rgb(0,0,1,1/4),xlim=c(0,4000),main="original - This plot is in beta")
plot(hist(d$minus_thirty_minutes,breaks="FD",prob=TRUE), col=rgb(1,0,0,1/4),add=T,xlim=c(0,4000),main="minus_thirty_minutes - This plot is in beta")
Notably, I am using add=T
, which is presumably meant to specify that the second plot should be overlaid on top of the first. When my script has finished, the result I am getting is not two histograms overlaid on top of each other, but rather a 3-page PDF whose 3 individual plots contain the titles:
i) Histogram of d$original
ii) original - This plot is in beta
iii) Histogram of d$minus_thirty_minutes
So there's two points here I'm looking to clarify. Firstly, even if the plots weren't overlaid, I would expect just a 2-page PDF, not a 3-page PDF. Can someone explain why I am getting a 3-page PDF? Secondly, is there a correction I can make here somewhere to get just the two histograms plotted, and both of them on the same plot (i.e. 1-page PDF)?
The other Stackoverflow question/answer I linked to in the first paragraph did mention that alpha-blending isn't supported on all devices, and so I'm curious whether this has anything to do with it. Either way, it would be good to know if there is a R
-based solution to my problem or whether I'm going to have to pipe my data into a different language/plotting engine.
© Stack Overflow or respective owner