ggplot2错误:geom_line需要以下缺少美学:y

时间:2018-04-22 18:48:13

标签: r ggplot2

我试图使用R中的循环从列表中绘制几个折线图。列表temp.list如下所示:

> 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

我只使用上面数据的某些部分来绘制,特别是列表中的第2到13项将在y轴上,即3,6,11,15,...,34。对于x轴,我希望第1周,第2周,......,第12周的刻度线。由于我不知道如何在gglplot命令中为x分配字符值,因此我为x轴创建了一个名为weeks的变量,如下所示:

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

我用于生成绘图的代码如下:

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))
}

此代码不会生成任何图表。当我只运行以下行时:

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))

我收到此错误:

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

任何帮助解决这个问题将不胜感激。我查看了一些相关的讨论here,但我不清楚如何继续。

此外,还欢迎任何更好的方法来生成多个图。从temp.list开始,我希望生成300多个单独的折线图(即,不是一张图表中的所有线)。

1 个答案:

答案 0 :(得分:0)

以下是使用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)
                 )