How can I mathematically transform the default labels on legend in ggplot?

masher

I have a 2D surface plot made with ggplot which I am colouring to show the value of it's third dimension. This data is wide-ranging in the Z direction, so I have log-transformed it before doing the interpolation for plotting.

I like the default choices that the plotting algorithm has chosen for the breaks. However, the labels for those breaks are now the log values.

The current code for the colouring is:

scale_fill_viridis(breaks = waiver(), labels = waiver())

waiver() is a flag to the underlying algorithm to use default values. Ideally, I'd like to do something like

scale_fill_viridis(breaks = waiver(), labels = sprintf("%.1f", 10^waiver()))

but that breaks with a Error in 10^my_breaks : non-numeric argument to binary operator error

How can I mathematically transform the default values for the labels into the values they would be prior to the log transform?

Greg

How about something like this:

your_function <- function(x){sprintf("%.1f", 10^as.numeric(x))}

labels = scales::trans_format("identity", your_function)

your_function will take the inverse of log10, effectively undoing the log transform. scales::trans_format will reformat the given label by applying whatever function it passed to it, in this case your_function.

You can of course define the reformatting function inside trans_format but in order to make things as clear as possible I've done it in two steps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I change the legend labels in R (ggplot2)

How to modify ggplot legend labels

How can I add a legend to this plot (with ggplot())?

How can I customize certain labels, but keep default values, in ggplot guide_colorsteps?

How can I rename legend labels in Vega Lite?

How to edit the current legend labels in ggplot

Default colors in ggplot2 for extended legend labels

How can I change the legend linetype in ggplot2?

How can I replace legend key in ggplot2?

How can I force ggplot to show more levels on the legend?

How can I edit legend using ggplot2 in R?

How can I combine legends for color and linetype into a single legend in ggplot?

How can I add a legend for error bars in ggplot?

How can I have both a legend and data labels, with different labels, in Highcharts?

Reorder ggplot legend labels

ggplot missing labels in legend

How can colour be added manually in ggplot and legend labels renamed in the same plot?

How can I mathematically compare two unknown types in Scala?

How can I represent any negative value mathematically using summation?

How do I get custom colors AND have my legend have the correct labels (ggplot)?

How do I calculate mathematically the transform 3D I to be applied on my CSS?

How can I completely remove legend.key from ggplot2 legend?

How can I rotate labels in ggplot2?

How can I rotate the tick labels in ggplot and annotate missing dates?

How can I add the reactive y axis labels in shiny with ggplot

How can I customize labels in ggplot guide_colorsteps?

How can I change the placement of Facet_Wrap labels in ggplot?

How can I wrap AND truncate axis labels via ggplot?

How do I format the numerical labels in a legend?