[R] two Y Axes (in the same scale) in ggplot2
John Kane
jrkrideau at inbox.com
Wed May 9 19:04:29 CEST 2012
I don't think it is doable in ggplot2. Hadley dislikes dual scales on a single plot which is what most people would use such a thing for and so it is basically not on his to-do list.
It is relatively easy to do in base graphics.
Here on example I put together a few years ago as an exercise -- it uses dual scales and I'm really with Hadley (plus Cleveland, Wainer, etc) on not using them.
I can see a use for different scales but only for data on the same scale. If that does not make much sense, think of a raw scale and a log scale .
Good luck
==================================================================
# A simple plot with two y-axes but jazzed up.
# Note that this is poor graphing practice.
x <- 1:10
y1 <- x^2
y2 <- 100-3*x+rnorm(10)
op <- par(mar=c(5.1,4.1,4.1,4.1))
plot(x,y1, col ="red", ylab='')
text(7.5, 17, "y1", col="red")
mtext('Red Plot',side=2,line=2.5, col="red")
par(new=TRUE)
plot(x,y2,xaxt='n',yaxt='n',xlab='',ylab='',pch=3, col="blue")
axis(4)
mtext('Blue Plot',side=4,line=2.5, col="blue")
text(2.2, 89, "y2",col="blue")
par(op)
John Kane
Kingston ON Canada
> -----Original Message-----
> From: pdxgary163 at gmail.com
> Sent: Tue, 8 May 2012 12:29:52 -0700 (PDT)
> To: r-help at r-project.org
> Subject: [R] two Y Axes (in the same scale) in ggplot2
>
> Dear R users,
>
> I'm plotting housing prices in City A over past 30 years in ggplot2. The
> Xs
> are years since 1980. I have two housing price variables: new home prices
> and old home prices, both of them measured by $/sqft. I have searched
> related threads on multiple Y axes in ggplot2 and I understand that
> multiple
> Y axes in different scales are not possible. I'm wondering if it is
> possible
> to have multiple Y axes with the same scale in ggplot2, like in my case.
> If
> still not possible, is there a easy way to do it in R's default plot
> function? Thanks.
>
> Gary
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/two-Y-Axes-in-the-same-scale-in-ggplot2-tp4618441.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.
____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
More information about the R-help
mailing list