bcgov / ssdtools

An R package to fit and plot Species Sensitivity Distributions (SSDs)
https://bcgov.github.io/ssdtools/
Apache License 2.0
31 stars 17 forks source link

Allow expressions in the xlab #376

Closed jenni-gnz closed 2 months ago

jenni-gnz commented 2 months ago

I would like to label the x-axis in ssdplots with “Dissolved copper (ug/L)” where the u is actually a mu. Usually I do this with expression in xlab(), but the way that the plot ssd_plot function works it doesn’t update if I use + xlab(expression("Copper EC10 value ("mu"g/L)")); and if I use an expression within ssd_plot (xlab = "xxxx") that doesn’t work either. Is there a way to allow x-labels to be added that allows for expressions and symbols? Thanks

joethorley commented 2 months ago

@jenni-gnz - this code does what you want

library(ssdtools)
library(ggplot2)
library(latex2exp)

gp <- ssd_plot(ssddata::ccme_boron, boron_pred, label = "Species", shape = "Group")
gp + scale_x_continuous(TeX("Concentration ($\\mu g$/L)"))
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.

Created on 2024-09-18 with reprex v2.1.1

jenni-gnz commented 2 months ago

Thanks. I also had to add into scale_x_continuous(..., breaks = c(1, 10, 100, 1000, 10000)) to stop the scale labels changing to the squished up look. For those that want a subscript in there, here is a solution: scale_x_continuous(TeX("Copper $EC10_{my subscript}\\phantom{x}(\\mu g$/L)$"), breaks = c(1, 10, 100, 1000, 10000))

(this took ages to figure out as extra spaces were added with various other options, so adding here in case anyone else wants it, including future me)

joethorley commented 2 months ago

Thanks @jenni-gnz - we'll add it to the FAQs

joethorley commented 2 months ago

done in dev branch