Ggplot not showing all dates on x asis even when forced

Hello, i am trying to do a visualization with ggplot.

ggplot(catdata,aes(x=Date,y=pr,col=Category))+geom_line()+scale_x_yearmon(format="%Y %m",n=16)+theme(axis.text.x = element_text(angle = 30, hjust = 1))

Rplot05

It looks like this. As you can see 2 months are missing.

but if I use

catdata$m==6

I get several TRUE , meaning its not empty. Anyone have an idea why it hides those x axis labels?

I don't know why with is happening, but I would use scale_x_date instead of scale_x_yearmon.

Never forget to check that your date column is really in date format - sometimes it changes on you - class(catdata$Date).

2 Likes

It would be good to see your catdata.

1 Like

I used the zoo library so my if i do

class(catdata$Date)

i get : [1] "yearmon"

My data looks like the following

Please post a data sample that we can use to investigate your problem. For example, post the output of dput(catdata[catdata$Category=="animals", ]).

2 Likes

Could you please turn this into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

Right now the best way to install reprex is:

# install.packages("devtools")
devtools::install_github("tidyverse/reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ, linked to below.

> dput(catdata[catdata$Category=="animals", ]

structure(list(y = c(2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2017, 2017, 2018, 2018), m = c(1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 1, 2), Category = c("animals", "animals",
"animals", "animals", "animals", "animals", "animals", "animals",
"animals", "animals", "animals", "animals", "animals", "animals"
), pr = c(92.560799, 93.895604, 217.773775, 111.958975, 118.432367,
94.326318, 136.052199, 144.02308, 194.307174, 456.824091, 601.234364,
359.550693, 280.035412, 271.973927), Date = structure(c(2017,
2017.08333333333, 2017.16666666667, 2017.25, 2017.33333333333,
2017.41666666667, 2017.5, 2017.58333333333, 2017.66666666667,
2017.75, 2017.83333333333, 2017.91666666667, 2018, 2018.08333333333
), class = "yearmon")), .Names = c("y", "m", "Category", "pr",
"Date"), row.names = c(NA, -14L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = c("y", "m"), drop = TRUE, indices = list(
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L), group_sizes = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
y = c(2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017,
2017, 2017, 2017, 2018, 2018), m = c(1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 1, 2)), row.names = c(NA, -14L), class = "data.frame", vars = c("y",
"m"), drop = TRUE, .Names = c("y", "m")))

I hope that helps

I'm not sure what's going wrong with scale_x_yearmon (more about this below). In any case, I'd suggest using standard R Date format instead. Here's an example using the data sample you provided. We create column Date2 using y and m and then use scale_x_date to get the desired breaks and labels:

library(zoo)
library(tidyverse)

# Create a Date column from columns y and m
catdata = catdata %>% 
  mutate(Date2 = as.Date(paste0(y,"-",m,"-",1)))

ggplot(catdata, aes(x=Date2, y=pr, col=Category, group=Category)) + 
  geom_line() + 
  scale_x_date(date_breaks="1 month", date_labels="%Y %m") + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1))

Rplot01

Back to scale_x_yearmon: I can reproduce the behavior you're seeing when I use n=16. Look at what happens with n=20. Note that all of the labels are present. However, there's an extra label at December 2016 to the left of the edge of the plot panel. In addition, note that some of the x-axis labels are darker than others, presumably due to overplotting (that is, some of the labels are being drawn more than once). I haven't traced through what scale_x_yearmon is doing, but it looks like either it has a bug, or (looking at the help for scale_x_yearmon) perhaps it's only intended to be used with zoo model objects or time series objects.

ggplot(catdata,aes(x=Date, y=pr, col=Category, group=Category)) + 
  geom_line() + 
  scale_x_yearmon(format="%Y %m", n=20) +
  theme(axis.text.x = element_text(angle = 30, hjust = 1))

Rplot03

2 Likes