在他关于visualizing the same dataset in multiple ways的文章中,Nathan Yau使用有针对性的片段来显示各个时期之间的变化更为明显。
是否有使用ggplot2
生成类似情节的简单方法?
这里是original data in CVS format和原始情节:
答案 0 :(得分:4)
这个怎么样?
#load packages
library(ggplot2)
library(dplyr)
library(tidyr)
#read data
df <- read.csv(url("https://www.dropbox.com/s/7mmpor5m8dek51a/df.csv?raw=1"))
df_1 <- df %>%
filter(Year%in%c("2000", "2015")) %>%
mutate(year = "year") %>%
tidyr::unite(year_fin, year, Year, sep = ".") %>%
select(Country, year_fin, life_expectancy_birth_both) %>%
tidyr::spread(year_fin, life_expectancy_birth_both)
#reorder data
df_2 <- transform(df_1, Country = reorder(Country, year.2015))
#plot
ggplot(df_2, aes(x=life_expectancy_birth_both, y=Country)) +
geom_segment(aes(x= year.2000 , xend= year.2015, y=Country, yend=Country), color = "red", size = 0.2,
arrow = arrow(length = unit(0.1, "cm")))+
scale_x_continuous(breaks=c(40,45,50, 55, 60, 65, 70, 75, 80, 85))+
geom_vline(xintercept = c(40,45,50, 55, 60, 65, 70, 75, 80, 85), linetype = 3, size =0.20)+
labs(title = "LIFE EXPECTANCY AT BIRTH" ,
subtitle = "2000 vs. 2015",
x= " ",
y= " ")+
theme(plot.title=element_text(size=10,
hjust= -1,
face="bold",
colour="black"),
plot.subtitle=element_text(size=8,
hjust=-0.52,
face="bold",
color="black"),
axis.text.y=element_text(size = 4),
axis.text.x=element_text(size = 4),
axis.ticks=element_blank(),
panel.background=element_blank())
ggsave("life_expectancy.png", height = 10, width = 5 , dpi = 600)