Reputation: 101
I currently have a bar chart where the height of each bar corresponds to the value of some variable over a single week, and consecutive bars correspond to consecutive weeks. However, I would like to label the x-axis by month. Here is a mock-up of what I have in mind:
A few notes:
So my primary question is how to group the x-axis into months as displayed in the mock-up. Secondarily, if anyone wants to help me with the double-headed arrow labelled "Past" and "Now" and the uneven bar width, that would be great.
Here's some code with dummy data if anyone wants to use it:
library("ggplot2")
weeks <- c("06/01/2019", "06/01/2019", "06/08/2019", "06/08/2019", "06/15/2019", "06/15/2019", "06/22/2019", "06/22/2019", "06/29/2019", "06/29/2019", "07/06/2019", "07/06/2019", "07/13/2019", "07/13/2019", "07/20/2019", "07/20/2019", "07/27/2019", "07/27/2019", "08/03/2019", "08/03/2019", "08/10/2019", "08/10/2019", "08/17/2019", "08/17/2019", "08/24/2019", "08/24/2019", "08/31/2019", "08/31/2019")
values <- c(29, 10, 27, 20, 36, 2, 23, 4, 17, 16, 20, 13, 27, 10, 29, 1, 26, 0, 19, 20, 27, 4, 16, 1, 10, 10, 10, 10)
types <- c("Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful")
df <- data.frame(Week=weeks, Value=values, Type=types)
p <- ggplot(data=df, aes(x=Week, y=Value, fill=Type))
p <- p + geom_bar(stat="identity")
p <- p + scale_fill_manual(values=c("Successful"="#89EEA8", "Unsuccessful"="#FE8081"))
p <- p + theme(axis.text.x = element_text(angle=90, hjust=1))
Upvotes: 0
Views: 56
Reputation: 281
Is this kind of what you were looking for? I think you're biggest problem is that your Week variable is a character instead of a date.
library("ggplot2")
weeks <- c("06/01/2019", "06/01/2019", "06/08/2019", "06/08/2019", "06/15/2019", "06/15/2019", "06/22/2019", "06/22/2019", "06/29/2019", "06/29/2019", "07/06/2019", "07/06/2019", "07/13/2019", "07/13/2019", "07/20/2019", "07/20/2019", "07/27/2019", "07/27/2019", "08/03/2019", "08/03/2019", "08/10/2019", "08/10/2019", "08/17/2019", "08/17/2019", "08/24/2019", "08/24/2019", "08/31/2019", "08/31/2019")
values <- c(29, 10, 27, 20, 36, 2, 23, 4, 17, 16, 20, 13, 27, 10, 29, 1, 26, 0, 19, 20, 27, 4, 16, 1, 10, 10, 10, 10)
types <- c("Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful", "Successful", "Unsuccessful")
df <- data.frame(Week=lubridate::date(as.POSIXct(weeks, format = "%m/%d/%Y")), Value=values, Type=types)
p <- ggplot(data=df, aes(x=Week, y=Value, fill=Type))
p <- p + geom_bar(stat="identity")
p <- p + scale_fill_manual(values=c("Successful"="#89EEA8", "Unsuccessful"="#FE8081"))
p <- p + theme(axis.text.x = element_text(angle=90, hjust=1))
p + scale_x_date()
Upvotes: 1