2

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.

Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51

4 Answers4

4

Use the formula shown:

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

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks. I would prefer a solution that doesn't change the code specified in the question though. I want to use `list` + `paste` because I am saving these expressions as columns of type of `character`. – Indrajeet Patil Aug 26 '19 at 00:45
  • 1
    Just define the same expression as a character string in which case you can paste it together from elements in any way you like. `subtitle <- "italic(mu) == 3.22*', '*'CI'[95*'%']*group('[',2.87*','*3.57,']')"` Then use this in ggplot: `subtitle = parse(text = subtitle)` – G. Grothendieck Aug 26 '19 at 02:19
3

An option would be bquote

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

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
3

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

Edo
  • 7,567
  • 2
  • 9
  • 19
2

I'll assume that what you actually care about is that the output looks right, not that plotmath is used. You can use the ggtext package I'm currently developing, which gives you the possibility to use simple markdown/HTML inside of ggplot2. I generally find it much easier to create basic math expressions that way than wrangling with plotmath. And you don't have to work with R expressions at all, the input is always a simple character string.

# this requires the current development versions of ggplot2 and ggtext
# remotes::install_github("tidyverse/ggplot2")
# remotes::install_github("clauswilke/ggtext")

library(ggplot2)
library(ggtext)

ggplot(mtcars, aes(as.factor(cyl), wt)) + 
  geom_boxplot() +
  labs(
    title = "Mean weight:",
    subtitle = "*&mu;* = 3.22, CI<sub>95%</sub>[2.87, 3.57]"
  ) +
  theme(plot.subtitle = element_markdown())

Created on 2019-12-02 by the reprex package (v0.3.0)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • Thanks, Claus! I am excited to see that displaying expressions in `ggplot` will get easier in future using `ggtext`. But, currently, this solution doesn't work for me. I would like the solution to stick to the format (as `list` in `character` type column that can be parsed) I've shown because it's the one I need to implement in a package function: https://github.com/IndrajeetPatil/ggstatsplot/blob/8403eff24f117747dc5b5aaad2fcbcdc2565f8b7/R/helpers_ggbetweenstats_graphics.R#L65-L82 – Indrajeet Patil Dec 03 '19 at 09:04