3

I am struggling to format the x-axis of a stacked area plot with ggplot2. This is my data:

df <- data.frame(
     Taxon = c("Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms"),
     Abundance = c(14192, 120, 440, 6000, 80, 360, 25800, 4384, 169428, 879103, 2000, 52360, 213508, 22560, 470900, 472808, 11920, 316312, 81504, 6280, 15096, 50656, 11360, 43448),
     Date = c("05/01/2019", "05/01/2019", "05/01/2019", "09/03/2019", "09/03/2019", "09/03/2019", "11/04/2019", "11/04/2019", "11/04/2019", "01/05/2019", "01/05/2019", "01/05/2019", "01/06/2019", "01/06/2019", "01/06/2019", "01/07/2019", "01/07/2019", "01/07/2019", "01/08/2019", "01/08/2019", "01/08/2019", "01/09/2019","01/09/2019", "01/09/2019")
)

df %>% group_by(Date, Taxon) %>% summarise_all(sum) -> df1

ggplot(df1, aes(x=as.Date(Date, format="%d/%m/%Y"), Abundance, colour=Taxon, fill=Taxon)) + geom_area(stat="identity", position="stack") + labs(x = "", y = "") + scale_x_date(labels = date_format("%d/%m/%Y")) + theme(axis.text.x = element_text(angle = 45, vjust = 0.5))

This gives me the following plot:

ggplot2

My questions are now:

  1. How do I get the x axis labels to correspond to the dates in df2$Date and not the first of the month?
  2. I would like all dates to be visible on the x axis. I tried to use scale_x_continuous(labels=dates) with dates <- unique(df1$Date) but this gives me an error. How do I get it right?

I would very much appreciate any hints!

3
  • If you want to adjust the breaks (i.e. the values at which labels are shown) that's with the date_breaks argument, not the date_labels argument Commented Jan 16, 2020 at 15:49
  • Does this answer your question? R: ggplot display all dates on x axis Commented Jan 16, 2020 at 15:58
  • Thanks for your help! Jonathan provided what I needed. Commented Jan 17, 2020 at 8:42

3 Answers 3

1

You should set the breaks argument in scale_x_date as the same object that you assigned to labels.

scale_x_date(labels = as.Date(df1$Date, format="%d/%m/%Y"), 
breaks = as.Date(df1$Date, format="%d/%m/%Y"))

Plot

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I wanted! Thank you so much!
1

Use the breaks argument

To change date frequency, you have to use the break argument on scale_x_date() function, like this:

+ scale_x_date(labels = date_format("%d/%m/%Y"), breaks = date_breaks("2 weeks"))

This is the complete code for a graph with 2 weeks break:

library(dplyr)
library(ggplot2)
library(scales)

df1 %>% ggplot(aes(
  x = as.Date(Date, format = "%d/%m/%Y"),
  Abundance,
  colour = Taxon,
  fill = Taxon
)) +
  geom_area(stat = "identity", position = "stack") +
  labs(x = "", y = "") +
  scale_x_date(labels = date_format("%d/%m/%Y"), breaks = date_breaks("2 weeks")) +
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))

enter image description here

Hope this helps.

1 Comment

Thanks for your help, but Jonathan's answer was what I needed.
0

Adjust the date_breaks argument in scale_x_date, ie.:

ggplot(df1, aes(x=as.Date(Date, format="%d/%m/%Y"), Abundance, colour=Taxon, fill=Taxon)) + 
  geom_area(stat="identity", position="stack") + labs(x = "", y = "") + 
  scale_x_date(labels = date_format("%d/%m/%Y"),date_breaks = '1 week') + 
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.