I'm struggling with formatting the x-axis for my plot since I'm working with dates and I hope someone can help me out!
Here is my data:
Sample_date <- c("2019-02-19", "2019-02-19", "2019-02-19", "2019-02-27", "2019-02-27", "2019-02-27", "2019-02-28", "2019-02-28", "2019-02-28", "2019-03-07", "2019-03-07", "2019-03-07", "2019-03-21", "2019-03-21", "2019-03-21", "2019-04-23", "2019-04-23", "2019-04-23", "2019-04-24", "2019-04-24", "2019-04-24")
Taxon <- c("D", "K", "A", "D", "K", "A", "D", "K", "A", "D", "K", "A", "D", "K", "A", "D", "K", "A", "D", "K", "A")
Value <- as.numeric(c("560", "9752", "8712", "1080", "16400", "18513", "640", "4267", "6534", "2320", "14660", "14697", "1520", "8094", "32670", "5040", "859796", "9801", "6560", "803232", "0"))
df <- data.frame(Sample_date, Taxon, Value)
df$Sample_date <- format(as.Date(df$Sample_date, format = "%Y-%m-%d"), "%d/%m")
title <- "Distribution 2019"
This is just a simplified extraction of my dataset, which spans over the whole year 2019. I would like the x-axis to show the specific dates and not a general scale with months or weeks. As some of the dates are really close, I cannot solve the issue with overlapping x-axis labels. Turning the dates 90° does not solve the issue as they would still overlap (the original dataset is big). So I would like to alternate the height of the dates on the x-axis by pasting a newline in front of every other date.
I thought I found the help I needed in these two posts (1 and 2), but this threw up new challenges when using dates as the formatting of my dates got lost.
I used the following code to make my plot:
ggplot(df, aes(x = as.Date(Sample_date, format = "%d/%m"), y = Value, fill = Taxon)) +
geom_area(stat = "identity", position = "stack") +
scale_x_date(
labels = function(dates) {
fixedLabels <- c()
for (l in 1:length(dates)) {
fixedLabels[l] <- paste0(ifelse(l %% 2 == 0, '', '\n'), dates[l])
}
return(fixedLabels)
},
breaks = as.Date(df$Sample_date, format = "%d/%m")
) +
theme(
axis.text = element_text(size = 18),
plot.title = element_text(hjust = 0, size = 18),
legend.text = element_text(size = 18),
legend.key.size = unit(1.5, "cm"),
legend.position = "bottom",
legend.title = element_blank(),
axis.title.y = element_text(size = 18),
axis.ticks.x = element_blank(),
panel.grid.minor.x = element_blank(),
plot.margin = unit(c(0.2, 2, -0.2, 0), "cm")
) +
ggtitle(title) +
scale_y_continuous(labels = function(x) format(x, big.mark = " ", scientific = FALSE)) +
labs(x = "", y = "cells/liter")
The dates appear twice since I included breaks
in the scale_x_date
. So, my question is: How do I remove one set of dates and display my dates on the x-axis as 21/03 instead of 2020-03-21?
I have searched the web for help, but cannot make it out by myself. I would very much appreciate if you could give me any hints!