[英]Multi Line Graph in R
I have a .csv file of the average life expectancy by country for the last 50 years. 我有一个.csv文件,其中提供了过去50年中每个国家/地区的平均预期寿命。 I am trying to create a graph of life expectancy by country, with the years 1960-2011 on the x axis, and the average life expectancy on the y axis.
我正在尝试创建按国家/地区划分的预期寿命图表,x轴为1960-2011年,y轴为平均寿命。 I only want to plot the top ten countries, each with their own line.
我只想绘制前十个国家/地区,每个国家都有自己的路线。 I have researched every possible way to plot a multi line graph of the data I have and it seems to me that it is impossible with the way the data is formatted.
我研究了各种可能的方法来绘制我的数据的多线图,在我看来,用数据格式化的方式是不可能的。 My questions are:
我的问题是:
I was able to create the desired graph in Excel 我能够在Excel中创建所需的图形
which is exactly what I'd like to do in R.
这正是我想在R中做的。
Here is a link to the lexp.csv file. 这是lexp.csv文件的链接。 https://drive.google.com/file/d/0BwsBIUlCf0Z3QVgtVGt4ampVcmM/view?usp=sharing
https://drive.google.com/file/d/0BwsBIUlCf0Z3QVgtVGt4ampVcmM/view?usp=sharing
You are correct that the data would benefit from reorganization. 您是正确的,数据将从重组中受益。 This is a "wide to long" problem ie it would be better to have 3 columns: Country, Year and Age.
这是一个“从宽到长”的问题, 即最好有3列:“国家/地区”,“年份”和“年龄”。
You can reformat the data using the tidyr
package, process it using the dplyr
package and plot using ggplot2
. 您可以使用
tidyr
软件包重新格式化数据,使用tidyr
软件包处理数据,并使用dplyr
进行ggplot2
。 So, assuming that you have read the CSV into R and have a data frame named lexp
, you could try something like this: 因此,假设您已将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")
library("reshape2")
library("ggplot2")
test_data_long <- melt(df, id="Country") # convert to long format
testdata<-test_data_long[complete.cases(test_data_long),]
ggplot(data=testdata,
aes(x=variable, y=value)) +
geom_line(aes(color = Country, group = Country))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.