Search code examples
rggplot2text-parsing

parse in geom_label : ERROR while rich displaying an object: Error in parse(text = text[[i]])=


I print a geom_label using ggplot with the following code :

ggplot()+
    geom_label(aes( x = 10, y = 0.545, label = " expression 1 =  p-value = 9.19 e-09 ; CI : [0.00, 0.00]"), 
             label.size = NA , hjust =0.08, size = 3.5, family = "Helvetica")

and I am trying to print the p in italic.
So I have tried

ggplot()+
    geom_label(aes( x = 10, y = 0.545, label = " expression 1 =  p-value = 9.19 e-09 ; CI : [0.00, 0.00]"), 
             label.size = NA , hjust =0.08, size = 3.5, family = "Helvetica")+
    geom_label(aes( x = 10, y = 0.509, label = paste("expression 2 =  italic(p)-value = 9.19 e-09 ; CI : [0.00, 0.00]")), 
             label.size = NA, hjust =0.08, size = 3.5, family = "Helvetica", parse = T)

Which return me the folloing error message :

ERROR while rich displaying an object: Error in parse(text = text[[i]]): :1:19: unexpected string constant 1: 'expression 1 : ' 'italic(p)'

Do you know where the error come from and how to solve it ?

PS : Based on this post Unexpected symbol error in parse(text = str) with hyphen after a digit, I also tried

ggplot()+
    geom_label(aes( x = 10, y = 0.545, label = " expression 1 =  p-value = 9.19 e-09 ; CI : [0.00, 0.00]"), 
             label.size = NA , hjust =0.08, size = 3.5, family = "Helvetica")+
    geom_label(aes( x = 10, y = 0.509, label = paste("'expression 2 =  italic(p)-value = 9.19 e-09 ; CI : [0.00, 0.00]'")), 
             label.size = NA, hjust =0.08, size = 3.5, family = "Helvetica", parse = T)

which works but returns me this res


Solution

  • Study help("plotmath").

    library(ggplot2)
    ggplot()+
      geom_label(aes(x = 10, y = 0.545, 
                     label = " expression 1 =  p-value = 9.19 e-09 ; CI : [0.00, 0.00]"), 
                 label.size = NA , hjust =0.08, size = 3.5)+
      geom_label(aes(x = 10, y = 0.509, 
                     label = paste("'expression 2 = ' *  italic(p) * '-value = 9.19 e-09 ; CI : [0.00, 0.00]'")), 
                 label.size = NA, hjust =0.08, size = 3.5, parse = T)
    

    I have removed family = "Helvetica" because it caused warnings on my system.