首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggplot2:使用定向分段可视化两个时间段之间的变化

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

Stack Overflow用户
提问于 2017-01-27 06:14:35
回答 1查看 322关注 0票数 1

在他关于visualizing the same dataset in multiple ways的文章中,Nathan Yau使用定向片段来显示在不同时期之间变化更明显的地方。

有没有一种简单的方法可以使用ggplot2生成类似的图

这是original data in CVS format,以及原始的图:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-27 07:48:27

那这个呢?

代码语言:javascript
复制
#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)  

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41883943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档