ggplot geom_text接受降价

时间:2018-08-22 20:06:36

标签: r ggplot2 r-markdown

我正在尝试使用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)))

output

1 个答案:

答案 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) 

enter image description here

相关问题