ggplot缩放单位格式千位小数位

时间:2017-06-17 01:54:09

标签: r ggplot2 rstudio tidyverse

完成R / RStudio / tidyverse的菜鸟。 使用R 3.4.0“You Stupid Darkness”/ RStudio 1.0.136。 尝试格式化y轴以显示数千到1位小数。 我正在使用 : scale_y_continuous(labels = scales::unit_format("k", 1e-3))但显示为整数。如何显示1位小数而不是30k,我得到30.1k?

THX

2 个答案:

答案 0 :(得分:5)

如果您需要更灵活的内容,我建议您使用自己的自定义功能并将其插入scale_y_continuous,如下所示:

library(ggplot2)

# custom formatting function
scaleFUN <- function(x) sprintf("%.1fk", x/1000)

# setup diamonds dataset to display something in the thousands
diamonds2 <- diamonds
diamonds2$price <- diamonds2$price * 100

# make your plot and label using the custom scaleFUN function
ggplot(diamonds2, aes(x = carat, y = price)) + 
  geom_point() + 
  scale_y_continuous(name = 'Price ($K)', 
                     labels = scaleFUN)

答案 1 :(得分:2)

根据<button onclick="F1()"> <b>Step 1</b> </button> <div id="step1DIV" class="hidden"> <p> text </p> </div>,您可以设置?scales::unit_format

accuracy = 0.1