我正在尝试使用geom_text向ggplot图添加标签。我可以正确添加标签,但无法使用markdown标签。我需要使文本遵循统计惯例。
ggplot(diamonds,
aes(carat, price)) +
geom_point(alpha=0.1) +
geom_smooth(method="lm") +
geom_text(size=2.5, aes(x = 4, y = 4,
label=paste("r^2=", 0.34, "\n",
"gradient= ", 0.56, "\n",
"_p_=", 0.003)))
答案 0 :(得分:2)
使用@missuse答案的here,我们可以做到
nonmetric_label = c(paste0("italic(R)^2 ==", 0.34),
paste0("gradient ==", 0.65),
paste0("italic(p) ==", 0.003))
ggplot(diamonds,
aes(carat, price)) +
geom_point(alpha=0.1) +
geom_smooth(method="lm") +
annotate("text", x = c(4,4,4), y = c(6500,3250,500),
label = nonmetric_label , parse = TRUE)