ggplot 比例单位格式千位小数点后一位
ggplot scales unit format thousands one decimal place
完全成为 R/RStudio/tidyverse 的菜鸟。
使用 R 3.4.0 "You Stupid Darkness" / RStudio 1.0.136。
尝试格式化 y 轴以显示千位小数点后一位。
我在用着 :
scale_y_continuous(labels = scales::unit_format("k", 1e-3))
但显示为整数。如何显示 1 位小数,而不是 30k,我得到 30.1k?
感谢
如果您需要更灵活的东西,我建议使用您自己的自定义函数并将其插入 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)
根据?scales::unit_format
,可以设置accuracy = 0.1
:
scale_y_continuous(labels = scales::unit_format(
unit = "k",
scale = 1e-3,
accuracy = 0.1))
完全成为 R/RStudio/tidyverse 的菜鸟。
使用 R 3.4.0 "You Stupid Darkness" / RStudio 1.0.136。
尝试格式化 y 轴以显示千位小数点后一位。
我在用着 :
scale_y_continuous(labels = scales::unit_format("k", 1e-3))
但显示为整数。如何显示 1 位小数,而不是 30k,我得到 30.1k?
感谢
如果您需要更灵活的东西,我建议使用您自己的自定义函数并将其插入 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)
根据?scales::unit_format
,可以设置accuracy = 0.1
:
scale_y_continuous(labels = scales::unit_format(
unit = "k",
scale = 1e-3,
accuracy = 0.1))