I have a dataframe which looks something like the following (for example):
set.seed(42) ## for sake of reproducibility
n <- 6
dat <- data.frame(date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),
category=rep(LETTERS[1:2], n/2),
daily_count=sample(18:100, n, replace=TRUE)
)
dat
# date category daily_count
#1 2020-12-26 A 60
#2 2020-12-27 B 32
#3 2020-12-28 B 39
#4 2020-12-29 B 75
#5 2020-12-30 A 25
#6 2020-12-31 A 53
#7 2020-12-26 A 60
#8 2020-12-27 A 32
#9 2020-12-28 A 39
#10 2020-12-29 B 75
#11 2020-12-30 B 25
#12 2020-12-31 B 53
.
.
.
I am trying to create a boxplot with month and year on its X-Axis and it looks like this:
I would like to create a vertical line on 2013-08-23. I am using the following code for this:
library(ggplot2)
ggplot(dat)
geom_boxplot(aes(y=daily_count,
x=reorder(format(dat$date,'%b %y'),dat$date),
fill=dat$category))
xlab('Month & Year') ylab('Count') guides(fill=guide_legend(title="Category"))
theme_bw()
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10))
geom_vline(xintercept = as.numeric(as.Date("2013-08-23")), linetype=1, colour="red")
Any guidance please?
CodePudding user response:
In this answer:
- I've created a much bigger sample
- I'm using
yearmonth
fromtsibble
for simplicity - I've solved the issue with the vertical line
- I cleaned up a bit the use of labs for a cleaner code
set.seed(42)
dates <- seq.Date(as.Date("2012-08-01"), as.Date("2014-08-30"), "day")
n <- length(dates)
dat <- data.frame(date = dates,
category = rep(LETTERS[1:2], n/2),
daily_count = sample(18:100, n, replace=TRUE))
library(ggplot2)
library(tsibble)
ggplot(dat)
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category))
labs(x = 'Month & Year',
y = 'Count',
fill = "Category")
theme_bw()
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10))
geom_vline(xintercept = lubridate::ymd("2013-08-23"), linetype=1, colour="red", size = 2)
CodePudding user response:
I think there is no need to do any "expansion"... This in NOT my solution, but a modified solution by @dario. I think the problem was in aesthetics.
ggplot(dat, aes(y=daily_count,
x=date,
fill=category))
geom_boxplot()
labs(x = 'Month & Year', ylab= 'Count', fill = "Category")
theme_bw()
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10))
geom_vline(xintercept = as.Date("2020-12-28"), linetype=1, colour="red")