You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! I am trying to use the label_ functions for plotting formatted numbers in geom_text() instead of the intended use in ggplot2 scales. But the output of label_log() is different from that of label_scientific() so I thought this could be improved upon to make consistent?
I had to use the deparse() function with a map_chr() call to make scale_log() work eventually..
Here's the outputs of the label_..() functions -
# returns expression (not string)scales::label_log(digits=1)(c(1, 10, 100))
#> expression(10^0, 10^1, 10^2)# returns string vector as documentedscales::label_scientific(digits=1)(c(1, 10, 100))
#> [1] "1e+00" "1e+01" "1e+02"# showing the functional outputscales::label_log()
#> function (x) #> {#> if (length(x) == 0) {#> return(expression())#> }#> exponent <- format(log(x, base = base), digits = digits)#> text <- paste0(base, "^", exponent)#> ret <- parse_safe(text)#> ret[is.na(x)] <- NA#> ret#> }#> <bytecode: 0x0000021649f9b4b8>#> <environment: 0x000002164d3b7290>
Here's an example code to make the geom_text() which only works with label_scientific()
library(tidyverse)
# make simple test data 3 columns, 5 rowsb<-tibble::tibble(b1=1:5, b2=10^b1)
# with label_scientificggplot2::ggplot(b,
aes(x=b1, y=b2)) +
geom_point() +
geom_text(aes(label=scales::label_scientific(digits=1)(b2)), vjust=-2)
# with label_log()
ggplot(b,
aes(x=b1, y=b2)) +
geom_point() +
geom_text(aes(label=scales::label_log()(b2)), vjust=-2)
#> Don't know how to automatically pick scale for object of type <expression>.#> Defaulting to continuous.#> Error in `geom_text()`:#> ! Problem while computing aesthetics.#> ℹ Error occurred in the 2nd layer.#> Caused by error in `compute_aesthetics()`:#> ! Aesthetics are not valid data columns.#> ✖ The following aesthetics are invalid:#> ✖ `label = (scales::label_log())(b2)`#> ℹ Did you mistype the name of a data column or forget to add `after_stat()`?# I can fix it by mapping a deparse() function
ggplot(b,
aes(x=b1, y=b2)) +
geom_point() +
geom_text(aes(label=scales::label_log()(b2) %>%
{map_chr(as.list(.), deparse)}
),
vjust=-2)
Hello! I am trying to use the
label_
functions for plotting formatted numbers ingeom_text()
instead of the intended use inggplot2 scales
. But the output oflabel_log()
is different from that oflabel_scientific()
so I thought this could be improved upon to make consistent?I had to use the
deparse()
function with amap_chr()
call to makescale_log()
work eventually..Here's the outputs of the
label_..()
functions -Created on 2024-07-31 with reprex v2.1.0
Here's an example code to make the
geom_text()
which only works withlabel_scientific()
Created on 2024-07-31 with reprex v2.1.0
The text was updated successfully, but these errors were encountered: