你是对的,数据将从重组中受益。这是一个“广泛到长期”的问题即最好有3列:国家,年份和年龄。
您可以使用它使用dplyr
包和阴谋使用ggplot2
的tidyr
包,过程数据的格式。因此,假设您已经阅读了CSV到R和有一个名为lexp
数据帧,你可以尝试这样的事:
library(dplyr)
library(tidyr)
library(ggplot2)
lexp %>%
# reformat from wide to long
gather(Year, Age, -Country, convert = TRUE) %>%
# select most recent year
filter(Year == max(Year)) %>%
# sort by decreasing age
arrange(desc(Age)) %>%
# take the top 10 countries
slice(1:10) %>%
select(Country) %>%
# join back to the original data
inner_join(lexp) %>%
# reformat again from wide to long
gather(Year, Age, -Country, convert = TRUE) %>%
# and plot the graph
ggplot(aes(Year, Age)) + geom_line(aes(color = Country, group = Country)) +
theme_dark() + theme(axis.text.x = element_text(angle = 90)) +
labs(title = "Life Expectancy") +
scale_color_brewer(palette = "Set3")
结果:
你如何定义“前10名“?最近一年的最高平均值? – neilfws
'库(tidyverse); gsheet :: gsheet2tbl('https://docs.google.com/spreadsheets/d/1K5CKUaiUyhTy9YFjDCqLzmKgRf_DO2Ycy0Wbv95KwC4/edit?usp=sharing')%>%top_n(10,\'2011 \')%>%gather(Year,\ (预期寿命), - 国家,转换= TRUE)%>%ggplot(aes(Year,\'Life Expectancy \',color = Country))+ geom_line()' – alistaire
这是我的宠物,预期“是多余的。预期寿命是一个平均值。 (这是统计期望值。) –