[R] Line over Boxplot

Rui Barradas ruipbarradas at sapo.pt
Thu Sep 20 18:39:23 CEST 2012


Hello,

Works with me after correcting your lines() instruction. Your code 
doesn't say what columns to use as coordinates, just where to look for them.
Also, (1) allways explicitly close the device using dev.off(). (2) The 
grid lines were over the boxes. A way to avoid this is to plot the 
boxes, then the grid, then replot the boxes with the parameter add = 
TRUE (There's a function ?grid). And (3) you don't need the call to 
par(new=TRUE), abline() doesn't restart the graphics device.
The full code follows.


url1 <- "http://r.789695.n4.nabble.com/file/n4643736/tmax.final.text"
url2 <- "http://r.789695.n4.nabble.com/file/n4643736/temp.final.text"

pdf(file="boxplot_tmax_$YYYY$MM$DD${HH}.pdf", height=10, width=12)

soton.df = read.table(url1, header=TRUE)
gfs.df = read.table(url2, header=TRUE)

boxplot (TMAX ~ HOUR, data=soton.df,
     xlab="Forecast Hour", ylab="MAX TEMP",
     main="GEFS $YYYY$MM$DD ${HH}Z FORECAST MAX TEMPS",
     whiskcol="red", col="red", outline=TRUE,
     ylim=c(0, 100), xlim=c(1, 30),
     xaxs="i", yaxs="i")

abline(h=seq(0, 100,by=5), lty=2)
abline(v=seq(1, 30, by=1), lty=2)

boxplot (TMAX ~ HOUR, data=soton.df,
     xlab="Forecast Hour", ylab="MAX TEMP",
     main="GEFS $YYYY$MM$DD ${HH}Z FORECAST MAX TEMPS",
     whiskcol="red", col="red", outline=TRUE,
     ylim=c(0, 100),xlim=c(1, 30),
     xaxs="i", yaxs="i",
     add = TRUE)
lines(TMAX ~ HOUR, data=gfs.df, type="o", col="green")

dev.off()

Hope this helps,

Rui Barradas
Em 20-09-2012 14:41, gfishel escreveu:
> Very much a rookie at R, and have only recently started using it again so
> pardon the simple question. I am trying to produce a box plot from one data
> set and then overlay a line plot from another data set. The box plot data
> set is made up of 20 sets of 30 data points, or 600 total data points. The
> line has only 30 total data points. The box plot is plotting fine, but for
> some reason, the line plot is starting at the 6th data position and running
> off the screen. I tried modifying the text file so that the data repeated it
> self 30 times to make the total number of lines in the file identical, but
> that did not help! Here are my two datasets.....
>
> temp.final.text
> <http://r.789695.n4.nabble.com/file/n4643736/temp.final.text>
> tmax.final.text
> <http://r.789695.n4.nabble.com/file/n4643736/tmax.final.text>
>
> And here is my script....
>
> R   --save --no-save --vanilla  <<  EOF
>
> pdf(file="boxplot_tmax_$YYYY$MM$DD${HH}.pdf", height=10, width=12)
>
> soton.df = read.table ("tmax.final.text", header=TRUE)
> gfs.df = read.table ("greg.txt", header=TRUE)
> boxplot (TMAX ~ HOUR, data=soton.df, xlab="Forecast Hour", ylab="MAX TEMP",
> main="GEFS $YYYY$MM$DD ${HH}Z FORECAST MAX TEMPS", whiskcol="red",
> col="red", outline=TRUE, ylim=c(0,100),xlim=c(1,30),xaxs="i",yaxs="i")
> lines (data=gfs.df, type="o", col="green")
> par(new=TRUE)
> abline(h=seq(0,100,by=5),lty=2)
> abline(v=seq(1,30,by=1),lty=2)
>
>
>
> EOF
>
> Thanks for  helping me out!
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Line-over-Boxplot-tp4643736.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list