Using plotmath in ggplot2 with a percent sign (%)

I would like to use Greek characters, Latin characters and a percent sign in the face labels of the ggplot2 histogram. Greek characters can be executed with: 'facet_grid (. ~ Variable, labeller = label_parsed)':

a<-c("Delta~V","VarcoV","Delta~V","VarcoV") b<-c(1,2,3,4) d<-c("one","one","two","two") mydata<-data.frame(cbind(b,a,d)) ggplot(mydata,aes(x=d,y=b))+facet_grid(.~a, labeller=label_parsed)+geom_bar(stat="identity") 

Now I also want to add a face label that contains% and a latin character:

  a<-c("Delta~V","VarcoV","%V","Delta~V","VarcoV","%V") b<-c(1,2,3,4,5,6) d<-c("one","one","one","two","two","two") mydata<-data.frame(cbind(b,a,d)) ggplot(mydata,aes(x=d,y=b))+facet_grid(.~a, labeller= label_parsed)+geom_bar(stat="identity") 

This results in the following error:

  Error in parse(text = x) : <text>:1:1: unexpected input 1: %V ^ 

Any ideas on how to include a percent sign?

+7
source share
1 answer

Latin characters do not need special processing, and you can see this in the first element a . Try the following:

 a<-c("Delta~V","VarcoV","'%'*V","Delta~V","VarcoV","'%'*V") 

The% sign is special, so you need to quote it. You could just make "% V", but I selected the asterisk "*" (asterisk) to show how to separate plotmath markers without visible space. (You already know how to split tokens with a delimiter-delimiter, "~".)

The key lesson is to mix types of quotes. The first type of quote will signal which type is used to complete the character token / string. You can also use the escape character: "\". It also succeeds:

 a<-c("Delta~V","VarcoV","\"%\"*V","Delta~V","VarcoV","\"%\"*V") 
+10
source

All Articles