Format labels produced by cut() as percentages

后端 未结 6 2083
失恋的感觉
失恋的感觉 2021-02-05 10:21

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 i

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 10:41

    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.

提交回复
热议问题