首页 > 解决方案 > 如何在 ggplot2 中调用特定的 id 或变量?

问题描述

这是已编辑的问题。我想创建一个基于面板数据的多面图。虽然仅使用一个 y 变量绘制面板数据相对简单,但我想问如何在 R 中使用应该出现在同一个图中的多个 y 变量创建一个图形。问题是我有两个“y”。每个 ggplot 都有,(aes(x=year, y=something, ...但我有两个“y”,即 source1 和 source2,我找不到创建在同一方面包含两个 y 变量的多方面图的解决方案。请参阅下面的面板数据说明。我想在 R 中绘制的面板数据如下所示: ![在此处输入图像描述

或 txt 格式:

id  area    element item    year    value   source1 source2 diff    relative
46  Australia   Yield   Barley  2000    20080   2.02    1.67    0.35    17.32673267
46  Australia   Yield   Barley  2010    18268   1.9 1.75    0.15    7.894736842
46  Australia   Yield   Barley  2018    23044   2.46    1.86    0.6 24.3902439
113 Australia   Yield   Maize   2000    58727   5.95    6.88    -0.93   -15.6302521
113 Australia   Yield   Maize   2010    67515   6.97    7.54    -0.57   -8.177905308
113 Australia   Yield   Maize   2018    82594   8   8.54    -0.54   -6.75
238 Australia   Yield   Potatoes    2000    314873  33.74   7.33    26.4    78.27504446
238 Australia   Yield   Potatoes    2010    383895  38.73   7.81    30.92   79.83475342
238 Australia   Yield   Potatoes    2018    426258  42.39   8.56    33.82   79.80655815
2224    Australia   Yield   Rapeseed    2000    12177   1.23    1.11    0.12    9.756097561
2224    Australia   Yield   Rapeseed    2010    11261   1.12    1.23    -0.11   -9.821428571
2224    Australia   Yield   Rapeseed    2018    12280   1.39    1.36    0.03    2.158273381
5557    Australia   Yield   Soybeans    2000    18714   1.87    2.08    -0.21   -11.22994652
5557    Australia   Yield   Soybeans    2010    19042   1.84    2.24    -0.4    -21.73913043
5557    Australia   Yield   Soybeans    2018    17027   1.81    2.43    -0.62   -34.25414365
757 Australia   Yield   SugC    2000    0   0   27.45   -27.45  #DIV/0!
757 Australia   Yield   SugC    2010    0   0   28.76   -28.76  #DIV/0!
757 Australia   Yield   SugC    2018    0   0   30.27   -30.27  #DIV/0!
8890    Australia   Yield   Sunflower seed  2000    10494   1.08    1.02    0.06    5.555555556
8890    Australia   Yield   Sunflower seed  2010    15414   1.25    1.13    0.12    9.6
8890    Australia   Yield   Sunflower seed  2018    18235   1.32    1.26    0.06    4.545454545
33335   Australia   Yield   Wheat   2000    18398   1.9 1.51    0.4 20.52631579
33335   Australia   Yield   Wheat   2010    15987   1.75    1.73    0.02    1.142857143
33335   Australia   Yield   Wheat   2018    19444   2.19    1.99    0.2 9.132420091
48  Brazil  Yield   Barley  2000    19437   2.12    1.88    0.25    11.32075472
48  Brazil  Yield   Barley  2010    33115   3.12    2.08    1.04    33.33333333
48  Brazil  Yield   Barley  2018    32591   3.31    2.33    0.98    29.60725076
115 Brazil  Yield   Maize   2000    27182   2.97    2.64    0.33    11.11111111
115 Brazil  Yield   Maize   2010    43667   4.1 3.76    0.34    8.292682927
115 Brazil  Yield   Maize   2018    51044   5   5.13    -0.13   -2.6
240 Brazil  Yield   Potatoes    2000    171813  17.38   3.59    13.79   79.34407365
240 Brazil  Yield   Potatoes    2010    258859  25.66   3.92    21.74   84.72330475
240 Brazil  Yield   Potatoes    2018    311760  30.59   4.32    26.27   85.87773782
2226    Brazil  Yield   Rapeseed    2000    17083   1.59    1.86    -0.26   -16.98113208
2226    Brazil  Yield   Rapeseed    2010    15217   1.37    2.22    -0.84   -62.04379562
2226    Brazil  Yield   Rapeseed    2018    12500   1.3 2.66    -1.36   -104.6153846
5559    Brazil  Yield   Soybeans    2000    24033   2.5 2.28    0.22    8.8
5559    Brazil  Yield   Soybeans    2010    29475   2.9 2.58    0.32    11.03448276
5559    Brazil  Yield   Soybeans    2018    33903   3.22    2.95    0.28    8.385093168
1478    Brazil  Yield   SugC    2000    0   0   15.01   -15.01  #DIV/0!
1478    Brazil  Yield   SugC    2010    0   0   16.43   -16.43  #DIV/0!
1478    Brazil  Yield   SugC    2018    0   0   18.2    -18.2   #DIV/0!
8892    Brazil  Yield   Sunflower seed  2000    16724   1.53    1.55    -0.01   -1.307189542
8892    Brazil  Yield   Sunflower seed  2010    11322   1.21    1.77    -0.57   -46.28099174
8892    Brazil  Yield   Sunflower seed  2018    16064   1.55    2.05    -0.5    -32.25806452
33337   Brazil  Yield   Wheat   2000    15156   1.81    1.43    0.38    20.99447514
33337   Brazil  Yield   Wheat   2010    28288   2.52    1.94    0.59    23.01587302
33337   Brazil  Yield   Wheat   2018    26238   2.69    2.56    0.13    
...and so on

我在 R 中创建了面板数据:

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

然后我在ggplot中尝试了这个情节:

ggplot(df, aes(x=year, y=diff, group=area, fill=area)) +
    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)

但是,它不显示面积上的刻面,然后在每个刻面内显示每种作物的 source1 和 source2 值。我想创建这样的东西:

在此处输入图像描述

问题变得更加复杂,因为我有很多维度:面积、项目、年份和这两个 y 变量:source1 和 source2。最重要的是创建将 source1 和 source2 显示为线条或条形的构面,以便进行比较。但问题是如何创建按年份、区域和项目显示这两个 y 变量的构面?所有这些都不会产生过于拥挤的图表。

因为 dc27 询问了图形示例,所以另一个可能的示例是: 在此处输入图像描述

其中 source1 和 source2 应该是 2 条并排显示每年、每个项目和每个区域的值。如果您对如何绘制此面板数据有任何其他建议,我们非常欢迎。

标签: rggplot2panel

解决方案


在这里,您的问题的一个可能解决方案是创建一个变量,将作物两两分组,您可以在facet_wrap函数中使用。

让我们以这个假例子为例,它将创建 6 个区域,其中有 6 种不同的作物,每年有一个值,持续 12 年。

df <- data.frame(Region = rep(paste("Region",1:6, sep = "_"), each = 12*6),
                 Crop = rep(paste("Crop",1:6, sep ="_"), 12*6),
                 Year = rep(paste("Year",1:12, sep ="_"), each = 6),
                 Value = runif(432,min = -1, max = 1))

现在,使用dplyr,我们可以计算 12 年内每个区域的每种作物的平均值,并添加面板组:

library(dplyr)

df %>% group_by(Region,Crop) %>%
  summarise(Mean = mean(Value)) %>%
  mutate(Panel = rep(paste("Panel",1:3), each = 2)) 

# A tibble: 36 x 4
# Groups:   Region [6]
   Region   Crop       Mean Panel  
   <fct>    <fct>     <dbl> <chr>  
 1 Region_1 Crop_1  0.0323  Panel 1
 2 Region_1 Crop_2  0.200   Panel 1
 3 Region_1 Crop_3  0.00647 Panel 2
 4 Region_1 Crop_4 -0.243   Panel 2
 5 Region_1 Crop_5 -0.433   Panel 3
 6 Region_1 Crop_6 -0.128   Panel 3
 7 Region_2 Crop_1  0.0655  Panel 1
 8 Region_2 Crop_2 -0.174   Panel 1
 9 Region_2 Crop_3  0.316   Panel 2
10 Region_2 Crop_4 -0.131   Panel 2
# … with 26 more rows

现在,如果您将此管道序列添加到ggplot中,您可以将您的裁剪两两分开。

df %>% group_by(Region,Crop) %>%
  summarise(Mean = mean(Value)) %>%
  #mutate(Group_Crop = as.numeric(factor(Crop))) %>%
  mutate(Panel = rep(paste("Panel",1:3), each = 2)) %>%
  ggplot(aes(x = Region, y = Mean, fill = Crop))+
  geom_col()+
  facet_wrap(~Panel, nrow = 1)+
  theme(legend.position = "top", axis.text.x = element_text(angle = 90, size=12), plot.title = element_text(hjust = 0.5))+
  geom_hline( yintercept=0, linetype="dashed")+
  labs(title="Average growth rate of yield tonne/ha in selected regions")

在此处输入图像描述

是你在找的吗?


推荐阅读