rggplot2apostrophesuperscript

Apostrophe and superscript in ggplot axis label r


reproducible example:

x1<-rnorm(5,5,1)
y1<-rnorm(5,5,1)
d<-data.frame(x1,y1)

ggplot(data=d,aes(x=x1,y=y1))+
  geom_point()+
  ylab(bquote('y (since year s'* ~ 1^st*' day)'))

I would like to add an apostrophe after "year" and I tried to add '\\'' but it does not work.

There are already several questions asking about superscript:

Subscripts and superscripts "-" or "+" with ggplot2 axis labels? (ionic chemical notation)

How to write chemical formulas in ggplot

ggplot labels adding superscript, how to come out of the superscript?

But I could not find any that combines it with an apostrophe (which is recognized by the superscript expression).


Solution

  • Single quotes ' and double quotes " are usually interchangeable in R code. Get the single quote you want by surrounding the string containing it with double quotes.

    library(ggplot2)
    x1<-rnorm(5,5,1)
    y1<-rnorm(5,5,1)
    d<-data.frame(x1,y1)
    
    ggplot(data=d,aes(x=x1,y=y1))+
      geom_point()+
      ylab(bquote("y (since year's"* ~ 1^st*' day)'))
    

    Created on 2021-01-02 by the reprex package (v0.3.0)