美文网首页HelloWorld
R绘图 | 对比条形图+连线

R绘图 | 对比条形图+连线

作者: 木舟笔记 | 来源:发表于2022-04-29 11:34 被阅读0次
week2.jpg

整个新系列。目前的几个系列, #R实战生信分析为主, #跟着CNS学作图复现顶刊Figure为主,而本系列 #R绘图 则是学习不在文章中但同样很好看的图,致力于给同学们在数据可视化中提供新的思路和方法。

22

本期图片

2022_16.png

示例数据和代码领取

详见:R绘图 | 对比条形图+连线

绘制

## 本图可用于两组的基因、蛋白、物种丰度等的对比 或随时间的变化发展
rm(list = ls())
library(tidyverse)
library(ggtext)
library(showtext)
library(ggplot2)
showtext_opts(dpi = 300)
showtext_auto(enable = TRUE)

# Font
font_add_google("Roboto Mono")
f1 = "Roboto Mono"
df = read.csv("df_plot.csv")
# 两组相同的部分(即先筛选出出现频率为2的部分)
selected = df %>% select(definition, g) %>% 
  count(definition) %>%
  filter(n==2) %>%
  pull(definition)

# Plot
# 注意scico,cowplot,ggnewscaled包是否安装
df %>%
  ggplot(aes(x=g, y=rank)) +
  geom_text(aes(label=definition, hjust=ifelse(g==1,1,0)), family=f1) +
  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="bamako", 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")), 
                label=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)) +
  cowplot::theme_map(13) +
  theme(legend.position = "none",
        plot.margin=margin(.5,.5,.3,.5, unit="cm"),
        plot.title=element_text(size=15, hjust=.5),
        plot.subtitle = element_text(hjust=.5),
        plot.caption=element_text(size=8.5)) +
  labs(caption="#MZBJ week 2  |  Data from Cryptics.georgeho.org",
       title="20 most common crossword puzzle definitions",
       subtitle="from Big Dave's (2009-02-27 to 2022-04-15) and Times (2012-12-27 to 2021-09-12)")

ggsave("2022_16.png", height=6, width=8, bg="#fafafa")

参考

往期内容

  1. (免费教程+代码领取)|跟着Cell学作图系列合集
  2. Q&A | 如何在论文中画出漂亮的插图?
  3. Front Immunol 复现 | 1. GEO数据下载及sva批次校正(PCA可视化)
  4. R绘图 | 气泡散点图+拟合曲线

相关文章

网友评论

    本文标题:R绘图 | 对比条形图+连线

    本文链接:https://www.haomeiwen.com/subject/tqksyrtx.html