Rnovice
Rnovice

Reputation: 353

ggplot2 error: geom_line requires the following missing aesthetics: y

I am trying to plot several line graphs from a list using a loop in R. The list temp.list looks like this:

> temp.list[1]
$`1`
  NEW_UPC       Week1 Week2 Week3 Week4 Week5 Week6 Week7 Week8 Week9 Week10 Week11 Week12
5 11410008398     3     6    11    15    15    27    31    33    34     34     34     34
  Life Status Num_markets    Sales
5  197      1          50 186048.1

I use only some part of the data above to plot, specifically items 2 to 13 in the list will go on the y-axis, i.e. 3,6,11,15,...,34. For x-axis, I would like to have Week 1, Week 2, ..., Week 12 at the tick marks. As I don't know how to assign character values to x in gglplot command, I created a variable called weeks for x-axis as below:

weeks = c(1,2,3,4,5,6,7,8,9,10,11,12)

The code that I used to generate the plot is below:

for (i in 1:2) {
  markets= temp.list[[i]][2:13]
  ggplot(data = NULL,aes(x=weeks,y=markets))+geom_line()+
    scale_x_continuous(breaks = seq(0,12,1))+
    scale_y_continuous(breaks = seq(0,50,5))
}

This code does not generate any plot. When I run just the below lines:

ggplot(data = NULL,aes(x=weeks,y=markets))+geom_line()+
    scale_x_continuous(breaks = seq(0,12,1))+
    scale_y_continuous(breaks = seq(0,50,5))

I get this error:

Error: geom_line requires the following missing aesthetics: y
In addition: Warning message:
In (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  :
  row names were found from a short variable and have been discarded

Any help to fix this will be appreciated. I looked at some related discussions here, but I am not clear how to proceed.

Also, any better way to generate multiple plots is also welcome. From temp.list, I am looking to generate over 300 separate line graphs (i.e, not all lines in one chart).

Upvotes: 0

Views: 4370

Answers (1)

bVa
bVa

Reputation: 3948

Here is a solution using tidyverse:

library(tidyverse)

# 1: create a function for the plot
## gather will give a long format data.frame
## then extract the number of the week and format it to numeric
## then use ggplot on this data
plot_function <- function(data) {
  data %>%
      gather(., key = week, value = markets, starts_with("Week")) %>%
      mutate(week= as.numeric(str_sub(week, 5, -1))) %>%
      ggplot(., aes(x = week, y = markets)) +
      geom_line() +
      scale_x_continuous(breaks = seq(0, 12, 1)) +
      scale_y_continuous(breaks = seq(0, 50, 5))
  }

# 2: map the function to your list
plots <- map(temp.list, plot_function)

# 3: access each plot easily
plots[[1]]
plots[[2]]
...

enter image description here enter image description here

#Data used for this example
temp.list = list('1' = data.frame(NEW_UPC = 11410008398, 
                                  Week1 = 3, 
                                 Week2 = 6,
                                  Week3 = 11,
                                  Week4 = 15,
                                  Week5 = 15,
                                  Week6 = 27,
                                  Week7 = 31,
                                  Week8 = 33,
                                  Week9 = 34,
                                  Week10 = 34,
                                  Week11 = 34,
                                  Week12 = 34,
                                  Life  = 197,
                                  Status  = 1,
                                  Num_markets  = 50,
                                  Sales = 186048.1),
                 '2' = data.frame(NEW_UPC = 11410008398, 
                                  Week1 = 4, 
                                  Week2 = 5,
                                  Week3 = 8,
                                  Week4 = 13,
                                  Week5 = 14,
                                  Week6 = 25,
                                  Week7 = 29,
                                  Week8 = 30,
                                  Week9 = 31,
                                  Week10 = 33,
                                  Week11 = 34,
                                  Week12 = 34,
                                  Life  = 201,
                                  Status  = 1,
                                  Num_markets  = 50,
                                  Sales = 186048.1)
                 )

Upvotes: 0

Related Questions