I have a dataframe containing values for three variables at three different dates:
df <- data.frame(date = as.Date(c(rep('2021-01-01',6),rep('2021-05-04',6),rep('2015-04-04',6))),
variable = rep(c('apple','bananna','carrot'),6),
value = rnorm(18,5,2))
I'd like to make a boxplot with fill relating to variable, date on the x axis and value on the y axis:
ggplot(df,aes(x=as.factor(date),y=value,fill=variable)) geom_boxplot()
This fills based on variable, but it doesn't scale date according to the actual differences in time:
If I set group=date
, it gets scaled properly, but fill=variable
stops working:
ggplot(df,aes(x=date,y=value,group=date,fill=variable)) geom_boxplot()
How do I get the date to scale properly and also fill based on variable name?
CodePudding user response:
Seems like this was already answered
CodePudding user response:
The problem can be solved with the creation of a factor variable combining date
and variable
. This is done with interaction
.
I have also added code to increase the plot window width, in order to make the bars and labels more visible.
df <- data.frame(date = as.Date(c(rep('2021-01-01',6),rep('2021-05-04',6),rep('2015-04-04',6))),
variable = rep(c('apple','bananna','carrot'),6),
value = rnorm(18,5,2))
library(ggplot2)
x11(width = 20, height = 8) # this is not stricktly needed
ggplot(df, aes(x = date, y = value, fill = variable))
geom_boxplot(aes(group = interaction(date, variable)))
scale_x_date(
breaks = unique(df$date),
date_labels = "%Y-%m-%d"
)
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
Created on 2022-06-02 by the reprex package (v2.0.1)
The same can be done by creating a temporary new factor in a dplyr
pipe to mutate
.
suppressPackageStartupMessages({
library(ggplot2)
library(dplyr)
})
df %>%
mutate(group = interaction(date, variable)) %>%
ggplot(aes(x = date, y = value, fill = variable))
geom_boxplot(aes(group = group))
scale_x_date(
breaks = unique(df$date),
date_labels = "%Y-%m-%d"
)
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
Created on 2022-06-02 by the reprex package (v2.0.1)