Pecker
Pecker

Reputation: 133

R - ggplot2 issues with date as character for x-axis

I'm new to using R and ggplot2, and I cannot figure out how to fix the issue with the graph I am trying to create.

enter image description here

Here is what the graph looks like at the moment. I have dates on the x-axis, but for some reason they don't work with the year, but only order by the month and day.

Here is a screenshot of the data I am working with:

enter image description here

As you can see, the order looks correct here.

I produced a re-creatable sample where the same issue occurs

Week <- c("1/6/2019", "1/26/2020", "6/7/2020")
Coronavirus <- c(0, 16, 67)
Grubhub <- c(65, 23, 59)
UberEats <- c(52, 80, 68)
Doordash <- c(27, 35, 50)

my.data <- data.frame(Week, Coronavirus, Grubhub, UberEats, Doordash)
my.data

test.output <- ggplot(data = my.data, aes(x = Week, group = 1)) +
  geom_line(aes(y = Coronavirus), color = "red") +
  geom_line(aes(y = Grubhub), color = "darkgreen") +
  geom_line(aes(y = UberEats), color = "blue") +
  geom_line(aes(y = Doordash), color = "purple") +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Weekly Google Search Term Interest by Category", y = "Search Term Interest", x = "Week [2020]") 
test.output

The order of the dates on the x-axis are incorrect here as well.

enter image description here

Any suggestions on how I can fix the issue?

Upvotes: 1

Views: 3515

Answers (2)

www
www

Reputation: 39154

You need to change your Week column to date format.

library(ggplot2)
library(lubridate)

my.data <- data.frame(Week, Coronavirus, Grubhub, UberEats, Doordash)
my.data$Week <- mdy(my.data$Week)

test.output <- ggplot(data = my.data, aes(x = Week, group = 1)) +
  geom_line(aes(y = Coronavirus), color = "red") +
  geom_line(aes(y = Grubhub), color = "darkgreen") +
  geom_line(aes(y = UberEats), color = "blue") +
  geom_line(aes(y = Doordash), color = "purple") +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Weekly Google Search Term Interest by Category", y = "Search Term Interest", x = "Week [2020]") 
test.output

enter image description here

If you don't want to use the mdy function. The following will also work.

my.data$Week <- as.Date(my.data$Week, format = "%m/%d/%Y") 

Upvotes: 2

r2evans
r2evans

Reputation: 160407

  1. "1/6/2019" is not a date, it is a string. ggplot2 (and most other things) should never infer that you want it dealt with as a date. What it does "know" is that it is a string, and since it is not a factor, it orders things lexicographically (not year-first). Note that this matches your observation that it sorts first by month, then day, since those are the first few characters in the strings.

  2. Once we make the Week column a proper Date class, if you want to keep the presentation in the "%m/%d/%Y" format, you need to add scale_x_date.

Week <- c("1/6/2019", "1/26/2020", "6/7/2020")
Coronavirus <- c(0, 16, 67)
Grubhub <- c(65, 23, 59)
UberEats <- c(52, 80, 68)
Doordash <- c(27, 35, 50)

my.data <- data.frame(Week, Coronavirus, Grubhub, UberEats, Doordash)
my.data$Week <- as.Date(my.data$Week, format = "%m/%d/%Y")
my.data

test.output <- ggplot(data = my.data, aes(x = Week, group = 1)) +
  geom_line(aes(y = Coronavirus), color = "red") +
  geom_line(aes(y = Grubhub), color = "darkgreen") +
  geom_line(aes(y = UberEats), color = "blue") +
  geom_line(aes(y = Doordash), color = "purple") +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Weekly Google Search Term Interest by Category", y = "Search Term Interest", x = "Week [2020]") 
test.output

ggplot2 with default date labels

If you prefer "%m/%d/%Y", then

test.output + scale_x_date(date_labels = "%m/%d/%Y")

ggplot2 with m/d/Y labels

Upvotes: 2

Related Questions