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.

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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)
3
+100

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

Comments

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

5 Comments

Thanks; there are two parts to the question though. Your response solves only the first issue.
@IndrajeetPatil Thanks, I updated with bquote as it seems more direct
Do you mind also providing a version of the solution that doesn't use bquote and is more in line with the existing code in the question?
@IndrajeetPatil I would have done that but there is another answer doing that
It doesn't though. It uses neither list nor paste, which is important for me because I want to save these expressions as a column in a dataframe.
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)

1 Comment

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: github.com/IndrajeetPatil/ggstatsplot/blob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.