其他
让ggplot2变成Graphpad Prism样式:ggprims(05)
简介
ggprism
是ggplot2
的扩展包,可以让你的图形看起来像GraphPad Prism形式。使用起来非常简单,一行代码即可!支持目前Graphpad Prism的所有主题!
前面介绍了theme_prism
,color/fill/shape
,坐标轴修改,添加显著性P值,今天介绍两个示例!
安装
install.packages("ggprism")
# 或使用github版
remotes::install_github("csdaw/ggprism")
实战模仿
通过模仿GraphPad prism里面的2个例子,感受这个包的魅力!
real life data
第一个例子来自于GraphPad prism 7。
加载R包和数据:
library(dplyr)
library(ggplot2)
library(ggprism)
library(ggbeeswarm)
library(rstatix)
##
## 载入程辑包:'rstatix'
## The following object is masked from 'package:stats':
##
## filter
data("wings")
wings$measure <- wings$measure %>%
gsub("\\.", " ", .) %>%
tools::toTitleCase() %>%
factor(., levels = c("Wing Size", "Cell Size", "Cell Number"))
head(wings)
## # A tibble: 6 x 4
## sex genotype measure percent.change
## <fct> <fct> <fct> <dbl>
## 1 male Tps1MIC/+ Wing Size -2.45
## 2 male Tps1MIC/+ Cell Size 6.33
## 3 male Tps1MIC/+ Cell Number -8.41
## 4 male Tps1MIC/+ Wing Size -1.14
## 5 male Tps1MIC/+ Cell Size -2.53
## 6 male Tps1MIC/+ Cell Number 1.26
直接上图:
wings_pvals <- wings %>%
group_by(sex, measure) %>%
rstatix::t_test(
percent.change ~ genotype,
p.adjust.method = "BH",
var.equal = TRUE,
ref.group = "Tps1MIC/+"
) %>%
rstatix::add_x_position(x = "measure", dodge = 0.9) %>%
mutate(label = c("***", "*", "P = 0.26", "***", "***", "P = 0.65"))
ggplot(wings, aes(x = measure, y = percent.change)) +
ggbeeswarm::geom_beeswarm(aes(fill = genotype), dodge.width = 0.9, shape = 21, size = 2) +
facet_wrap(~ sex, scales = "free",
labeller = labeller(sex = c(male = "\u2642", female = "\u2640"))) +
geom_hline(yintercept = 0, linetype = 2, size = 0.3) +
stat_summary(geom = "crossbar", aes(fill = genotype),fun = mean,
position = position_dodge(0.9), colour = "red", size = 0.8, width = 0.7,
show.legend = FALSE) +
add_pvalue(wings_pvals, y = 10, xmin = "xmin", xmax = "xmax",
tip.length = 0, fontface = "italic",
lineend = "round", bracket.size = 1) +
theme_prism(base_fontface = "plain", base_line_size = 1.2,
base_family = "Arial") +
scale_x_discrete(guide = guide_prism_bracket(width = 0.15),
labels = scales::wrap_format(5)) +
scale_y_continuous(limits = c(-20, 12), expand = c(0, 0),
breaks = seq(-20, 10, 5), guide = "prism_offset") +
labs(y = "% change") +
scale_fill_manual(values = c("#026FEE", "#87FFFF"),
labels = c(expression("Tps"*1^italic("MIC")~"/ +"),
expression("Tps"*1^italic("MIC")))) +
theme(legend.position = "bottom",
axis.title.x = element_blank(),
strip.text = element_text(size = 14),
legend.spacing.x = unit(0, "pt"),
legend.text = element_text(margin = margin(r = 20))) +
geom_text(data = data.frame(sex = factor("female", levels = c("male", "female")),
measure = "Cell Number",
percent.change = -18.5,
lab = "(n = 10)"),
aes(label = lab)) +
guides(fill = guide_legend(override.aes = list(size = 3)))
第2个例子
剂量反应曲线
df <- data.frame(
agonist = c(1e-10, 1e-8, 3e-8, 1e-7, 3e-7, 1e-6, 3e-6, 1e-5, 3e-5, 1e-4, 3e-4),
ctr1 = c(0, 11, 125, 190, 258, 322, 354, 348, NA, 412, NA),
ctr2 = c(3, 33, 141, 218, 289, 353, 359, 298, NA, 378, NA),
ctr3 = c(2, 25, 160, 196, 345, 328, 369, 372, NA, 399, NA),
trt1 = c(3, NA, 11, 52, 80, 171, 289, 272, 359, 352, 389),
trt2 = c(5, NA, 25, 55, 77, 195, 230, 333, 306, 320, 338),
trt3 = c(4, NA, 28, 61, 44, 246, 243, 310, 297, 365, NA)
) %>%
mutate(log.agonist = log10(agonist)) %>%
pivot_longer(
c(-agonist, -log.agonist),
names_pattern = "(.{3})([0-9])",
names_to = c("treatment", "rep"),
values_to = "response"
) %>%
filter(!is.na(response))
head(df)
## # A tibble: 6 x 5
## agonist log.agonist treatment rep response
## <dbl> <dbl> <chr> <chr> <dbl>
## 1 0.0000000001 -10 ctr 1 0
## 2 0.0000000001 -10 ctr 2 3
## 3 0.0000000001 -10 ctr 3 2
## 4 0.0000000001 -10 trt 1 3
## 5 0.0000000001 -10 trt 2 5
## 6 0.0000000001 -10 trt 3 4
dose_resp <- y ~ min + ((max - min) / (1 + exp(hill_coefficient * (ec50 - x))))
ggplot(df, aes(x = log.agonist, y = response)) +
geom_smooth(
aes(colour = treatment),
method = "nls", formula = dose_resp, se = FALSE,
method.args = list(start = list(min = 1.67, max = 397, ec50 = -7, hill_coefficient = 1))
) +
scale_colour_manual(labels = c("No inhibitor", "Inhibitor"),
values = c("#00167B", "#9FA3FE")) +
ggnewscale::new_scale_colour() +
geom_point(aes(colour = treatment, shape = treatment), size = 3) +
scale_colour_prism(palette = "winter_bright",
labels = c("No inhibitor", "Inhibitor")) +
scale_shape_prism(labels = c("No inhibitor", "Inhibitor")) +
theme_prism(palette = "winter_bright", base_size = 16) +
scale_y_continuous(limits = c(-100, 500),
breaks = seq(-100, 500, 100),
guide = "prism_offset") +
scale_x_continuous(
limits = c(-10, -3),
breaks = -10:-3,
guide = "prism_offset_minor",
minor_breaks = log10(rep(1:9, 7)*(10^rep(-10:-4, each = 9))),
labels = function(lab) {
do.call(
expression,
lapply(paste(lab), function(x) bquote(bold("10"^.(x))))
)
}
) +
theme(axis.title.y = element_blank(),
legend.title = element_blank(),
legend.position = c(0.05, 0.95),
legend.justification = c(0.05, 0.95)) +
labs(x = "[Agonist], M")
以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发!
欢迎在评论区留言或直接添加我的微信!
欢迎关注我的公众号:医学和生信笔记
“医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!
往期精彩内容:
使用tinyarray包简化你的GEO分析流程!
使用tinyarray简化你的TCGA分析流程!
R语言和医学统计学系列(11):球形检验
超详细的R语言热图之complexheatmap系列1