简体   繁体   English

如何在 R 中创建面板数据的多方面图?

[英]How to create a multi-facet grap of panel data in R?

I would like to create a multi-facet graph based on panel data.我想创建一个基于面板数据的多面图。 While graphing panel data with only one y-variable is relatively simple, I am asking how can I create a graph in R using multiple y-variables that should appear in the same graph.虽然仅使用一个 y 变量绘制面板数据相对简单,但我想问如何使用应该出现在同一图中的多个 y 变量在 R 中创建一个图形。 The problem is that I have two "y".问题是我有两个“y”。 Each ggplot has the (...aes(x=year, y=something, ...) but I have two "y's", namely source1 and source2 and I can't find a solution to create a multi-facet graph that contains both y-variables in the same facet. Please see the description of the panel data below. The panel data that I want to graph in R looks like this:每个 ggplot 都有(...aes(x=year, y=something, ...)但我有两个“y”,即 source1 和 source2 我找不到创建多面图的解决方案在同一方面包含两个 y 变量。请参阅下面对面板数据的描述。我要在 R 中绘制的面板数据如下所示:

structure(list(id = c(46L, 46L, 46L, 113L, 113L, 113L, 238L, 
238L, 238L, 2224L, 2224L, 2224L, 5557L, 5557L, 5557L, 757L, 757L, 
757L, 8890L, 8890L, 8890L, 33335L, 33335L, 33335L, 48L, 48L, 
48L, 115L, 115L, 115L, 240L, 240L, 240L, 2226L, 2226L, 2226L, 
5559L, 5559L, 5559L, 1478L), area = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("Australia and New Zealand", "Brazil", 
"Canada", "China", "India", "United States of America"), class = "factor"), 

I've created the panel data in R:我在 R 中创建了面板数据:

panel <- pdata.frame(data, index = c("id", "year"), drop.index = FALSE)

Then I've tried this plot in ggplot:然后我在ggplot中尝试了这个plot:

    geom_area() +
    scale_fill_viridis(discrete = TRUE) +
    theme(legend.position="none") +
    ggtitle("Yield") +
    theme_ipsum() +
    theme(
      legend.position="none",
      panel.spacing = unit(0.1, "lines"),
      strip.text.x = element_text(size = 8),
      plot.title = element_text(size=14)
    ) +
    facet_wrap(~item)

however, it does not shows facets on area and then inside each facet the values of source1 and source2 values for each crop.但是,它不显示面积上的刻面,然后在每个刻面内显示每种作物的 source1 和 source2 值。 I would like to create something like this:我想创建这样的东西:

在此处输入图像描述

The problem becomes even more complicate because I have many dimensions: area, item, years and those two y-variables: source1 and source2.问题变得更加复杂,因为我有很多维度:面积、项目、年份和这两个 y 变量:source1 和 source2。 The most important is to create facets that show source1 and source2 appear as lines or bars for comparison reasons.最重要的是创建将 source1 和 source2 显示为线条或条形的构面,以便进行比较。 But the problem is how to create facets that show this two y-variables by year, area and item?但问题是如何创建按年份、区域和项目显示这两个 y 变量的构面? And all of these without producing an overcrowded graph.所有这些都不会产生过于拥挤的图表。

Because dc27 asked examples of graph, another possible example would be:因为 dc27 询问了图形示例,所以另一个可能的示例是:

在此处输入图像描述

where source1 and source2 should be 2 bars side by side showing values per year, per item and per area.其中 source1 和 source2 应该是 2 条并排显示每年、每个项目和每个区域的值。 If you have any other suggestions on how to plot this panel data you are more than welcome.如果您对如何 plot 此面板数据有任何其他建议,我们非常欢迎您。

Ok based on your question and discussion, you want to provide values for average and GLOBIOM as y axis but always been plotted side-by-side in order to compare various item and area.好的,根据您的问题和讨论,您希望提供averageGLOBIOM的值作为 y 轴,但始终并排绘制以比较各种项目和区域。

Here, a possible way is to pivot your columns of interest for y in a longer format using for example pivot_longer function:在这里,一种可能的方法是使用例如pivot_longer function 以更长的格式 pivot 您对 y 感兴趣的列:

library(tidyr)
library(dplyr)
library(ggplot2)

data %>% pivot_longer(cols = c(average, GLOBIOM), names_to = "Var", values_to = "val")

# A tibble: 80 x 10
      id area                      element item    year value   diff relat        Var       val
   <int> <fct>                     <fct>   <fct>  <int> <int>  <dbl> <fct>        <chr>   <dbl>
 1    46 Australia and New Zealand Yield   Barley  2000 20080  0.35  17.32673267  average  2.02
 2    46 Australia and New Zealand Yield   Barley  2000 20080  0.35  17.32673267  GLOBIOM  1.67
 3    46 Australia and New Zealand Yield   Barley  2010 18268  0.15  7.894736842  average  1.9 
 4    46 Australia and New Zealand Yield   Barley  2010 18268  0.15  7.894736842  GLOBIOM  1.75
 5    46 Australia and New Zealand Yield   Barley  2018 23044  0.6   24.3902439   average  2.46
 6    46 Australia and New Zealand Yield   Barley  2018 23044  0.6   24.3902439   GLOBIOM  1.86
 7   113 Australia and New Zealand Yield   Maize   2000 58727 -0.93  -15.6302521  average  5.95
 8   113 Australia and New Zealand Yield   Maize   2000 58727 -0.93  -15.6302521  GLOBIOM  6.88
 9   113 Australia and New Zealand Yield   Maize   2010 67515 -0.570 -8.177905308 average  6.97
10   113 Australia and New Zealand Yield   Maize   2010 67515 -0.570 -8.177905308 GLOBIOM  7.54
# … with 70 more rows

Then, you can use "val" for y axis and "var" as fill argument for dodged barchart.然后,您可以将“val”用于 y 轴,将“var”用作闪避条形图的fill参数。 Using facet_grid , you can separate data for various area and items.使用facet_grid ,您可以分离各种区域和项目的数据。

Altogether, you can do something like this:总而言之,您可以执行以下操作:

library(tidyr)
library(dplyr)
library(ggplot2)

data %>% pivot_longer(cols = c(average, GLOBIOM), names_to = "Var", values_to = "val") %>%
  ggplot(aes(x = element, y = val, fill = Var))+
  geom_col(position = position_dodge())+
  facet_grid(area~item, scales = "free")

在此处输入图像描述

Does it answer your question?它回答了你的问题吗?


On your question, you mention to have plot being display per year, per item and per area.在您的问题上,您提到每年、每个项目和每个区域都会显示 plot。 One way of doing this is to do:这样做的一种方法是:

data %>% pivot_longer(cols = c(average, GLOBIOM), names_to = "Var", values_to = "val") %>%
  ggplot(aes(x = as.factor(year), y = val, fill = Var))+
  geom_col(position = position_dodge())+
  facet_grid(area~item, scales = "free")+
  theme(axis.text.x = element_text(angle = 60, hjust =1))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM