user9302275
user9302275

Reputation:

Multi-Line Time Series Not Formatting Correctly

I'm trying to do a multiple line time series plot using ggplot2. I keep following the directions I'm finding, but nothing appears to work.

I've already tried multiple attempts based on documentation.

The data I'm working with looks like this:

+----------------+----------+--------+
| purchase_month |   type   | orders |
+----------------+----------+--------+
| 2018-07        | local    |    199 |
| 2018-08        | local    |    231 |
| 2018-09        | local    |    222 |
| 2018-10        | local    |    190 |
| 2018-07        | domestic |   1102 |
| 2018-08        | domestic |    924 |
| 2018-09        | domestic |    999 |
| 2018-10        | domestic |    779 |
+----------------+----------+--------+


ggplot(data = sample_data, aes(x = purchase_month, y = orders)) + 
geom_line(aes(color = type), size = 1) +
scale_color_manual(values = c("#00AFBB", "#E7B800")) +
theme_minimal()

When I run the code, it does not work. When I run the first two lines, it doesn't even plot any points. It just builds the X and Y axis.

Upvotes: 2

Views: 49

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269421

If we change the purchase_month column to be class yearmon and then specify the same as the X scale it will work. Using the data shown reproducibly in the Note at the end:

library(zoo)

sample_data2 <- transform(sample_data, purchase_month = as.yearmon(purchase_month))

ggplot(data = sample_data2, aes(x = purchase_month, y = orders)) + 
  geom_line(aes(color = type), size = 1) +
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  theme_minimal() +
  scale_x_yearmon()

or another approachs is to convert to a wide form multivariate series with one column per type and use autoplot.zoo. Note that by omitting facet = NULL a multipanel output can be produced.

library(zoo)

z <- read.zoo(sample_data, index = "purchase_month", split = "type", FUN = as.yearmon)

autoplot(z, geom = "blank", facet = NULL) + 
  geom_line(size = 1) +
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  theme_minimal() +
  scale_x_yearmon()

Either of these produces:

screenshot

Note

Lines <- "
purchase_month |   type   | orders
2018-07        | local    |    199
2018-08        | local    |    231
2018-09        | local    |    222
2018-10        | local    |    190
2018-07        | domestic |   1102
2018-08        | domestic |    924
2018-09        | domestic |    999
2018-10        | domestic |    779"
sample_data <- read.table(text = Lines, header = TRUE, sep = "|", strip.white = TRUE)

Upvotes: 2

Marcus Nunes
Marcus Nunes

Reputation: 861

The tricky part is to have a column in your dataset as a date. Let's do it together. First, let's use this data to solve our problem:

sample_data <- structure(list(purchase_month = structure(c(17713, 17744, 17775,
  17805, 17713, 17744, 17775, 17805), class = "Date"), type = structure(c(2L,
  2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("domestic", "local"), class = "factor"), 
  orders = c(199, 231, 222, 190, 1102, 924, 999, 779)), row.names = c(NA, 
  -8L), class = "data.frame")

This is the data we will be using:

sample_data
  purchase_month     type orders
1        2018-07    local    199
2        2018-08    local    231
3        2018-09    local    222
4        2018-10    local    190
5        2018-07 domestic   1102
6        2018-08 domestic    924
7        2018-09 domestic    999
8        2018-10 domestic    779

Notice that purchase_month is not a date. I will transform it to a date adding -01 to its end, because every date in R needs a day. Since you are interested in months, we can use any day here. Then, I will use ymd function from lubridate package to inform R that our strings are in year-month-day format:

library(lubridate)
sample_data$purchase_month <- ymd(paste0(sample_data$purchase_month, "-01"))

Noe you just need to use you own ggplot2 code in order to have your plot:

ggplot(data = sample_data, aes(x = purchase_month, y = orders)) + 
  geom_line(aes(color = type), size = 1) +
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  theme_minimal()

enter image description here

Upvotes: 2

Rui Barradas
Rui Barradas

Reputation: 76402

There are two things wrong with your code:

  1. The data column purchase_month is not an object of class "Date".
  2. You need to group the data by type.

I mean the following.

sample_data$purchase_month <- as.Date(paste(sample_data$purchase_month, "01", sep = "-"))

ggplot(data = sample_data, 
       aes(x = purchase_month, y = orders, color = type), group = type) + 
  geom_line(size = 1) +
  scale_color_manual(values = c("#00AFBB", "#E7B800")) +
  theme_minimal()

enter image description here

Upvotes: 1

Related Questions