valentinitnelav / plotbiomes

R package for plotting Whittaker biomes with ggplot2
https://valentinitnelav.github.io/plotbiomes/
MIT License
34 stars 9 forks source link

Customisation of the axis - precipitation from cm to mm #12

Open ajpelu opened 1 month ago

ajpelu commented 1 month ago

hi @valentinitnelav Thanks for your pkg. Is there any way to customize the axis (change the labels of the axis, or the units)? For instance, I'm interested in plot the precipitation axis in mm not in cm. Is there any way to do it? I tried this, but it does not work

library(scales)
trans_mm = scales::trans_new("mm", \(x) x*10, \(x) x/10) # Custom transformation
whittaker_base_plot() + scale_y_continuous(trans = trans_mm)

I got the message:

Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

Any advice? Thanks

valentinitnelav commented 1 month ago

Hi @ajpelu , what about this example below?

The main idea is to first apply the transformation to the data and then use it to plot:

library(plotbiomes)
library(ggplot2)

data(Whittaker_biomes)

# Transform cm to mm in data (or whatever transformation you may have), then do the custom plotting
Whittaker_biomes$precp_mm <- Whittaker_biomes$precp_cm * 10

plot_1 <- ggplot() +
  # add biome polygons
  geom_polygon(data = Whittaker_biomes,
               aes(x    = temp_c,
                   y    = precp_mm,
                   fill = biome),
               # adjust polygon borders
               colour = "gray98",
               size   = 1) +
  # fill the polygons with predefined colors
  scale_fill_manual(name   = "Whittaker biomes",
                    breaks = names(Ricklefs_colors),
                    labels = names(Ricklefs_colors),
                    values = Ricklefs_colors) +
  scale_x_continuous(expression("Temperature " ( degree*C))) +
  scale_y_continuous('Precipitation (mm)') + 
  theme_bw()
plot_1

image

ajpelu commented 1 day ago

Ok, Thanks @valentinitnelav. It works.