可视化系列【五】:用ggplot2实现气泡图绘制

2023-08-25  本文已影响0人  Bio_Infor

遇到有人问,如何使用ggplot2绘制这样的气泡图:


于是找了个nature communications中的数据来尝试进行绘制,但具体是哪篇文章我可能已经无法溯源了,不过好在数据还保留着,需要数据的还是老规矩,评论区或者私信。

代码

library(dplyr)
library(magrittr)
library(forcats)
library(tidyr)
library(vroom)
library(ggplot2)

data <- vroom(file = 'data.csv',
              col_names = TRUE)
pattern = 'PERT-PSP_|PERT-P100-PRM_|PERT-P100-DIA_|PATH-NP_|_PATHWAY|KINASE-PSP_|DISEASE-PSP_'
data %<>% 
  mutate(type = ifelse(type == 'SPH', 'Spheroid', 'Monolayer'),
         type = fct_relevel(type, c('Spheroid', 'Monolayer')),
         variable = fct_relevel(variable, c('1h', '3h', '6h', '12h', '24h')),
         id = gsub(pattern = pattern, replacement = '', id))
head(data)
# A tibble: 6 × 5
#  id                variable  pvalue     FC type    
#  <chr>             <fct>      <dbl>  <dbl> <fct>   
#1 VIRUS_INFECTION   1h       NA      NA     Spheroid
#2 UV                1h        0.0451  3.22  Spheroid
#3 TNF               1h        0.910   0.331 Spheroid
#4 THROMBIN          1h        0.0169  3.83  Spheroid
#5 SII_ANGIOTENSIN_2 1h        0.749  -1.02  Spheroid
#6 LPA               1h       NA      NA     Spheroid
ggplot(data = data, aes(x = variable, y = id)) +
  geom_point(aes(size = -log10(pvalue), fill = FC), shape = 21, color = 'black', stroke = 1) +
  labs(x = '5-FU treatment time (h)', y = '') +
  scale_size_area(name = '-log10(q-value)',
                  breaks = seq(0, 2, 0.5),
                  limits = c(0, 2),
                  max_size = 5.5) +
  scale_fill_gradient2(low = '#08519c', 
                        mid = 'white', 
                        high = 'red',
                        limits = c(-5, 5),
                        name = 'PTMSEA\nScore',
                        na.value = '#08519c') +
  facet_wrap(.~type) +
  theme(strip.background = element_blank(),
        strip.text.x = element_text(family = 'sans', size = 15),
        panel.background = element_rect(fill = c('#deebf7', '#ffffcc')),
        panel.grid = element_blank(),
        panel.border = element_rect(fill = NA, color = 'gray', linewidth = 2),
        axis.title.x = element_text(family = 'sans', face = 'bold', size = 15),
        axis.text = element_text(family = 'sans', colour = 'black'),
        axis.ticks = element_line(linewidth = 1))
ggsave(filename = 'plot.jpeg', dpi = 3000, width = 6, height = 10)

最终效果

写在最后

上一篇下一篇

猜你喜欢

热点阅读