Order of data in plots

24 views
Skip to first unread message

David Doyle

unread,
Oct 27, 2014, 9:04:09 PM10/27/14
to ggp...@googlegroups.com
Hi folks,

I was wondering if there is a way to control the order my data plots in the graph.

This is a statistical review of groundwater data from wells.  It is plotting the well in the order of their IDs.  ie. MW1B then MW3B then MW4A.....

I would like to have it do MW1B then WES-14-4-93 then ........

Is there a way I can control this??  

My data / code is below.

Thank you for your time
David Doyle

library(ggplot2)

MS <- read.table("http://www.doylesdartden.com/R/MS.csv", header=TRUE, sep=",",)

#Sets whic are detections and nondetects
MS$Detections <- ifelse(MS$D_AMMONIA==1, "Detected", "NonDetect")

#does the plot
p <- ggplot(data = MS, aes(x=Year, y=AMMONIA, 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("Ammonia (mg/L)") +
  ##Graph title
  ggtitle("Lower Mudstone Ammonia Time Sereis")

## does the graph using the Well IDs as the different wells.
p + facet_grid(Well ~ .)

Brandon Hurr

unread,
Oct 27, 2014, 9:07:06 PM10/27/14
to David Doyle, ggplot2
Use factor() to set the order of your levels for Well. 

Brandon Hurr

unread,
Oct 27, 2014, 9:59:53 PM10/27/14
to David Doyle, ggplot2
library(ggplot2)


MS <- read.table("http://www.doylesdartden.com/R/MS.csv", header=TRUE, sep=",",)

#Sets which are detections and nondetects

MS$Detections <- ifelse(MS$D_AMMONIA==1, "Detected", "NonDetect")

#reorder factor levels for plotting
MS$Well <- factor(MS$Well, levels = c("MW1B","WES-14-4-93","MW3B","MW4A","MW5B","MW6B") )



#does the plot
p <- ggplot(data = MS, aes(x=Year, y=AMMONIA, 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("Ammonia (mg/L)") +
  ##Graph title
  ggtitle("Lower Mudstone Ammonia Time Sereis")

## does the graph using the Well IDs as the different wells.
p + facet_grid(Well ~ .)

Dennis Murphy

unread,
Oct 28, 2014, 1:19:20 AM10/28/14
to David Doyle, ggplot2
This is the same question you posted to R-help, and it's not the first
time. Please don't cross-post unless you have a question that is
specific to ggplot2. (The same applies to cross-posting to Stack
Overflow.) David Winsemius provided a reasonable answer to your
question by suggesting that you order the factor levels prior to
calling a plot routine, and the same advice would hold for ggplot2, as
Brandon noted.

Dennis

On Mon, Oct 27, 2014 at 6:04 PM, David Doyle <kydavi...@gmail.com> wrote:
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

David Doyle

unread,
Oct 28, 2014, 7:20:29 AM10/28/14
to Dennis Murphy, ggplot2
Hi Dennis,

I apologize.  I had posted the question regarding my time vs. concentration here because I had used ggplot2 for those graphs and the question in R-Help was regarding the box plots which did not use ggplot2.  I thought there might a different requirement for them being the one was using ggplot2.

Thank you for your time
David Doyle
Reply all
Reply to author
Forward
0 new messages