其他
让ggplot2变成Graphpad Prism样式:ggprism(03)
简介
ggprism
是ggplot2
的扩展包,可以让你的图形看起来像GraphPad Prism形式。使用起来非常简单,一行代码即可!支持目前Graphpad Prism的所有主题!
前面介绍了theme_prism
,和color/fill/shape
,接下来介绍坐标轴修改。
安装
install.packages("ggprism")
# 或使用github版
remotes::install_github("csdaw/ggprism")
修改坐标轴
library(ggplot2)
library(ggprism)
library(patchwork)
刻度线
只支持连续性变量,不支持离散型变量。
p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) +
geom_boxplot(aes(fill = factor(supp))) +
theme_prism() +
theme(legend.position = "none")
# 两种方法都可以
p1 <- p + scale_y_continuous(guide = guide_prism_minor())
p2 <- p + guides(y = guide_prism_minor())
p1 + p2
使用默认样式:
p <- ggplot(ToothGrowth, aes(factor(supp), len)) +
geom_boxplot(aes(fill = factor(supp))) +
theme_prism() +
theme(legend.position = "none")
p1 <- p + scale_y_continuous(guide = "prism_minor")
p2 <- p + guides(y = "prism_minor")
p1 + p2
调整小刻度线的数量:
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
stat_summary(aes(fill = factor(dose)), na.rm = TRUE,
geom = "col", fun = mean, colour = "black", size = 0.9) +
theme_prism() +
theme(legend.position = "none")
p1 <- p + scale_y_continuous(guide = "prism_minor",
limits = c(0, 30),
expand = c(0, 0))
p2 <- p + scale_y_continuous(guide = "prism_minor",
limits = c(0, 30),
expand = c(0, 0),
minor_breaks = seq(0, 30, 2))
p1 + p2
这样一幅图是不是就很有GraphPad Prism的味道了?
调整小刻度线的长短:
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
stat_summary(aes(fill = factor(dose)), na.rm = TRUE,
geom = "col", fun = mean, colour = "black", size = 0.9) +
theme_prism() +
scale_y_continuous(guide = "prism_minor",
limits = c(0, 30),
expand = c(0, 0),
minor_breaks = seq(0, 30, 2))
p1 <- p + theme(legend.position = "none")
p2 <- p + theme(legend.position = "none",
prism.ticks.length.y = unit(20, "pt"))
p1 + p2
改变刻度线的方向:
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
stat_summary(aes(fill = factor(dose)), na.rm = TRUE,
geom = "col", fun = mean, colour = "black", size = 0.9) +
theme_prism() +
scale_y_continuous(guide = "prism_minor",
limits = c(0, 30),
expand = c(0, 0),
minor_breaks = seq(0, 30, 2))
p1 <- p + theme(legend.position = "none",
prism.ticks.length.y = unit(20, "pt"))
p2 <- p + theme(legend.position = "none",
prism.ticks.length.y = unit(-20, "pt"))
p1 + p2
改变刻度线的颜色:
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
stat_summary(aes(fill = factor(dose)), na.rm = TRUE,
geom = "col", fun = mean, colour = "black", size = 0.9) +
theme_prism() +
scale_y_continuous(guide = "prism_minor",
limits = c(0, 30),
expand = c(0, 0),
minor_breaks = seq(0, 30, 2))
p1 <- p + theme(legend.position = "none")
p2 <- p + theme(legend.position = "none",
axis.ticks.y = element_line(colour = "blue",
size = 2,
lineend = "round"))
p1 + p2
分离式的坐标轴
横纵坐标是分开的,这种坐标轴还是很火热的,在很多SCI文章中都能看到,base r 是可以的,但是语法不够友好。可以使用prism_offset
实现。
p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) +
geom_boxplot(aes(fill = factor(supp))) +
theme_prism() +
theme(legend.position = "none")
p1 <- p + scale_y_continuous(guide = "prism_offset")
p2 <- p + scale_y_continuous(limits = c(0,40), guide = "prism_offset")
p1 + p2
使用theme
更改颜色:
p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) +
geom_boxplot(aes(fill = factor(supp))) +
theme_prism() +
scale_y_continuous(limits = c(0, 40), guide = "prism_offset")
p1 <- p + theme(legend.position = "none")
p2 <- p + theme(legend.position = "none",
axis.line.y = element_line(colour = "blue",
size = 2,
lineend = "round"))
p1 + p2
添加小刻度线:prism_offset_minor
p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) +
geom_boxplot(aes(fill = factor(supp))) +
theme_prism() +
theme(legend.position = "none")
p1 <- p + scale_y_continuous(limits = c(0, 40),
guide = "prism_offset_minor")
p2 <- p + scale_y_continuous(limits = c(0, 40),
minor_breaks = seq(0, 40, 2),
guide = "prism_offset_minor")
p1 + p2
当然也可以改变颜色和方向:
p <- ggplot(ToothGrowth, aes(x = factor(supp), y = len)) +
geom_boxplot(aes(fill = factor(supp))) +
theme_prism() +
scale_y_continuous(limits = c(0, 40),
minor_breaks = seq(0, 40, 2),
guide = "prism_offset_minor")
p1 <- p + theme(legend.position = "none",
prism.ticks.length.y = unit(20, "pt"))
p2 <- p + theme(legend.position = "none",
prism.ticks.length.y = unit(-20, "pt"),
axis.ticks.y = element_line(colour = "blue",
size = 2,
lineend = "round"))
p1 + p2
离断式的坐标轴
brackets axis:通过prism_bracket
实现
# 横坐标
p1 <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) +
scale_shape_prism() +
theme_prism() +
theme(legend.position = "none") +
scale_y_continuous(limits = c(0, 40), guide = "prism_offset")
p2 <- p1 + scale_x_discrete(guide = "prism_bracket")
p1 + p2
# 纵坐标,其实是翻转效果
p1 <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) +
scale_shape_prism() +
theme_prism() +
theme(legend.position = "none") +
scale_y_continuous(limits = c(0, 40), guide = "prism_offset") +
scale_x_discrete(guide = "prism_bracket")
p2 <- p1 + coord_flip()
p1 + p2
控制横线的宽度:width
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) +
scale_shape_prism() +
theme_prism() +
theme(legend.position = "none") +
scale_y_continuous(limits = c(0, 40), guide = "prism_offset")
p1 <- p + scale_x_discrete(guide = "prism_bracket")
p2 <- p + scale_x_discrete(guide = guide_prism_bracket(width = 0.2))
p1 + p2
控制方向:outside = T
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) +
scale_shape_prism() +
theme_prism() +
theme(legend.position = "none") +
scale_y_continuous(limits = c(0, 40), guide = "prism_offset")
p1 <- p + scale_x_discrete(guide = "prism_bracket")
p2 <- p + scale_x_discrete(guide = guide_prism_bracket(outside = FALSE))
p1 + p2
改编坐标轴标签和横线之间的距离:
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) +
scale_shape_prism() +
theme_prism() +
scale_y_continuous(limits = c(0, 40), guide = "prism_offset") +
scale_x_discrete(guide = guide_prism_bracket(outside = FALSE))
p1 <- p + theme(legend.position = "none")
p2 <- p + theme(legend.position = "none",
axis.text.x = element_text(margin = margin(t = 10))) # 改变距离
p1 + p2
绘制带有边框和刻度线的图形:
# define a base plot
base <- ggplot(mpg, aes(x = displ, y = cty)) +
geom_point(aes(colour = class))
p1 <- base + theme_prism(border = TRUE) +
theme(legend.position = c(0.8, 0.75)) +
coord_cartesian(clip = "off")
p2 <- p1 + guides(x = "prism_minor", y = "prism_minor")
p_annot <- p2 + annotation_ticks(sides = "tr", type = "both", size = 1,
outside = TRUE,
tick.length = unit(14/2, "pt"),
minor.length = unit(14/4, "pt"))
(base + p1) / (p2 + p_annot)
以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发!
欢迎在评论区留言或直接添加我的微信!
欢迎关注我的公众号:医学和生信笔记
“医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!
往期精彩内容:
使用ggplot2画一个五颜六色条形图
让你的ggplot2主题支持markdown和css
让你的ggplot2支持markdown语法
让OneNote支持Markdown:oneMark,重新定义OneNote