Drawing with Plos Biology: R language ggplot2 double Y-axis line chart, confidence interval, error bar

paper

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

Data code link

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

Today's tweet repeats Figure S3 in the paper, a line chart with dual Y axes

9d4c814aa49f71f70a9e0f11cc568caa.png
image.png

After running the code provided by the paper, the mapping data set is obtained

  • regressionsd3fb84b9dcd9f45e478a8ba2d3570184.png

  • cifde95012867f0462d3d8e652dd575941.png

  • dat01$deviates_table[[1]]

85cfff833b96132b53740ec61da5f97b.png

Save these three datasets as csv files

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")

The first step in mapping is to read the data set

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

mapping code

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)

Add titles to both axes

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))
edeefbd4d97322b9297f88efd543aaa3.png
image.png

The sample data and code of today's tweet can be obtained by leaving a message in the background of the official account 20220413

Welcome everyone to pay attention to my public number

Xiao Ming's data analysis notebook

Xiaoming’s data analysis notebook public account mainly shares: 1. Simple examples of R language and python for data analysis and data visualization; 2. Reading notes on horticultural plants related transcriptomics, genomics, and population genetics literature; 3. Bioinformatics Learn introductory study materials and your own study notes!

Guess you like

Origin blog.csdn.net/weixin_45822007/article/details/124223864