Format labels produced by cut() as percentages

后端 未结 6 2085
失恋的感觉
失恋的感觉 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:35

    Use gsub with some regex after multiplying your original data by 100:

    gsub("([0-9.]+)","\\1%",levels(cut(x*100,breaks=10)))
     [1] "(0.449%,10.4%]" "(10.4%,20.3%]"  "(20.3%,30.2%]"  "(30.2%,40.2%]"  "(40.2%,50.1%]"  "(50.1%,60%]"    "(60%,69.9%]"    "(69.9%,79.9%]"  "(79.9%,89.8%]"  "(89.8%,99.7%]"
    

提交回复
热议问题