跟著Plos Biology學(xué)作圖:R語言ggplot2分組擬合曲線和95%置信區(qū)間

論文

image.png

今天的推文我們重復(fù)一下論文中的Figure1A

image.png

首先是讀取數(shù)據(jù)

library(readr)
data.use <- read_csv("use-resistance-seasonality/raw_data/antibiotic_use_data.csv")
head(data.use,n=10)
image.png

對(duì)數(shù)據(jù)進(jìn)行預(yù)處理

data.use %>% 
  mutate(year_month = 
           paste(as.character(year), 
                 as.character(str_pad(month, 2, pad = "0")), sep = "-")) %>%
  mutate(x = dense_rank(year_month)) -> f1a_data

這里學(xué)到了兩個(gè)新函數(shù)

  • str_pad 可以給字符串補(bǔ)長(zhǎng)度,比如
str_pad("ABC",5,pad="0","right")

就可以把ABC補(bǔ)充到5個(gè),結(jié)尾補(bǔ)充兩個(gè)0,這個(gè)很有用。比如原來的數(shù)字是1,2,3,4,5 改成01,02,03,04,05這種形式

  • dense_rank()是做排序的

準(zhǔn)備配色 和 添加文本標(biāo)簽的數(shù)據(jù)

labels <- data.frame(drug_class = c("Penicillins", "Macrolides", "Quinolones", "Tetracyclines", "Nitrofurans"),
                   x.pos = c(4, 4, 4, 4, 4),
                   y.pos = c(6.6, 3.3, 2.4, 1.5, 0.7))

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

作圖

f1a_data %>%
  ggplot(aes(x=x, y=mean_daily_claims_per_10000ppl, 
             group=drug_class, color=drug_class)) +
  geom_point(size = 0.7) +
  geom_smooth(aes(fill=drug_class), 
              span = 0.2, size = 0.7,
              method = "loess",
              level=0.95,
              formula = 'y~x') -> f1a1
print(f1a1)
image.png

下面是細(xì)節(jié)的美化

f1a1+
  geom_text(data = labels, aes(x=x.pos, y=y.pos, label=drug_class), hjust = 0, size = 4.2) +
  ylab("Mean daily claims/10,000 people") +
  scale_color_manual(values = colors) +
  scale_fill_manual(values = colors) +
  scale_x_continuous(breaks = c(1, 13, 25, 37, 49), labels = c("Jan '11", "Jan '12", "Jan '13", "Jan '14", "Jan '15")) +
  theme_minimal() +
  theme(panel.grid.minor = element_blank(),
        legend.position = "none",
        axis.text = element_text(size = 10),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size = 11)) 
image.png

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

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

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

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語言和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)容