mediaczar
mediaczar

Reputation: 1980

ggplot2: plotting time series data by month & week

I'm trying to plot time series data by week and month; ideally, I think, I'd like to use boxplots to visualise daily data binned by week. While I can change the labels and gridlines on the x-axis using scale_x_date, that won't affect the points in the plot.

Here's a demonstration of the problem and my current (clumsy) solution.

library(zoo)
library(ggplot2)

d = as.Date(c(as.Date("2007-06-01"):as.Date("2008-05-31"))) # using zoo to reformat numeric 
x = runif(366, min = 0, max = 100)
df = data.frame(d,x)

# PROBLEM #    
p = ggplot(df, aes(d, x))
p + geom_point()
p + geom_boxplot() # more or less useless

# CURRENT FIX #
df$Year.Month <- format(df$d, "%Y-%m")
p = ggplot(df, aes(Year.Month, x))
p + geom_point(alpha = 0.75)
p + geom_boxplot() # where I'm trying to get to...

I feel certain that there's a more elegant way to do this from within ggplot. Am I right?

@shadow's answer below is much neater. But is there a way to do this using binning? Using stats in some form, perhaps?

Upvotes: 8

Views: 7433

Answers (1)

Dan
Dan

Reputation: 1778

You can treat Dates as dates in R, and use scale_x_date() in ggplot to get the x-labels you want.

  • Also, I find it easier to just create a new variable-factor called "Month" to group the boxplots by month. In this case I used lubridate to accomplish the task.

  • If you do not want to go through the trouble of creating a new variable "Month", your bloxplot will be plotted on the 15th of the month, making the viz reading a bit more difficult.

    library(magrittr)
    library(lubridate)
    library(dplyr)
    
    df %>%
      mutate(Date2 = as.Date(paste0("2000-", month(d), "-", "01"))) %>%
      mutate(Month = lubridate::month(d)) %>%
    
    ggplot(aes(Date2, x, group=Month)) +
      geom_boxplot() +
      scale_x_date(date_breaks="1 month", date_labels = "%b")
    

enter image description here

If you do not create the variable "Month", boxplots won't align nicely with the x tick marks:

enter image description here

Upvotes: 2

Related Questions