1

I want to add the following title to a ggplot:

"Number of times where UK emissions of Sulphur Dioxide exceeded 10 microgrammes per metre cubed"

The components of this title come from:

free text = "Number of times where UK emissions of "
input$select = Sulphur Dioxide (or any other pollutant the user selects)
free text = "exceeded "
input$number = 10 (or any other integer the user selects)
formula = μg/m3 (where the 3 is superscript of m)

Have used a combination of paste and expression and bquote, but its just not coming out right.

Here's my code for the above....

ggplot() + 
aes(x=year, y=cnt) +
labs(title = paste("Number of times where UK emissions of ", input$select, 
   " exceeded", input$number, expression(mu), "g/m", "^{3}"))
stixmcvix
  • 313
  • 1
  • 2
  • 10
  • What exactly does your combination of `expression` and `bquote` look like? It would help if you provided some sort of [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make testing possible solutions easier. The fact that you are using shiny isn't all that relevant. Just assume that everything from `input$` is probably a string. – MrFlick Jan 17 '20 at 15:52
  • 1
    I'm not sure what you are asking, but if you use `"\u03BCg/m\u00B3"` for the `μg/m³` part it will print nicely. – Allan Cameron Jan 17 '20 at 15:58
  • Have added my code in to the original question – stixmcvix Jan 17 '20 at 16:03
  • You little beauty @AllanCameron. That has worked a treat :) Thanks a million. – stixmcvix Jan 17 '20 at 16:07
  • Great @stixmcvix . Have added as answer. – Allan Cameron Jan 17 '20 at 16:14

2 Answers2

1

I'm not sure what you are asking, but if you use "\u03BCg/m\u00B3" for the μg/m³ part it will print nicely.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
0

Or if you wanted to use the proper plotmath expressions, It would look like this

library(ggplot2)
ggplot(mtcars) + 
  aes(x=mpg, y=disp) +
  labs(title = bquote("Number of times where UK emissions of "~.(input$select)~ 
                     " exceeded"~.(input$number)~mu*g/m^3))
MrFlick
  • 195,160
  • 17
  • 277
  • 295