[R] Converting plots to ggplot2

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Jan 18 17:05:03 CET 2008


Dear David,

Try this solution. The data are stored is a dataframe instead of a list
but they should be the same. Once you have this dataframe, thinks get
rather easy. 

    library(ggplot2)
    a <- seq(0, 360, 5)*pi/180
    s <- seq(5, 45, 10)*pi/180
    dataset <- expand.grid(a = a, s = s)
    dataset$ac <- sin(dataset$a + (45*pi/180)) + 1
    dataset$asc <- dataset$s * (cos(dataset$ac) + sin(dataset$ac))

    ggplot(data = dataset, aes(x = a, y = ac)) + geom_line() +
geom_line(aes(y = asc, group = s)) + geom_vline(intercept = c(45,
225)*pi/180) + scale_x_continuous("azimuth", breaks = seq(0,6,1),
labels=round(seq(0,6,1)*180/pi)) + scale_y_continuous("index")

when you change # group = s # into # colour = factor(s) # you can
distict the different levels of s in the plot.

    ggplot(data = dataset, aes(x = a, y = ac)) + geom_line() +
geom_line(aes(y = asc, colour = factor(s))) + geom_vline(intercept =
c(45, 225)*pi/180) + scale_x_continuous("azimuth", breaks = seq(0,6,1),
labels=round(seq(0,6,1)*180/pi)) + scale_y_continuous("index")

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be 
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Thompson, David (MNR)
Verzonden: vrijdag 18 januari 2008 16:44
Aan: hadley wickham
CC: r-help op r-project.org
Onderwerp: Re: [R] Converting plots to ggplot2

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 op ontario.ca
http://ofri.mnr.gov.on.ca

______________________________________________
R-help op 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