aphalo / photobiology

Package ‘photobiology’ defines a system of classes for storing spectral data and accompanying methods and operators. This is the core of a suite of R packages for photobiological calculations.
3 stars 1 forks source link

autoplot + theme #31

Open CdeMills opened 1 month ago

CdeMills commented 1 month ago

Hello

I'm trying to customize a source_spct plot with theming.

Using

ggplot(sun.spct) + geom_spct() + theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
                                                                axis.title = element_text(size = 12, face = "bold"),
                                                               axis.text = element_text(size = 12, face = "bold"))

Produces a graph where axis text and labels are in bold, but without any special decoration.

autoplot(sun.spct) + theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
                                             axis.title = element_text(size = 12, face = "bold"),
                                             axis.text = element_text(size = 12, face = "bold"))

Produce something quite similar, with the required annotations, except that the vertical axis text (Spectral Energy Irradiance ...) seems to belong to another class and is not rendered the same way. Could you check ?

Using photobiology_0.11.2 ggplot2_3.5.1 ggspectra_0.3.12

Thank you

Pascal

CdeMills commented 1 month ago

Hello follow-up: I tried

autoplot(sun.spct) + theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold") +
                                              axis.title = element_text(size = 12, face = "bold", family="Aurebesh")
                                              axis.text = element_text(size = 12, face = "bold"))

"Aurebesh" being the font used in Star Wars movies with a clearly recognisable (or rather unrecognisable) pattern. The font change occurs in rstudio under Linux, but not with rstudio under WIndows. So it's rather a small pecularity of the underlying rendering.

aphalo commented 1 month ago

Hi. Thanks for reporting this. Have you checked which graphic device is being used by RStudio under each operating system? It can be set through menues. It would be interesting to test this problem by printing/exporting using different graphic devices: say pdf, cairo.pdf, png, etc. I will try this and check if this is a problem with 'ggplot2' or with 'ggspectra'.

CdeMills commented 1 month ago

Pedro, I found the real culprit ... in ggspectra, the ylab is produced using the "plotmath" engine. That engine has its own idea about the text being bold or not. The following code produce the expected result:

gg <- autoplot(sun.spct) +  xlab("Wavelength [nm]") +
          ylab(bquote(bold("Spectral Energy Irradiance" ~ 
                                     E[lambda]~~ "[" ~W~m^-2~nm^-2~"]"))) +
         theme(plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),  
         axis.title = element_text(size = 12, face = "bold"),
         axis.text = element_text(size = 12, face = "bold"))
print(gg)

In that version, ylab is redefined in a way similar to ggspectra approach, but with the whole definition inside bold(). Tested (for now) with rstudio (linux) + "AGG" gaphics + cairo as export:

phi <- .5 * (1 + sqrt(5))
args_png <- list(height = 10, width = 10 * phi, units = "cm")
args_eps <- list(height = 10, width = 10 * phi, units = "cm",
                         device = cairo_ps)
args_pdf <- list(height = 10, width = 10 * phi, units = "cm",
                         device = cairo_pdf)
rlang::exec(ggsave, !!! c(filename = "Sun.svg", args_png))
rlang::exec(ggsave, !!! c(filename = "Sun.png", args_png))
rlang::exec(ggsave, !!! c(filename = "Sun.eps", args_eps))
rlang::exec(ggsave, !!! c(filename = "Sun.pdf", args_pdf))
aphalo commented 1 month ago

O.k., thanks! Then it seems that plotmath does not obey theme settings, which is not surprising. Not everybody wants bold axis labels, so I do not see an easy way out with plotmath, and it is needed to get the units well formatted. Implementing support for markdown encoded axis labels could be an improvement... this is in my to do list but I am waiting until package 'marquee' gets more stable. 'ggtext' does work with some limitations but it looks like 'marquee' aims to replace it and get better integrated into 'ggplot2'. I think I will make sure all axis labels are defined using plotmath, and document that they do not obey the theme settings related to fonts. At least consistency will be maintained.

CdeMills commented 1 month ago

Pedro, I experimented a bit with plotmath. I came with this definition for ylab using bold fonts:

ylab(bquote(bold("Spectral energy irradiance" ~
                     E[lambda] ~~ group("[", W~m^{-2}~nm^{-1}, "]")))) 

The differences with your approach

Spectral~~energy~~irradiance~~E[lambda]~~(W~m^{-2}~nm^{-1})"" 

IMHO, the vertical label constructed this way is a bit more compact.

aphalo commented 2 weeks ago

Thanks! Yes, having the text as a character string embedded in the plotmath expression is a better approach. Changing this would make sense. As to using bold, it would need to be conditional on being selected by users. The same applies for how to display units. I know that in some fields it is customary to show units in square brackets, or even without any brackets. It is difficult to have automatically generated annotations and axis labels that are flexible avoiding code bloat. My aim is to change the autoplot() methods so that the axis-label construction is done in scales so that the consistency between plots created with autolplot() and ggplot() is easy to achieve.