R繪圖 | 對比條形圖+連線

week2.jpg

整個新系列。目前的幾個系列, #R實戰(zhàn)生信分析為主, #跟著CNS學作圖復現(xiàn)頂刊Figure為主,而本系列 #R繪圖 則是學習不在文章中但同樣很好看的圖,致力于給同學們在數(shù)據(jù)可視化中提供新的思路和方法。

22

本期圖片

2022_16.png

示例數(shù)據(jù)和代碼領(lǐng)取

詳見:R繪圖 | 對比條形圖+連線

繪制

## 本圖可用于兩組的基因、蛋白、物種豐度等的對比 或隨時間的變化發(fā)展
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")
# 兩組相同的部分(即先篩選出出現(xiàn)頻率為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")

參考

往期內(nèi)容

  1. (免費教程+代碼領(lǐng)取)|跟著Cell學作圖系列合集
  2. Q&A | 如何在論文中畫出漂亮的插圖?
  3. Front Immunol 復現(xiàn) | 1. GEO數(shù)據(jù)下載及sva批次校正(PCA可視化)
  4. R繪圖 | 氣泡散點圖+擬合曲線

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容