plotbiomes icon indicating copy to clipboard operation
plotbiomes copied to clipboard

Customisation of the axis - precipitation from cm to mm

Open ajpelu opened this issue 1 year ago • 1 comments

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

ajpelu avatar Jul 24 '24 18:07 ajpelu

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

valentinitnelav avatar Jul 25 '24 10:07 valentinitnelav

Ok, Thanks @valentinitnelav. It works.

ajpelu avatar Sep 13 '24 06:09 ajpelu