thiagoveloso
thiagoveloso

Reputation: 2763

R - Add string with superscript to faceted plots

I am trying to add trends and significance level on some faceted plots made by ggplot2. The text includes subscript characters, and this is when the problem arises.

Basically, this is the dataset I am plotting:

library(ggplot2)

dat <- structure(list(year = c(2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 
2018L, 2019L, 2020L, 2021L, 2022L, 2023L, 2024L, 2025L, 2026L, 
2027L, 2028L, 2029L, 2030L, 2031L, 2032L, 2033L, 2034L, 2035L, 
2036L, 2037L, 2038L, 2039L, 2040L, 2041L, 2042L, 2043L, 2044L, 
2045L, 2046L, 2047L, 2048L, 2049L, 2050L, 2051L, 2052L, 2053L, 
2054L, 2055L, 2056L, 2057L, 2058L, 2059L, 2060L, 2061L, 2062L, 
2063L, 2064L, 2065L, 2066L, 2067L, 2068L, 2069L, 2070L, 2071L, 
2072L, 2073L, 2074L, 2075L, 2076L, 2077L, 2078L, 2079L, 2080L, 
2081L, 2082L, 2083L, 2084L, 2085L, 2086L, 2087L, 2088L, 2089L, 
2090L, 2091L, 2092L, 2093L, 2094L, 2095L, 2096L, 2097L, 2098L, 
2099L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2018L, 2019L, 
2020L, 2021L, 2022L, 2023L, 2024L, 2025L, 2026L, 2027L, 2028L, 
2029L, 2030L, 2031L, 2032L, 2033L, 2034L, 2035L, 2036L, 2037L, 
2038L, 2039L, 2040L, 2041L, 2042L, 2043L, 2044L, 2045L, 2046L, 
2047L, 2048L, 2049L, 2050L, 2051L, 2052L, 2053L, 2054L, 2055L, 
2056L, 2057L, 2058L, 2059L, 2060L, 2061L, 2062L, 2063L, 2064L, 
2065L, 2066L, 2067L, 2068L, 2069L, 2070L, 2071L, 2072L, 2073L, 
2074L, 2075L, 2076L, 2077L, 2078L, 2079L, 2080L, 2081L, 2082L, 
2083L, 2084L, 2085L, 2086L, 2087L, 2088L, 2089L, 2090L, 2091L, 
2092L, 2093L, 2094L, 2095L, 2096L, 2097L, 2098L, 2099L), scen = c("RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 4.5", 
"RCP 4.5", "RCP 4.5", "RCP 4.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", "RCP 8.5", 
"RCP 8.5"), mean = c(331.679093983488, 336.675841932168, 356.757081318776, 
340.794923663589, 361.141206264033, 322.781918204685, 385.433918818759, 
380.702401030404, 328.154492680223, 375.056187891587, 347.508793315836, 
329.683327634971, 366.301636247352, 305.269460861638, 318.325452041009, 
340.040273546697, 325.61840639099, 332.701705036009, 305.419280480644, 
355.881017126304, 341.705597163198, 364.689829628731, 336.100633972456, 
310.993566815872, 318.7211477292, 383.093389414867, 362.683808979847, 
353.366601045305, 337.455762950249, 327.204334366791, 353.944525594788, 
365.569618044337, 316.609658092386, 365.492509562037, 338.823736230177, 
326.990255170755, 337.12880224233, 364.018173018607, 355.559313186275, 
345.835649384861, 293.344988355919, 350.2548370495, 326.503131882392, 
347.887581770452, 339.261222911228, 356.961652791314, 345.493946556441, 
356.664486187732, 313.817590707126, 320.579682130737, 353.26217280519, 
364.193121331002, 314.969531819531, 318.071678726834, 337.395178039222, 
360.837824180377, 374.228500679098, 340.846453091398, 327.251036512267, 
330.593965621599, 330.419870983545, 320.366991284311, 344.864360664964, 
352.257307469967, 365.111966274704, 331.293272934474, 322.518548693901, 
340.297573036132, 340.917774500577, 329.804655233008, 327.124699614286, 
300.365317850756, 342.861653935517, 311.358349249704, 328.453339172631, 
346.038216932353, 346.766752960328, 319.190762615461, 325.559678273368, 
332.168944750783, 320.473543271376, 344.039703666605, 340.735082598786, 
330.317665822525, 320.239744179718, 360.765596287051, 319.215582620153, 
329.397678349641, 367.425644582558, 351.405690877457, 327.764435329283, 
320.911673923194, 379.596203115623, 327.124563391652, 355.993699861717, 
364.836645848153, 343.546366089566, 316.597684965416, 319.633607826233, 
351.626592751372, 325.431155017843, 319.106889321875, 357.303044581657, 
349.645502396967, 330.835305913427, 329.695569944562, 328.533509632532, 
348.577454519246, 320.803612550699, 349.175559755207, 362.507923544398, 
332.971101638609, 315.012143351717, 361.135215088672, 333.861975572386, 
333.63069170209, 322.770000848899, 323.219699185415, 337.606463038298, 
324.301435827476, 307.596445335943, 354.275656643891, 358.378815636134, 
322.323420431029, 355.007944626204, 323.734865475369, 328.398173983695, 
334.123595755852, 347.740272990841, 342.600839556979, 332.078058558359, 
329.722800202485, 348.016231218117, 283.936542624605, 317.954962943558, 
343.853292305013, 316.415594685187, 313.506610683122, 310.837729503909, 
317.261464179931, 339.092730322064, 356.218725969052, 324.340904129111, 
329.477747235979, 327.91232498827, 309.729236320742, 342.626327404423, 
316.836359836033, 318.479932398423, 292.983254747108, 318.412394099583, 
309.685068576021, 315.329740942469, 323.122255089855, 319.140169330916, 
326.390173349715, 330.555480409915, 326.790038618823, 312.856556940927, 
331.198281594557, 310.935485522741, 298.144092924833, 332.815595864041, 
313.316276265887, 287.756945811166, 322.338512454071, 325.515444297276, 
300.451757754303, 300.274937678746, 293.091256119042, 288.493754529722, 
307.740644001614, 310.202440599097, 291.851698474421, 297.353643193515, 
281.511755611183)), .Names = c("year", "scen", "mean"), row.names = c(NA, 
176L), class = "data.frame")

