【问题标题】:Drawing a ggplot out of a nested list with dataframes使用数据框从嵌套列表中绘制 ggplot
【发布时间】:2020-01-02 00:39:04
【问题描述】:

这是我的数据框

df <- tibble("Fruit_Name" = c("Banana", "Apple", "Orange", "Peach", "Pear", "Watermelon"), "Code" = c(1,1,2, 2,3, 3), "Share_2002" = c(0.116, 3.442, 2.445, 1.932, 0.985, 0.321), "Share_2010" = c(1.4, 2.8, 2.4, 2.10, 0.99, 1.04), "Share_2018" = c(0.161, 0.232, 1.234, 0.456, 0.089, 0.06), "Share_2018_bis" = c(0.5, 0.34, 1.5, 1.2, 0.75, 1.8))

从这个初始数据框我构建了一个嵌套列表:

    fruits <- df %>%
  rename("2002" = Share_2002,
         "2010" = Share_2010,
         "2018" = Share_2018,
         "2018bis" = Share_2018_bis) %>%
  arrange(Code)%>%
  group_split(Code) %>%
  map(~list(fruit_normal = .x, fruit_long = .x %>%
                            gather(Year, Share, c(3,4,5), -Code, -Fruit_Name) %>%
                            arrange(Fruit_Name) %>%
                            mutate_all(funs(str_replace(., "2018bis", "2018"))),
            fruits2 = .x %>%
              gather(Year, Share, c(3,4,6), -Code, -Fruit_Name) %>%
              arrange(Fruit_Name) %>%
              mutate_all(funs(str_replace(., "2018bis", "2018")))))

我想要一个或多或少像这样的图表

ggplot(x, aes(x = Year, y = Share, color = Fruit_Name)) + 
geom_line(size = 2) + 
facet_grid(Fruit_Name~ .)

这使我可以为每个代码组中的每个水果创建一个 x 轴为年份和 y 轴为共享的图表。

我无法为 ggplot 指定每个嵌套数据框! 我不想从列表中构建数据框,但尝试访问现有的。

我尝试过这样的事情:

myplot <- function(x){ggplot(x, aes(x = Year, y = Share, color = Fruit_Name)) + 
geom_line(size = 2) + 
facet_grid(Code~ .)}

i <- c(1,2,3)
for (x in i){
  lapply(fruits[["i"]], map_depth(x, 2, myplot(fruits2)))
}

但它不起作用,因为在任何情况下 ggplot 都需要数据框作为参数而不是列表!!

谢谢

【问题讨论】:

  • 我不明白你在做什么。而且您的示例似乎有未定义的变量,例如Occupationfruit_long。也许fruits[["i"]] 应该是fruits[[i]]。但 ggplot 只会采用 data.frames;它不会列出列表。你无法改变这一点。您需要确保传入一个 data.frame 进行绘图。
  • 如果你运行代码你会看到列表 fruits 包含 3 个嵌套列表,每个列表包含 3 个数据帧(fruit_normal、fruit_long、fruit2)。我想为每个子列表绘制 Year, Share for BOTH fruit_long 和 fruit_2。因此,我传递给 ggplot 的参数是 2 个数据帧,我只是不知道如何访问它们,因为它们在列表中的列表中,而且我也不知道如何遍历它们,因为我想要相同的图对于每个子列表。谢谢!

标签: r ggplot2 dplyr purrr


【解决方案1】:

我认为您的示例数据中有错字,因为您没有像其他人一样重命名 Share_2010,所以我从

开始
fruits <- df %>%
  rename("2002" = Share_2002,
         "2010" = Share_2010,
         "2018" = Share_2018,
         "2018bis" = Share_2018_bis) %>%
  arrange(Code)%>%
  group_split(Code) %>%
  map(~list(fruit_normal = .x, fruit_long = .x %>%
                            gather(Year, Share, c(3,4,5), -Code, -Fruit_Name) %>%
                            arrange(Fruit_Name) %>%
                            mutate_all(funs(str_replace(., "2018bis", "2018"))),
            fruits2 = .x %>%
              gather(Year, Share, c(3,4,6), -Code, -Fruit_Name) %>%
              arrange(Fruit_Name) %>%
              mutate_all(funs(str_replace(., "2018bis", "2018")))))

然后为您的绘图代码添加分组美学并使其成为一个函数

plotFruit <- function(df) {
    ggplot(df, aes(group = Fruit_Name, x = Year, y = Share, color = Fruit_Name)) + 
        geom_line(size = 2) + 
        facet_grid(Fruit_Name~ .)}

并使用

解压这个庞大的数据结构
map(fruits, pluck, "fruit_long") %>% map(plotFruit)

map(fruits, pluck, "fruits2") %>% map(plotFruit)

这就是你要找的吗?

【讨论】:

  • 这个很有用!如果我想在同一张图上看到两个图来查看 2018 年和 2018 年之二之间的差异怎么办?我已经修改了函数以包含 2 个数据帧,但我不确定如何使用 map 和 pluck 传递它们!
  • map2 传递 x 和 y 以及函数 plotFruit 可能是个好主意,但我如何通过 pluck 传递它们?谢谢
猜你喜欢
  • 2017-05-26
  • 2017-07-02
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
相关资源
最近更新 更多