跟著Plos Biology學(xué)作圖:R語(yǔ)言ggplot2雙Y軸折線圖

論文

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

數(shù)據(jù)代碼鏈接

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

今天的推文重復(fù)一下論文中的 Figure S3,雙Y軸的折線圖

image.png

經(jīng)過論文提供的代碼運(yùn)行,得到作圖數(shù)據(jù)集

  • regressions


    image.png
  • ci


    image.png
  • dat01$deviates_table[[1]]

將這三個(gè)數(shù)據(jù)集保存為csv文件

library(tidyverse)
library(readr)

regressions %>% 
  write_csv(file = "regressions.csv")
ci %>% 
  write_csv(file = "ci.csv")
dat01$deviates_table[[1]] %>% 
  write_csv(file = "deviates_table.csv")

作圖第一步讀取數(shù)據(jù)集

regressions<-read_csv("regressions.csv")
head(regressions)
ci<-read_csv("ci.csv")
head(ci)
deviates_table<-read_csv("deviates_table.csv")
head(deviates_table)

作圖代碼

library(ggplot2)
col<-"#359023"
title<-"Ampicillin *"
ratio<-27.79891
ggplot()+
  geom_point(data=deviates_table,
             aes(x=month,y=seasonal_deviate))+
  geom_errorbar(data = deviates_table, 
                aes(x = month, 
                    ymin = seasonal_deviate - sem, 
                    ymax = seasonal_deviate + sem), 
                width = 0.5, 
                color = col)+
  geom_line(data=regressions,
            aes(x = month, y = value, 
                color = leg, linetype = leg), 
            size = 0.7) +
  geom_ribbon(data = ci, 
              aes(x = month, ymin = r_lower, 
                  ymax = r_upper), 
              fill = col, 
              alpha = 0.3) +
  geom_ribbon(data = ci, 
              aes(x = month, 
                  ymin = u_upper/ratio, 
                  ymax = u_lower/ratio), 
              fill = "grey20", alpha = 0.3) +
  scale_color_manual(values = c(col, "grey20")) +
  scale_y_continuous(sec.axis = sec_axis(~. * ratio), 
                     limits = c(-.165, .165)) +
  scale_x_continuous(breaks=c(1, 3, 5, 7, 9, 11)) +
  ggtitle(title) +
  xlab("Month") +
  theme_classic() +
  guides(color = guide_legend(nrow = 2, byrow = TRUE)) +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        legend.text = element_text(size = 9),
        plot.title = element_text(size = 11, hjust = 0.5, face = "bold"),
        axis.text = element_text(size = 10),
        axis.title.y = element_blank()
  ) -> f3splot

print(f3splot)

添加兩個(gè)坐標(biāo)軸的標(biāo)題

library(ggpubr)
f3splot %>% 
  annotate_figure(left = text_grob(expression("Seasonal deviates in resistance ("*log["2"]*"(MIC))"), size = 10, rot = 90)) %>%
  annotate_figure(right = text_grob("Seasonal deviates in use\n(mean daily claims/10,000 people)", size = 10, rot = 270))
image.png

今天推文的示例數(shù)據(jù)和代碼可以在公眾號(hào)后臺(tái)留言20220413獲取

歡迎大家關(guān)注我的公眾號(hào)

小明的數(shù)據(jù)分析筆記本

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!

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

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

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