遇到有人问,如何使用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)
最终效果
写在最后
- 使用
shape = 21
来指定geom_point()
点的属性,这样才能同时指定点的边框颜色合填充颜色。