Plotting means and raw values on a common time scale

31 views
Skip to first unread message

Sam Albers

unread,
Nov 26, 2015, 4:42:10 PM11/26/15
to ggplot2
Hello all,

I can't believe that this has never come up for me until now. I'd like
to plot some summary statistics, summarized over a time parameter. I
understand why the code below doesn't work. Month is not a POSIX
object while Date is. So I am not expecting the code to work. My issue
is that the workaround I've come up with seems clunky at best and
potentially dangerous at worst. Can any recommend a better way of
doing this?

Thanks in advance

Sam

##Code
library(lubridate)
library(plyr)
library(ggplot2)

airquality$Date<-dmy(paste(airquality$Day,airquality$Month,"2000",sep="/"))

## Base plot
p<-ggplot(airquality, aes(x=Date, y=Ozone)) +
geom_point()

## Monthly Average
airqualityMonthMean<-ddply(airquality, .(Month), summarise,
Ozone=mean(Ozone, na.rm=TRUE))

## Now add a line illustrating the monthly mean
p + geom_line(data=airqualityMonthMean, aes(x=Month, y=Ozone))

## Sort of a Work around
airqualityMonthMean$Month<-dmy(paste("1",airqualityMonthMean$Month,"2000",sep="/"))

## Re plot and it works
p + geom_line(data=airqualityMonthMean, aes(x=Month, y=Ozone))

Brian Deering

unread,
Nov 27, 2015, 1:40:25 PM11/27/15
to ggplot2
I can't figure out what problem you're trying to overcome if what you have works.

Can you be more specific?

Sam Albers

unread,
Dec 1, 2015, 6:56:40 PM12/1/15
to Brian Deering, ggplot2
Hi Brian,

Thanks for the message. Dennis Murphy provided me with exactly what I
was looking which was an improved solution to the one that I have
already come up with. I'll reproduce it below:

# uncomment if plyr is loaded
# detach(package:plyr)

library(dplyr)
library(lubridate)
library(ggplot2)

# Add POSIXct object to airquality data, save as aq
aq <- airquality %>%
mutate(Date = dmy(paste(Day, Month, "2000", sep="/")))

# Compute monthly means and set a corresponding date
# in the middle of the month
aqMonthlyMean <- aq %>%
group_by(Month) %>%
summarise(Ozone = mean(Ozone, na.rm = TRUE)) %>%
mutate(Date = dmy(paste("15", Month, "2000", sep = "/")))

# Plot
aq %>% ggplot(., aes(x = Date, y = Ozone)) +
geom_point() +
geom_point(data = aqMonthlyMean, color = "darkorange", size = 3) +
geom_path(data = aqMonthlyMean, color = "darkorange", size = 1)



Regards,

Sam

On Fri, Nov 27, 2015 at 10:40 AM, Brian Deering <pndf...@gmail.com> wrote:
> I can't figure out what problem you're trying to overcome if what you have works.
>
> Can you be more specific?
>
> --
> --
> 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.
Reply all
Reply to author
Forward
0 new messages