繁体   English   中英

面板数据和行 plot 用于 R 中的几个变量

[英]Panel data and line plot for several variables in R

我有面板数据,其中“时间”变量表示年份,“unit_id”表示国家。 我想制作所有数字变量的行 plot (这些变量的值跨年份)。 一条线代表一个变量的所有国家/地区。

我试过了:

exampl%>%
  gather() %>%                             # Convert to key-value pairs
  ggplot(aes(time)) +                     # Plot the values
  facet_wrap(~ key, scales = "free") +   # In separate panels
  geom_line()

但它不起作用,我有一个错误:美学必须是有效的数据列。 有问题的美学:x = 时间。 您是否输入错误数据列的名称或忘记添加 after_stat()

那是我的数据:

dput(exampl)
structure(list(unit_id = c("AGO", "AGO", "AGO", "AGO", "AGO", 
"AGO", "BEN", "BEN", "BEN", "BEN", "BEN", "BEN", "BGD", "BGD", 
"BGD", "BGD", "BGD", "BGD", "CIV", "CIV", "CIV", "CIV", "CIV", 
"CIV"), time = c(2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2005L, 
2006L, 2007L, 2008L, 2009L, 2010L, 2005L, 2006L, 2007L, 2008L, 
2009L, 2010L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L), cab = c(28.2321949, 
29.25037956, 37.5, 31.28475761, 32.32411957, 33.39371109, 26.71179771, 
27.9, 28.71139336, 29.71598816, 30.73922729, 34.2, 44.23, 50.52510246, 
46.5, 52.65625763, 55.14787292, 55.26, 58.9, 53.74567795, 54.78623962, 
55.83730316, 56.8971138, 57.96392822), dit = c(13.89709218, 20.40781491, 
16.2123884, 8.125547549, -10.76938871, 8.957039766, -3.445136313, 
-3.090532362, -6.554877979, -5.520878675, -6.717259628, -5.567234574, 
-0.250193221, 1.66538338, 1.076211925, 1.01077433, 3.470143475, 
1.82904182, 0.232220267, 2.690679668, -0.683459128, 1.864047049, 
6.662914109, 1.866754256), fdi = c(-3.526655479, -0.072001021, 
-1.368761628, 1.896315051, 3.136662133, -3.851110464, -0.133873602, 
-0.175920935, 1.706123419, 0.49455672, -0.193899058, 0.561144791, 
1.17120896, 0.635657188, 0.817754424, 1.449748396, 0.879494542, 
1.068934861, 2.042272555, 1.969861557, 2.178644539, 1.925660984, 
1.631267003, 1.439124272), government = c(16.02903718, 15.34172241, 
15.5369347, 16.81461146, 19.89859034, 17.04234392, 8.991073644, 
9.666734707, 9.568485256, 10.23358845, 11.28862086, 11.21186949, 
5.180225165, 5.440078557, 5.359456215, 5.178276878, 5.093745166, 
5.075325836, 14.42744504, 13.59161479, 13.33857571, 12.66976339, 
12.63534697, 12.17758809)), class = "data.frame", row.names = c(NA, 
-24L))

您需要从gather调用中排除“时间”列,因为您需要每一行都有一个时间值。

您还需要为 geom_line 调用提供 x 和 y 美学。

尝试这个:

exampl%>%
  tidyr::gather(key = "key", value = "value", -time, -unit_id) %>%
  ggplot(aes(x = time, y = value, color = unit_id)) +       
  facet_wrap(~ key, scales = "free") +   # In separate panels
  geom_line()

多面 ggplot 的示例图片

您可能还想查看pivot_longer() 这是一个新版本的gather ,可能对用户更加友好。

编辑(感谢 r2evans),这里是 pivot_longer。

exampl%>%
  tidyr::pivot_longer(
    -c(time, unit_id), 
    names_to = "key", 
    values_to = "value"
  ) %>%
  ggplot(aes(x = time, y = value, color = unit_id)) +                     
  facet_wrap(~ key, scales = "free") +   
  geom_line()

暂无
暂无

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

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