GGPLOT -Months/Days not showing

Hey Guys, I am hoping I could get some help with this minor problem I am facing.

I have a data frame called qq and it has two columns Date and Average. What I am trying to do is plot "Months" on the x-axis (going from Jan 1st to Dec 31st ) and on the y-axis "Average". I have been doing research and they recommend scale_x_date with date_label option set to %B. But for some reason, it's not working. As you can see it's like it's covered up and you cant see the months so really not sure what to do next. Any help appreciated. Thanks. N.B There is a picture of how it should really look the blue scatter plot diagram.


How it should look

I suspect your dates have been read in as characters or a factor. You can check this using


If so, you can use the as.Date function to convert them.


Thanks for replying, I ensured form earlier on that it was in date format by using the ymd function.

When i use the str function you can see for yourself that the data type is Date

I also even tried wrapped it around with the as.Date function as you can see below but still nothing.

Maybe you have too many individual months so that the labels are overlapping try setting date_breaks = "6 month" and rotating the labels with the theme function as I did in the code below.

DF <- data.frame(Date = seq.Date(from = as.Date("2020-01-01"), 
                                 to = as.Date("2020-12-31"), by = "day"),
                 Average = runif(n = 366, min = 10, max = 200))

ggplot(DF, aes(Date, Average)) + geom_point() + 
  scale_x_date(date_breaks = "6 month", date_labels = "%B") +
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))

Created on 2020-01-26 by the reprex package (v0.3.0)

1 Like

From the reference plot you are showing it seems to me you are trying to aggregate by the day of the year (no matter what year), if that is the case then you can do something like this.


qq <- data.frame(
    Date = seq.Date(ymd("2013-01-01"), ymd("2018-12-30"), by = "day"),
    Average = rnorm(2190)

qq %>%
    mutate(Date = ymd(paste("2000", month(Date), day(Date), sep = "-"))) %>%
    ggplot(aes(x = Date, y = Average)) +
    geom_point() +
    scale_x_date(date_breaks = "1 month",
                 date_labels = "%B") +
    theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5))


Thanks @andresrcs.

I did what you suggested and it works now.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.