I need to apply cut
on a continuous variable to show it with a Brewer color scale in ggplot2, as in Setting breakpoints for data with scale_fill_brewer() function in ggplot2. The continuous variable is a relative difference, and I'd like to format the data as "18.2 %" instead of "0.182". Is there an easy way to achieve this?
x <- runif(100)
levels(cut(x, breaks=10))
[1] "(0.0223,0.12]" "(0.12,0.218]" "(0.218,0.315]" "(0.315,0.413]"
[5] "(0.413,0.511]" "(0.511,0.608]" "(0.608,0.706]" "(0.706,0.804]"
[9] "(0.804,0.901]" "(0.901,0.999]"
I'd like, e.g., the first level to appear as (2.23 %, 12 %]
. Is there a better alternative to cut
?
I have implemented cut_format()
in version 0.2-3 of my kimisc
package, version 0.3 is on CRAN now.
# devtools::install_github("krlmlr/kimisc")
x <- seq(0.1, 0.9, by = 0.2)
breaks <- seq(0, 1, by = 0.25)
cut(x, breaks)
## [1] (0,0.25] (0.25,0.5] (0.25,0.5] (0.5,0.75] (0.75,1]
## Levels: (0,0.25] (0.25,0.5] (0.5,0.75] (0.75,1]
cut_format(x, breaks, format_fun = scales::percent)
## [1] (0%, 25%] (25%, 50%] (25%, 50%] (50%, 75%] (75%, 100%]
## Levels: (0%, 25%] (25%, 50%] (50%, 75%] (75%, 100%]
It's still not perfect, passing the number of breaks (as in the original example) doesn't work yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With