0

Update. With thanks of Limey in the comment below, the solution involved adding the sprintf within the scale_fill_viridis_c command, see code below. The original question: This is a simple problem that I have not managed to solve. I would like to have 2 decimal digits after the decimal point in the ggplot legend and not 6 as now (see figure below). I have looked at answers at stack overflow (e.g. how to put exact number of decimal places on label ggplot bar chart) but for some reasons, the solutions there do not seem to work in my case. I have e.g. tried using:
label=sprintf("%0.2f", round(c.t, digits = 2)))) and geom_text(aes(label = round(c.t, digits = 2)))

e.g. ggplot(aes(lon, lat, fill = c.t, label=sprintf("%0.2f", round(c.t, digits = 2))))

Please inform me if it is needed to post segment of the data to make it fully reproducible.

Legend with log transformed number with  6 decimal digits

 `library(geo) 
library(tidyverse)
Polychaeta<-Polychaeta%>% 
  dplyr::mutate(lon = geo::sr2d(grid_small.grid)$lon,
                lat = geo::sr2d(grid_small.grid)$lat) %>% 
  ggplot(aes(lon, lat, fill = c.t)) +
  ggtitle("a. Polychaeta")+
  labs(fill= "Tonnes")+
  theme_void() +
  geom_tile() +
  geom_polygon(data = geo::island, fill = "grey") +
  coord_quickmap() +
 scale_fill_viridis_c(option = "B", direction = -1, trans="log", label = function(x) sprintf("%.2f", x))`

0 Answers0