ggplot2:使用定向段来可视化两个时间段之间的变化

HAV*_*AVB 1 r ggplot2

在他关于以多种方式可视化同一数据集的文章,Nathan Yau 使用定向段来显示不同时期之间变化更明显的地方。

有没有一种直接的方法可以使用ggplot2?

这是CVS 格式原始数据和原始图:

出生时的预期寿命

shi*_*iny 5

那这个呢?

#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)  
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明