Ggplot annotated with Greek symbol and (1) apostrophe or (2) between text

It is impossible to add Greek letters to ggplot annotate when either: it is sandwiched between another text, or the text in question contains an apostrophe.

For example, the following works just fine:

df <- data.frame(x =  rnorm(10), y = rnorm(10))
temp<-paste("rho == 0.34")
ggplot(df, aes(x = x, y = y)) + geom_point() +
    annotate("text", x = mean(df$x), y = mean(df$y), parse=T,label = temp)

However, ggplot explodes when I do:

df <- data.frame(x =  rnorm(10), y = rnorm(10))
temp<-paste("Spearman rho == 0.34")
ggplot(df, aes(x = x, y = y)) + geom_point() +
    annotate("text", x = mean(df$x), y = mean(df$y), parse=T,label = temp)

ggplot upsets the sensitivity to these special characters. Other posts do not seem to address this issue (sorry if not). Thanks in advance.

+4
source share
2 answers

I felt your disappointment. The trick, besides using expressionfor the shortcut commented by @baptiste, should pass your shortcut as.characterto ggplot.

df <- data.frame(x =  rnorm(10), y = rnorm(10))
temp <- expression("Spearman's"~rho == 0.34)
ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  annotate("text", x = mean(df$x), y = mean(df$y), 
           parse = T, label = as.character(temp))

enter image description here

+5

, - , , escape- , :

df <- data.frame(x =  rnorm(10), y = rnorm(10))

spearmans_rho <- cor(df$x, df$y, method='spearman')
plot_label <- sprintf("\"Spearman's\" ~ rho == %0.2f", spearmans_rho)

ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  annotate("text", x = mean(df$x), y = mean(df$y), parse = T, label=plot_label)
+1

All Articles