【问题标题】:ggplot2 error: geom_line requires the following missing aesthetics: yggplot2 错误:geom_line 需要以下缺失的美学:y
【发布时间】:2018-04-22 18:48:13
【问题描述】:

我正在尝试使用 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 个单独的折线图(即,并非所有折线都在一个图表中)。

【问题讨论】:

  • 至少在您向我们展示的数据中,您有一个名为Num_markets 的列,而不是markets。所以你试图绘制一个不存在的变量
  • @camille。我没有使用Num_markets 作为图中的变量。我在循环中声明了另一个变量markets,它将为每次迭代从 temp.list 中选择项目 2 到 13。
  • 我明白了。 ggplot 通常会期望一个数据框,其中包含您分配给 x、y 等的列名。您可以尝试在循环中创建一个包含周数和市场的临时数据框吗?

标签: r ggplot2


【解决方案1】:

这是使用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]]
...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2021-09-16
    • 2019-07-15
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多