1

In his article about visualizing the same dataset in multiple ways, Nathan Yau uses directed segments to show where change is more pronounced between periods.

Is there a straightforward way to generate a similar plot using ggplot2?

Here's the original data in CVS format, and the original plot:

life expectancy at birth

Community
  • 1
  • 1
HAVB
  • 1,858
  • 1
  • 22
  • 37

1 Answers1

5

What about this?

#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)  

enter image description here

shiny
  • 3,380
  • 9
  • 42
  • 79