哥俩好-ggplot2优雅的绘制配对条形图

欢迎关注R语言数据分析指南

本节来介绍如何使用ggplot2包绘制配对条形图,下面通过一个小案例进行展示

安装并加载R包

package.list=c("tidyverse","ggtext","ggnewscale","scico")

for (package in package.list) {
  if (!require(package,character.only=T, quietly=T)) {
    install.packages(package)
    library(package, character.only=T)
  }
}

加载数据

big_dave <- readr::read_csv('big_dave.csv')
times <- readr::read_csv('times.csv')

数据清洗

df1 <- big_dave %>%
  filter(!is.na(definition)) %>%
  mutate(definition = str_to_lower(definition)) %>% 
  count(definition, sort=T) %>% 
  mutate(pct=n/sum(n)) %>%
  mutate(g=1,grp="Big Dave's") %>% slice(1:20)

df2 <- times %>% 
  filter(!is.na(definition)) %>%
  mutate(definition = str_to_lower(definition)) %>% 
  count(definition, sort=T)  %>% 
  mutate(pct=n/sum(n)) %>%
  mutate(g=2,grp="Times") %>% slice(1:20)

数据合并

df = rbind(df1,df2) %>% group_by(grp) %>%
  mutate(rank=rank(desc(n),ties.method = 'first')) %>%
  ungroup()

筛选部分数据

selected = df %>% select(definition, g) %>% 
  count(definition) %>%filter(n==2) %>% pull(definition)

数据可视化

df %>% ggplot(aes(x=g, y=rank)) +
  geom_text(aes(label=definition, hjust=ifelse(g==1,1,0))) +
  geom_line(data=df %>% filter(definition %in% selected),aes(group=definition)) +
  geom_segment(data=df %>% filter(g==2),
               aes(x=g+.8, xend=g+.8+pct*600, y=rank, yend=rank, color=pct), size=5) +
  geom_segment(data=df %>% filter(g==1),
               aes(x=g-.8, xend=g-.8-pct*600, y=rank, yend=rank, color=pct), size=5) +
  scico::scale_color_scico(palette="acton", direction=-1) +
  ggnewscale::new_scale_color() +
  geom_text(data=df %>% 
              filter(g==2),aes(x=g+0.85,y=rank,color=I(ifelse(pct>0.0015,"white","black")), 
                               abel=scales::percent(pct, accuracy = .01)),size=3, hjust=0) +
  ggnewscale::new_scale_color() +
  geom_text(data=df %>% filter(g==1),
            aes(x=g-0.85, y=rank, color=I(ifelse(pct>0.0014,"white","black")), 
                label=scales::percent(pct, accuracy = .01)), size=3, hjust=1) +
  annotate(geom="text",y=-.3,x=0.68, label="Big Dave's",size=4.3, fontface="bold") +
  annotate(geom="text",y=-.3,x=2.2, label="Times",size=4.3, fontface="bold") +
  scale_y_reverse() +
  scale_x_continuous(limits=c(-1.5,4.5), expand=c(0,0)) +
  theme_void()+
  theme(legend.position = "none",plot.margin=margin(.1,.1,.1,.1,unit="cm"))

数据获取

本节内容到此结束,本节内容到此结束,喜欢的小伙伴欢迎分享转发,评论区留言交流可获取数据

你可能感兴趣的:(哥俩好-ggplot2优雅的绘制配对条形图)