Bio*_*eek 5 r axes subtitle ggplot2
对于主y轴和x轴,我有通用标题,如"Tank's Ratio"和"Counts".我想要第二行标签,我指定比率和计数.例如.就在"Tank's Ratio"之下,我希望"#in water /#in sand"中的字体较小,但是沿着y轴.类似地,对于x轴.这是基本代码
data <- data.frame(set = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4), density = c(1, 3, 3, 1, 3, 1, 1, 1, 3, 3, 1, 3), counts = c(100, 2, 3, 76, 33, 12, 44, 13, 54, 36, 65, 1), ratio = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 90, 1))
library(ggplot2)
ggplot(data, aes(x = counts, y = ratio)) +
geom_point() +
ylab("Tank's Ratio") +
xlab("Counts")
Run Code Online (Sandbox Code Playgroud)
这不是最优雅的解决方案,但希望它有所帮助:
library(ggplot2)
library(gridExtra)
library(grid)
Run Code Online (Sandbox Code Playgroud)
首先,创建不带 ylab 的绘图:
g <- ggplot(data, aes(x = counts, y = ratio)) +
geom_point() +
ylab("") +
xlab("Counts")
Run Code Online (Sandbox Code Playgroud)
然后为两个轴添加副标题:
g2 <- grid.arrange(g,
bottom = textGrob("in water/ # in sand",
x = 0.55, y = 1, gp = gpar(fontsize = 9)),
left = textGrob("in water/ # in sand", rot = 90,
x = 1.5, gp = gpar(fontsize = 9)))
Run Code Online (Sandbox Code Playgroud)
最后,添加y轴的描述
grid.arrange(g2,
left = textGrob("Tank's Ratio", rot = 90,
x = 1.7, gp = gpar(fontsize = 12)))
Run Code Online (Sandbox Code Playgroud)