pluke
pluke

Reputation: 4356

ggplot geom_text accept markdown

I'm trying to add a label to a ggplot graph using geom_text. I can add the label correctly, but I can't get the label to use markdown. I need the text to follow statistical conventions.

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

Upvotes: 1

Views: 1074

Answers (1)

A. Suliman
A. Suliman

Reputation: 13135

Using @missuse answer's here, we can do

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

Upvotes: 2

Related Questions