tchakravarty tchakravarty - 2 months ago 31
R Question

ggplot2: boxplot by date (yearmon) and group

Here is an example from the

man page:

p = ggplot(mpg, aes(class, hwy))
p + geom_boxplot(aes(colour = drv))


which looks like this:

enter image description here

I would like to make a very similar plot, but with (
yearmon
formatted) dates where the
class
variable is in the example, and a factor variable where
drv
is in the example.

Here is some sample data:

df_box = data_frame(
Date = sample(
as.yearmon(seq.Date(from = as.Date("2013-01-01"), to = as.Date("2016-08-01"), by = "month")),
size = 10000,
replace = TRUE
),
Source = sample(c("Inside", "Outside"), size = 10000, replace = TRUE),
Value = rnorm(10000)
)


I have tried a bunch of different things:


  1. Put an
    as.factor
    around the date variable, then I no longer have the nicely spaced out date scale for the x-axis:

    df_box %>%
    ggplot(aes(
    x = as.factor(Date),
    y = Value,
    # group = Date,
    color = Source
    )) +
    geom_boxplot(outlier.shape = NA) +
    theme_bw() +
    xlab("Month Year") +
    theme(
    axis.text.x = element_text(hjust = 1, angle = 50)
    )



enter image description here


  1. On the other hand, if I use
    Date
    as an additional
    group
    variable as suggested here, adding
    color
    no longer has any additional impact:

    df_box %>%
    ggplot(aes(
    x = Date,
    y = Value,
    group = Date,
    color = Source
    )) +
    geom_boxplot() +
    theme_bw()



enter image description here

Any ideas as to how achieve the output of #1 while still maintaining a
yearmon
scale x-axis?

Answer

Since you need separate boxes for each combination of Date and Source, use interaction(Source, Date) as the group aesthetic:

ggplot(df_box, aes(x = Date, y = Value, 
                   colour = Source, 
                   group = interaction(Source, Date))) + 
    geom_boxplot()

plot with date formatted x-axis

Comments