如何从面板/纵向数据中选择一年来绘制图表?
Posted
技术标签:
【中文标题】如何从面板/纵向数据中选择一年来绘制图表?【英文标题】:How can I select a single year from panel/longitudinal data to graph? 【发布时间】:2020-08-18 09:16:13 【问题描述】:我有以下 167 个国家/地区的数据集,以及 2009-2017 年。 head(df) 如下所示:
area_code year area_name area_group Executive Constraints Government Effectiveness Government Integrity
1 AFG 2009 Afghanistan Asia-Pacific 5.940404 5.601888 5.817723
2 AFG 2010 Afghanistan Asia-Pacific 5.940404 5.614891 5.817723
3 AFG 2011 Afghanistan Asia-Pacific 5.940404 4.164573 6.077723
4 AFG 2012 Afghanistan Asia-Pacific 5.940404 4.171181 6.077723
5 AFG 2013 Afghanistan Asia-Pacific 5.940404 4.265419 6.837723
6 AFG 2014 Afghanistan Asia-Pacific 5.940404 4.238977 6.837723
Political Accountability Regulatory Quality Rule of Law Contract Enforcement Investor Protection
1 3.852381 4.622449 5.684202 4.211907 4.514932
2 3.495238 4.582903 5.684202 4.211907 4.514932
3 3.495238 4.688990 5.684202 4.211907 4.612633
4 3.495238 4.683196 5.684202 4.211907 4.641368
5 3.495238 4.943797 5.684202 4.211907 4.635621
6 4.780952 4.943783 5.684202 4.211907 4.641368
Property Rights Val.Avg HDI LE Subject Descriptor Units Scale
1 8.746632 5.443613 0.453 60.484 General government total expenditure Percent of GDP NA
2 9.032347 5.432727 0.463 61.028 General government total expenditure Percent of GDP NA
3 8.937204 5.312542 0.471 61.553 General government total expenditure Percent of GDP NA
4 8.937204 5.315825 0.482 62.054 General government total expenditure Percent of GDP NA
5 8.937204 5.439057 0.487 62.525 General government total expenditure Percent of GDP NA
6 8.937204 5.579613 0.491 62.966 General government total expenditure Percent of GDP NA
Country/Series-specific Notes Estimates Start After
1 See notes for: General government total expenditure (National currency). 2018
2 See notes for: General government total expenditure (National currency). 2018
3 See notes for: General government total expenditure (National currency). 2018
4 See notes for: General government total expenditure (National currency). 2018
5 See notes for: General government total expenditure (National currency). 2018
6 See notes for: General government total expenditure (National currency). 2018
e_p_GDP
1 21.17
2 20.800999999999998
3 21.937000000000001
4 25.027999999999999
5 24.977
6 25.402000000000001
我想知道如何仅绘制给定年份或国家/地区的数据? 例如。一个国家(例如阿富汗)在 2009 年至 2017 年间某个变量(例如 HDI)的散点图,或者是所有国家(例如 2017 年)一年内某个变量的散点图。
这里是 dput(head(df)):
structure(list(area_code = c("AFG", "AFG", "AFG", "AFG", "AFG",
"AFG"), year = c("2009", "2010", "2011", "2012", "2013", "2014"
), area_name = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan"), area_group = c("Asia-Pacific",
"Asia-Pacific", "Asia-Pacific", "Asia-Pacific", "Asia-Pacific",
"Asia-Pacific"), `Executive Constraints` = c(5.9404040404, 5.9404040404,
5.9404040404, 5.9404040404, 5.9404040404, 5.9404040404), `Government Effectiveness` = c(5.6018883995,
5.6148910662, 4.164573318, 4.171181318, 4.2654191847, 4.238976518
), `Government Integrity` = c(5.8177230501, 5.8177230501, 6.0777230501,
6.0777230501, 6.8377230501, 6.8377230501), `Political Accountability` = c(3.8523809524,
3.4952380952, 3.4952380952, 3.4952380952, 3.4952380952, 4.780952381
), `Regulatory Quality` = c(4.6224485009, 4.5829025009, 4.6889900009,
4.6831962509, 4.9437972509, 4.9437830009), `Rule of Law` = c(5.684202383,
5.684202383, 5.684202383, 5.684202383, 5.684202383, 5.684202383
), `Contract Enforcement` = c(4.2119071093, 4.2119071093, 4.2119071093,
4.2119071093, 4.2119071093, 4.2119071093), `Investor Protection` = c(4.5149315168,
4.5149315168, 4.6126326662, 4.6413682984, 4.6356211719, 4.6413682984
), `Property Rights` = c(8.7466324809, 9.0323467666, 8.9372039095,
8.9372039095, 8.9372039095, 8.9372039095), Val.Avg = c(5.44361315925556,
5.43272739205556, 5.31254161917778, 5.31582493942222, 5.439057355,
5.57961341006667), HDI = c(0.453, 0.463, 0.471, 0.482, 0.487,
0.491), LE = c(60.484, 61.028, 61.553, 62.054, 62.525, 62.966
), `Subject Descriptor` = c("General government total expenditure",
"General government total expenditure", "General government total expenditure",
"General government total expenditure", "General government total expenditure",
"General government total expenditure"), Units = c("Percent of GDP",
"Percent of GDP", "Percent of GDP", "Percent of GDP", "Percent of GDP",
"Percent of GDP"), Scale = c(NA, NA, NA, NA, NA, NA), `Country/Series-specific Notes` = c("See notes for: General government total expenditure (National currency).",
"See notes for: General government total expenditure (National currency).",
"See notes for: General government total expenditure (National currency).",
"See notes for: General government total expenditure (National currency).",
"See notes for: General government total expenditure (National currency).",
"See notes for: General government total expenditure (National currency)."
), `Estimates Start After` = c(2018, 2018, 2018, 2018, 2018,
2018), e_p_GDP = c("21.17", "20.800999999999998", "21.937000000000001",
"25.027999999999999", "24.977", "25.402000000000001")), row.names = c(NA,
6L), class = "data.frame")
【问题讨论】:
【参考方案1】:选择要绘制的数据子集
ggplot(subset(df, year==2019 & area_name=="Denmark"), aes(HDI,LE)) +
geom_point()
或者全部绘制:
ggplot(df, aes(HDI,LE)) +
geom_point() +
facet_grid(year~area_name)
【讨论】:
以上是关于如何从面板/纵向数据中选择一年来绘制图表?的主要内容,如果未能解决你的问题,请参考以下文章