首页 > 解决方案 > R:ggplot自定义变换轴标签以减少零

问题描述

在我的以下代码中:

library(ggplot2) 
library(scales)
myData <- data.frame(
  platform = c("Windows", "MacOs", "Linux"),
  number = c(27000, 16000, 9000)
)

ggplot(myData, aes(x = reorder(platform, -number), y = number))+ 
  geom_bar(stat="identity", fill="darkturquoise", width = 0.5)+
  geom_text(aes(label = number), vjust=-0.3)+
  xlab("Platform")+
  scale_y_continuous(breaks = round(seq(0,40000, by = 5000), 1))

产生这个情节: 在此处输入图像描述

如何更改参数scale_y_continuous以减少数量000?即,y 刻度将显示 5、10、15、20、25...

标签: rggplot2

解决方案


将 y 轴的标签除以 1000,如下所示:

ggplot(myData, aes(x = reorder(platform, -number), y = number))+ 
  geom_bar(stat="identity", fill="darkturquoise", width = 0.5)+
  geom_text(aes(label = number), vjust=-0.3)+
  xlab("Platform")+
  scale_y_continuous(breaks = seq( 0,40000, by = 5000),
                     labels = function(y_value) y_value / 1000)  # <- ! here !

推荐阅读