跟着Plos Biology学作图:R语言ggplot2散点图和误差线

论文

Large variation in the association between seasonal antibiotic use and resistance across multiple bacterial species and antibiotic classes

数据代码链接

https://github.com/orgs/gradlab/repositories

今天的推文重复一下论文中的 Figure 3A

image.png

今天的推文主要有两个知识点

  • 一个是坐标轴文字同时存在斜体字和正常字应该怎么处理(上图的横坐标)
  • 二是坐标轴标题如果要换行应该怎么处理(上图的纵坐标)

准备作图数据的代码

library(readr)
library(tidyverse)

res.modelA.params <- read_csv("use-resistance-seasonality/tables/resistance_modelA_values.csv")
filter_models_AIC_func = function(table, group_cols) {
  
  table.fil = table %>%
    group_by_at(vars(all_of(group_cols))) %>%
    mutate(rank = dense_rank(AIC)) %>%
    ungroup() %>%
    filter(rank == 1) %>%
    select(-AIC, -rank)
  
  return(table.fil)
}
res.modelA.params.fil <- filter_models_AIC_func(res.modelA.params, c("organism", "drug_name"))
f3a_data <- res.modelA.params.fil %>%
  filter(term == "amplitude") %>%
  mutate(org_drug = paste(organism, drug_code, sep = "/")) %>%
  select(org_drug, drug_class, period, term, estimate, ci.lower, ci.upper)

准备作图配色

colors <- setNames(c("#220050", "#b30059","#0091a8","#359023", "#ffa500"),
                  c("Macrolides", "Nitrofurans", "Penicillins", "Quinolones", "Tetracyclines"))

定义横坐标文字标签

这里设置斜体使用expression()函数

labs <- c("S. aureus/CIP" = expression(paste(italic("S. aureus"), " / CIP")),
         "S. aureus/ERY" = expression(paste(italic("S. aureus"), " / ERY")),
         "S. aureus/NIT" = expression(paste(italic("S. aureus"), " / NIT")),
         "S. aureus/OXA" = expression(paste(italic("S. aureus"), " / OXA")),
         "S. aureus/PEN" = expression(paste(italic("S. aureus"), " / PEN")),
         "S. aureus/TET" = expression(paste(italic("S. aureus"), " / TET")),
         "E. coli/AMC" = expression(paste(italic("E. coli"), " / AMC")),
         "E. coli/AMP" = expression(paste(italic("E. coli"), " / AMP")),
         "E. coli/CIP" = expression(paste(italic("E. coli"), " / CIP")),
         "E. coli/NIT" = expression(paste(italic("E. coli"), " / NIT")),
         "E. coli/TET" = expression(paste(italic("E. coli"), " / TET")),
         "K. pneumoniae/AMC" = expression(paste(italic("K. pneumoniae"), " / AMC")),
         "K. pneumoniae/CIP" = expression(paste(italic("K. pneumoniae"), " / CIP")),
         "K. pneumoniae/NIT" = expression(paste(italic("K. pneumoniae"), " / NIT")),
         "K. pneumoniae/TET" = expression(paste(italic("K. pneumoniae"), " / TET")))

画图代码

f3a <- f3a_data %>%
  ggplot(aes(x = reorder(org_drug, estimate), y = estimate, color = drug_class)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = ci.lower, ymax = ci.upper), width = 0.4) +
  geom_hline(yintercept = 0, color = "grey20", linetype = "dashed") +
  scale_y_continuous(breaks = c(0, 0.05, 0.1), limits = c(-0.02, 0.1)) +
  scale_x_discrete(labels = labs) +
  scale_color_manual(values = colors, name = "Antibiotic Class") +
  ylab(expression(atop("Amplitude of seasonality", "(" ~ log["2"] ~ "(MIC) deviates)"))) +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_text(size = 10),
    axis.text.x = element_text(size = 10, angle = 45, hjust = 1, vjust = 1),
    axis.text.y = element_text(size = 10),
    legend.title = element_text(size = 9, face = "bold"),
    legend.text = element_text(size = 9),
  )

f3a
image.png

纵坐标轴标题换行的代码是ylab()这一行 expression(atop()),如果要换行就把不同的字符串用逗号分开,可以试下如下代码的效果

ggplot()+
  geom_point(aes(x=1,y=1))+
  ylab(expression(atop("ABCDE"," abcde" ~ "(" ~ ")")))

今天推文的示例数据和代码可以在公众号小明的数据分析笔记本后台留言20220328获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

你可能感兴趣的:(跟着Plos Biology学作图:R语言ggplot2散点图和误差线)