Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot2 annotate with subscript and tilde

I'm trying to annotate a ggplot2 plot with a label that states the distribution for X1 ~ N(mu=10,sigma=3), where the 1 is meant to be subscript, like this:

label1<-"X[1] ~ N( \U03bc = 10, \U03c3 = 3)"

When I use label1 like this:

library(ggplot2)
label1<-"X[1] ~ N( \U03bc = 10, \U03c3 = 3)"
ggplot() + annotate("text", x=18,y=0.05,label=label1)

The subscript is ignored, the rest is what is wanted.

When I use label1 like this:

library(ggplot2)
label1<-"X[1] ~ N( \U03bc = 10, \U03c3 = 3)"
ggplot() + annotate("text", x=18,y=0.05,label=label1,parse=TRUE)

the subscript is plotted correct, but the tilde transforms in a space.

Any advice how to make and the subscript and the tilde-sign happen? Thanks!

like image 603
MartineJ Avatar asked Sep 18 '17 13:09

MartineJ


2 Answers

You can replace ~ with %~% in method 2 (i.e. parse = TRUE). I also replaced the unicode for mu & sigma with their greek letter representations:

label1 <- "X[1] %~% N(mu == 10, sigma == 3)"
ggplot() + 
  annotate("text", x=18, y=0.05, label=label1, parse=TRUE)

image

like image 193
Z.Lin Avatar answered Nov 16 '22 07:11

Z.Lin


Add % to the tilde as such:

label1<-"X[1] %~% N( \U03bc = 10, \U03c3 = 3)"
like image 22
timfaber Avatar answered Nov 16 '22 05:11

timfaber