Search code examples
rparsingggplot2expressionplotmath

using `plotmath` to display combination of subscript and `[ ]`


I want to create a plot where I want to display a mean value and confidence intervals for this mean value. To do so, I am using plotmath. Here is something I have done that works-

library(ggplot2)

ggplot(mtcars, aes(as.factor(cyl), wt)) + geom_boxplot() +
  labs(
    title = "Mean weight:",
    subtitle = parse(text = paste(
      "list(~italic(mu)==", 3.22, ",", "CI[95~'%'] ", "(", 2.87, ",", 3.57, "))",
      sep = ""
    ))
  )

Created on 2019-08-25 by the reprex package (v0.3.0)

But this is not what I actually want. The format in which I instead want to display these results is the following-

enter image description here

So there are two things I can't seem to figure out how to do using plotmath:

  1. 95 % should instead be 95%

  2. Use [ instead of (

How can I do this?

P.S. It is important, for reasons to complicated to explain here, for me to have list inside the paste function because I want to save these expressions as a character-type column in a dataframe. This is why I haven't accepted the two solutions provided below.


Solution

  • This solution keeps list and paste.

    library(ggplot2)
    
    ggplot(mtcars, aes(as.factor(cyl), wt)) + geom_boxplot() +
      labs(
        title = "Mean weight:",
        subtitle = parse(text = paste(
          "list(~italic(mu)==", 3.22, ",", "CI[95*'%'] ", "*'['*", 2.87, ",", 3.57, "*']')",
          sep = ""
        ))
      )
    
    

    enter image description here