[R] Converting plots to ggplot2

Thompson, David (MNR) David.John.Thompson at ontario.ca
Fri Jan 18 16:44:12 CET 2008


Hello Hadley,

Thank you (and Domenico) for your help. I'm almost there.
For convenience, I'll restate the question.
With the following data:
    a <- seq(0, 360, 5)*pi/180 ; a
    ac <- sin(a + (45*pi/180)) + 1 ; ac
    s <- seq(5, 45, 10)*pi/180 ; s
    asc <- lapply(s, function(x) x*cos(ac) + x*sin(ac)) ; asc

I can generate my desired plot by traditional methods with:
    plot(a, ac, type='l', xaxt = "n", xlab='azimuth', ylab='index')
    axis(1, at=seq(0,6,1), labels=round(seq(0,6,1)*180/pi),0)
    abline(v=c(45*pi/180, 225*pi/180)) ; par(new=TRUE)
    lapply(asc, function(x) {plot(a, x, type='l', xaxt = "n", 
        yaxt = "n", xlab='', ylab='', ylim=c(0, 2)) ; par(new=TRUE)})

With ggplot2 and your suggestions I can generate the same with:
    p <- qplot(a, ac, geom='line') + geom_line(aes(x=a, y=asc[[1]])) + 
    geom_line(aes(x=a, y=asc[[2]])) + geom_line(aes(x=a, y=asc[[3]])) + 
    geom_line(aes(x=a, y=asc[[4]])) + geom_line(aes(x=a, y=asc[[5]])) + 
    scale_x_continuous(name="azimuth") +
scale_y_continuous(name="index"))

But these attempts to recreate the x-axis labels both generate the
following error:
    p + scale_x_continuous(labels=round(seq(0,6,1)*180/pi,0))
    p + scale_x_continuous(breaks=seq(0,6,1),
labels=round(seq(0,6,1)*180/pi,0))
    Error in get("new", env = ScaleContinuous, inherits =
TRUE)(ScaleContinuous,  : 
      unused argument(s) (labels = c(0, 57, 115, 172, 229, 286, 344))

And these attempts to draw reference lines both generate the following
error:
    p + geom_vline(45*pi/180) + geom_vline(225*pi/180)
    p + geom_vline(0.79) + geom_vline(3.9)
    Error in data.frame(..., check.names = FALSE) : 
      arguments imply differing number of rows: 73, 1

What am I missing?
Thanx, DaveT.
*************************************
Silviculture Data Analyst
Ontario Forest Research Institute
Ontario Ministry of Natural Resources
david.john.thompson at ontario.ca
http://ofri.mnr.gov.on.ca



More information about the R-help mailing list