简体   繁体   中英

How can I select a single year from panel/longitudinal data to graph?

I have the following data set for 167 countries, and the years 2009-2017. The head(df) looks as follows:

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

I was wondering how I can plot data only for a given year or country? Eg a scatter plot of some variable (eg HDI) for one country (eg Afghanistan) between 2009 and 2017 or a plot of some variable for all countries in a single year (eg 2017).

Here is the 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")

Pick the subset of data to plot by

ggplot(subset(df, year==2019 & area_name=="Denmark"), aes(HDI,LE)) + 
geom_point()

Or plot it all:

ggplot(df, aes(HDI,LE)) + 
geom_point() +
facet_grid(year~area_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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