[R] ggplot2 different Y axis scales

David Doyle kydaviddoyle at gmail.com
Mon Nov 9 02:57:20 CET 2015


Thank you to Dennis and Jeff,

The scales = "free_y" did exactly what I needed.  Just in case some one
else has the same problem, the code is below.

Take Care
David

p <- ggplot(data = SS, aes(x=Year, y=Sulfate, col=Detections)) +
  geom_point(aes(shape=Detections))  +

  ##sets the colors
  scale_colour_manual(values=c("black","red")) +

  #location of the legend
  theme(legend.position=c("none")) +

  #sets the line color, type and size
  geom_line(colour="black", linetype="dotted", size=0.5) +
  ylab("Sulfate (mg/L)") +
  ##Graph title
  ggtitle("Figure 6-30
          Sandstone Sulfate Time Series")

## does the graph using the Well IDs as the different wells.
p + facet_grid(scales = "free_y",Well ~ .)


On Sat, Nov 7, 2015 at 7:48 AM, Dennis Murphy <djmuser at gmail.com> wrote:

> As Jeff mentioned, you can use scales = "free_y" to allow different
> y-scales for the response in each facet, but you do not have the
> ability to control the ranges of the y-scales in each facet. That is
> controlled by the training process for scales in ggplot2. Generally
> speaking, it should be pretty close to what you want, but may not be
> ideal.
>
> Dennis
>
> On Fri, Nov 6, 2015 at 2:04 PM, David Doyle <kydaviddoyle at gmail.com>
> wrote:
> > Hello Everyone,
> >
> > I'm using the following code to plot sulfate concentrations vs. time for
> > several groundwater wells at one time.  Normally I need the scales to all
> > be the same but in the case of sulfate I need to use a different scale
> for
> > each well.  This is because some of my wells have very high / wide ranges
> > (MW04 ranges from 4 - 3,000) where some have very small ranges (MW06
> ranges
> > from 13 - 34).
> >
> > Is there a way that I can  have qqplot2 automatically scale each well or
> a
> > way I could enter a scale range.?  For example I would like MW04 to have
> a
> > Y axis scale from 0 - 3,000 and MW06 to have a Y axis scale from 0 - 40
> >
> > I am using
> > RStudio version 0.99.484
> > R i386 3.2.2
> > ggplott2 ver 1.0.1
> > in a Windows 7 environment.
> >
> > Thank you for your time
> > David Doyle
> >
> >
> > library(ggplot2)
> > SS <-read.csv("http://doylesdartden.com/Stats/SS.csv", sep=",")
> >
> > #Sets whic are detections and nondetects
> > SS$Detections <- ifelse(SS$D_Sulfate==1, "Detected", "NonDetect")
> > png(file="Sulfate.png",width=2400,height=3000,res=300)
> > #does the plot
> > p <- ggplot(data = SS, aes(x=Year, y=Sulfate, col=Detections)) +
> >   geom_point(aes(shape=Detections))  +
> >
> >   ##sets the colors
> >   scale_colour_manual(values=c("black","red")) +
> >
> >   #location of the legend
> >   theme(legend.position=c("none")) +
> >
> >   #sets the line color, type and size
> >   geom_line(colour="black", linetype="dotted", size=0.5) +
> >   ylab("Sulfate (mg/L)") +
> >   ##Graph title
> >   ggtitle("Figure 6-30
> >           Sandstone Sulfate Time Series")
> >
> > ## does the graph using the Well IDs as the different wells.
> > p + facet_grid(Well ~ .)
> > dev.off()
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list