ggplot(dat, aes(x=year, y=mean)) + geom_line(color="blue") + 
  geom_smooth(method="loess", se=F, color="red") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  ylab("Irrigation (mm)") + xlab("") +
  facet_grid(scen ~ .)

This plot shows a time series of irrigation demand for a certain region until the end of this century. The facets represent a medium (RCP 4.5) and pessimistic (RCP 8.5) climate change scenarios.

Also, based on the results of a statistical test (Mann-Kendall) to determine trends in time series, I have created a data frame containing the text I would like to add:

# Here I reproduce the Mann-Kendall test results. One for each climate scenario.
test.stats1 <- structure(list(sl = 0.0511982476153832, S = -542, B = -0.166370102781674, 
    varS = 76985.3333333333, sl.corrected = NA, varS.corrected = NA, 
    partial.S = NA, partial.sl = NA, partial.varS = NA, partial.sl.corrected = NA, 
    partial.varS.corrected = NA, tau = -0.141588296760711), .Names = c("sl", 
"S", "B", "varS", "sl.corrected", "varS.corrected", "partial.S", 
"partial.sl", "partial.varS", "partial.sl.corrected", "partial.varS.corrected", 
"tau"), class = "rkt")

test.stats2 <- structure(list(sl = 2.79597145436128e-09, S = -1650, B = -0.494562069956172, 
    varS = 76985.3333333333, sl.corrected = NA, varS.corrected = NA, 
    partial.S = NA, partial.sl = NA, partial.varS = NA, partial.sl.corrected = NA, 
    partial.varS.corrected = NA, tau = -0.431034482758621), .Names = c("sl", 
"S", "B", "varS", "sl.corrected", "varS.corrected", "partial.S", 
"partial.sl", "partial.varS", "partial.sl.corrected", "partial.varS.corrected", 
"tau"), class = "rkt")

# Here, I create a data frame containing the text I need to add to the plot
df.stats <- data.frame(year=2090,
                       label=c(paste0("Trend: ", round(test.stats1$B,2), " mm year^-1"," (p=", round(test.stats1$sl,digits=2),")"),
                               paste0("Trend: ", round(test.stats2$B,2), " mm year^-1"," (p=", format.pval(test.stats2$sl,digits=3),")")),
                       scen=c("RCP 4.5","RCP 8.5"),
                       value=c(375,375))

However, my attempt to include the strings to the figure generates an error:

ggplot(dat, aes(x=year, y=mean)) + geom_line(color="blue") + 
  geom_smooth(method="loess", se=F, color="red") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  ylab("Irrigation (mm)") + xlab("") +
  facet_grid(scen ~ .) +
  geom_text(data=df.stats, aes(x=year, y=value, label=label), 
            colour="black", parse=TRUE)

which produces the error:

Error in parse(text = as.character(lab)) : <text>:1:14: unexpected symbol
1: Trend: -0.17 mm

What am I missing here?

Upvotes: 1

Views: 159

Answers (1)

thiagoveloso
thiagoveloso

Reputation: 2763

After some more research, I found the answer.

The key here is to use tildes:

"Trend:~-0.49~mm~year^-1~(p==2.8e-09)"

Because geom_text interprets labels in the format of plotmath.

Upvotes: 2

Related Questions