欢迎关注微信公众号(医学生物信息学),医学生的生信笔记,记录学习过程。
添加文字
library(ggplot2)
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_point()
p +
annotate("text", x = 3, y = 48, label = "Group 1") +
annotate("text", x = 4.5, y = 66, label = "Group 2")
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_point()
p +
annotate("text", x = 3, y = 48, label = "Group 1",
family = "serif", fontface = "italic", colour = "darkred", size = 3) +
annotate("text", x = 4.5, y = 66, label = "Group 2",
family = "serif", fontface = "italic", colour = "darkred", size = 3)
要添加单独的文本对象时,请注意不要使用geom_text()
。可以使用annotate(geom=“text”)
来向绘图中添加单个文本对象,而geom_text()
将根据数据创建多个文本对象。
library(ggplot2)
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_point()
p +
annotate("text", x = 3, y = 48, label = "Group 1", alpha = .1) +
geom_text(x = 4.5, y = 66, label = "Group 2", alpha = .1)
可以看到Group 2的透明度不是10%。
如果坐标轴是连续的,可以使用特殊值Inf
和-Inf
将文本注释放在绘图区域的边缘。还可以使用hjust
和vjust
来调整文本相对于角落的位置——如果将hjust
和vjust
设置为默认值,则文本将以边缘为中心。
library(ggplot2)
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_point()
p +
annotate("text", x = -Inf, y = Inf, label = "Upper left", hjust = -.2, vjust = 2) +
annotate("text", x = mean(range(faithful$eruptions)), y = -Inf, vjust = -0.4,
label = "Bottom middle")
添加数学公式
通过在annotate(geom = "text")
中设置parse = TRUE
来生成数学表达式的格式。
library(ggplot2)
p <- ggplot(data.frame(x = c(-3,3)), aes(x = x)) +
stat_function(fun = dnorm)
p +
annotate("text", x = 2, y = 0.3, parse = TRUE,
label = "frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
若要将正则文本与表达式混合使用,请在双引号中使用单引号来标记纯文本部分。每个由内引号括起来的文本块都被视为数学表达式中的一个变量。在R的数学表达式语法中,不能简单地将一个变量放在另一个变量的旁边,而中间没有其他东西。要显示相邻的两个变量,请在它们之间放置一个*
运算符。在图形中显示时,它被视为不可见的乘法符号(对于可见的乘法符号,请使用%*%
):
library(ggplot2)
p <- ggplot(data.frame(x = c(-3,3)), aes(x = x)) +
stat_function(fun = dnorm)
p +
annotate("text", x = 0, y = 0.05, parse = TRUE, size = 4,
label = "'Function: ' * y==frac(1, sqrt(2*pi)) * e^{-x^2/2}")
添加参考线
对于添加水平线和垂直线,可以使用geom_hline()
和geom_vline()
,对于倾斜线,可以使用geom_abline()
。
library(ggplot2)
library(gcookbook)
hw_plot <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
geom_point()
hw_plot +
geom_hline(yintercept = 60) +
geom_vline(xintercept = 14)
hw_plot +
geom_abline(intercept = 37.4, slope = 1.75)
library(dplyr)
library(ggplot2)
library(gcookbook)
hw_plot <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
geom_point()
hw_means <- heightweight %>%
group_by(sex) %>%
summarise(heightIn = mean(heightIn))
hw_plot +
geom_hline(
data = hw_means,
aes(yintercept = heightIn, colour = sex),
linetype = "dashed",
size = 1
)
如果其中一个轴是离散的而不是连续的,则不能将截距仅指定为字符串——它们仍然必须指定为数字。
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_point()
pg_plot +
geom_vline(xintercept = 2)
如果轴为因子,则第一个级别的数值为1,第二个级别的值为2,依此类推。可以手动指定数值截距,也可以使用which(levels(...))
计算数值。
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_point()
pg_plot +
geom_vline(xintercept = which(levels(PlantGrowth$group) == "ctrl"))
添加线段和箭头
library(ggplot2)
library(gcookbook)
p <- ggplot(filter(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) +
geom_line()
p +
annotate("segment", x = 1950, xend = 1980, y = -.25, yend = -.25)
library(ggplot2)
library(gcookbook)
p <- ggplot(filter(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) +
geom_line()
# 默认角度为30,箭头线的默认长度为0.2英寸
library(grid)
p +
annotate("segment", x = 1850, xend = 1820, y = -.8, yend = -.95,
colour = "blue", size = 2, arrow = arrow())
library(ggplot2)
library(gcookbook)
p <- ggplot(filter(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) +
geom_line()
library(grid)
p +
annotate("segment", x = 1850, xend = 1820, y = -.8, yend = -.95,
colour = "blue", size = 2, arrow = arrow()) +
annotate("segment", x = 1950, xend = 1980, y = -.25, yend = -.25,
arrow = arrow(ends = "both", angle = 90, length = unit(.2,"cm")))
添加阴影矩形
library(ggplot2)
library(gcookbook)
p <- ggplot(filter(climate, Source == "Berkeley"), aes(x = Year, y = Anomaly10y)) +
geom_line()
p +
annotate("rect", xmin = 1950, xmax = 1980, ymin = -1, ymax = 1,
alpha = .1,fill = "blue")
高亮显示图中的一部分
pg_mod <- PlantGrowth %>%
mutate(hl = recode(group, "ctrl" = "no", "trt1" = "no", "trt2" = "yes"))
ggplot(pg_mod, aes(x = group, y = weight, fill = hl)) +
geom_boxplot() +
scale_fill_manual(values = c("grey85", "#FFDDCC"), guide = "none")
ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
geom_boxplot() +
scale_fill_manual(values = c("red", "blue", "#FFDDCC"), guide = "none")
添加误差线
library(gcookbook)
library(dplyr)
ce_mod <- cabbage_exp %>%
filter(Cultivar == "c39")
se
:the standard error of the mean
sd
:the standard deviation
library(ggplot2)
# width = .2 用来设置误差线末端的宽度
ggplot(ce_mod, aes(x = Date, y = Weight)) +
geom_col(fill = "white", colour = "black") +
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se), width = .2)
ggplot(ce_mod, aes(x = Date, y = Weight)) +
geom_line(aes(group = 1)) +
geom_point(size = 4) +
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se), width = .2)
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_col(position = "dodge") +
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se),
position = position_dodge(0.9), width = .2)
pd <- position_dodge(.3)
ggplot(cabbage_exp, aes(x = Date, y = Weight, colour = Cultivar, group = Cultivar)) +
geom_errorbar(
aes(ymin = Weight - se, ymax = Weight + se),
width = .2,
size = 0.25,
colour = "black",
position = pd
) +
geom_line(position = pd) +
geom_point(position = pd, size = 2.5)
各子图(分面)中添加文字
mpg_plot <- ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(. ~ drv)
f_labels <- data.frame(drv = c("4", "f", "r"), label = c("4wd", "Front", "Rear"))
mpg_plot +
geom_text(x = 6, y = 40, aes(label = label), data = f_labels)
mpg_plot +
annotate("text", x = 6, y = 42, label = "label text")
lm_labels <- function(dat) {
mod <- lm(hwy ~ displ, data = dat)
formula <- sprintf("italic(y) == %.2f %+.2f * italic(x)",
round(coef(mod)[1], 2), round(coef(mod)[2], 2))
r <- cor(dat$displ, dat$hwy)
r2 <- sprintf("italic(R^2) == %.2f", r^2)
data.frame(formula = formula, r2 = r2, stringsAsFactors = FALSE)
}
library(dplyr)
labels <- mpg %>%
group_by(drv) %>%
do(lm_labels(.))
mpg_plot +
geom_smooth(method = lm, se = FALSE) +
geom_text(data = labels, aes(label = formula), x = 3, y = 40, parse = TRUE, hjust = 0) +
geom_text(x = 3, y = 35, aes(label = r2), data = labels, parse = TRUE, hjust = 0)
labels <- mpg %>%
group_by(drv) %>%
summarise(r2 = cor(displ, hwy)^2)
labels$r2 <- sprintf("italic(R^2) == %.2f", labels$r2)
labels
mpg_plot +
geom_smooth(method = lm, se = FALSE) +
geom_text(x = 3, y = 35, aes(label = r2), data = labels, parse = TRUE, hjust = 0)
参考资料
- https://r-graphics.org/