0

I made a previous post about trying to get a graph similar to this: https://i.stack.imgur.com/neIEz.png

So far my graph looks like this: https://i.stack.imgur.com/aXKVD.jpg

Here is my code so far, I would really appreciate any feedback or ways to improve it since I am relatively new to R. One of the main problems I ran into getting all of the data (1953-2013) to be graphed onto one water year (Oct-Sept). I created a solution, though I'm certain it's not ideal.

library(ggplot2)
library(plyr)
library(reshape2)
library(scales)

## Read Data
df <- read.csv("Salt River Flow.csv")

## Convert Date column to R-recognized dates
df$Date <- as.Date(df$Date, "%m/%d/%Y")

## Finds Water Years (Oct - Sept)
df$WY <- as.POSIXlt(as.POSIXlt(df$Date)+7948800)$year+1900

## Normalizes Water Years so stats can be applied to just months and days
df$w <- ifelse(month(df$Date) %in% c(10,11,12), 1900, 1901)

## Creates New Date (dat) Column
df$dat <- as.Date(paste(df$w,month(df$Date),day(df$Date), sep = "-"))

## Creates new data frame with summarised data by MonthDay
PlotData <- ddply(df, .(dat), summarise, 
Min = min(Flow), "10%" = quantile(Flow, p = 0.05), "25%" = quantile(Flow, p = 0.25), 
Median = quantile(Flow, p = 0.50), Mean = mean(Flow), "75%" = quantile(Flow, p = 0.75), 
"90%" = quantile(Flow, p = 0.90), Max = max(Flow))

## Melts data so it can be plotted with ggplot
m <- melt(PlotData, id="dat")

## Plots
p <- ggplot(m, aes(dat, value, group = variable, colour = variable))
+ geom_line() + labs(x = "Month",y = "Flow") + 
scale_x_date(labels=date_format("%b"))

As you can see, I am pretty close to what I want, but am having a heck of a time getting the ribbons (polygons?) to work because I don't know how to make the "ymin" and "ymax" variable based on my data.

Any comments or thoughts would be greatly appreciated!!!

Thank you!

Community
  • 1
  • 1
  • 1
    Seems to me you should tend to that previous question rather than creating a new question. You can edit questions. – tim riffe Nov 01 '13 at 21:25
  • Did not realize this, thank you. Will it let the people who already responded to my question see that I have changed it? – user2943039 Nov 01 '13 at 21:33
  • people get alerts if identified with, e.g., @user2943039 , or if you leave a comment to their answers. – tim riffe Nov 01 '13 at 21:39
  • Yes.... I think you want `geom_area` rather than `geom_line` if you need that fill highlighting of the 25-50-75th percentiles – IRTFM Nov 01 '13 at 21:39
  • @DWin, from what I can see, geom_area is just a more specific case of geom_ribbon. The problem I'm having is that I don't know how to create the code to get the result I want with geom_ribbon because I don't know how to make dynamic y ranges based on my data... – user2943039 Nov 01 '13 at 21:47
  • `ggplot` generally figures out the ranges automagically. – IRTFM Nov 01 '13 at 22:14

0 Answers0