在 ggplot2 上使用百分比刻度时如何设置中断?

How do I set breaks when using a percentage scale on ggplot2?

我正在尝试手动输入轴标签上的中断应该用于我的图形——即每 10 个百分点、每 25 个百分点等,而不是让 ggplot 自动计算它们。

ggplot(cars, aes(x=speed)) +
  geom_histogram(binwidth=.1, colour="white", fill="dark green", aes(y = (..count..)/sum(..count..))) + 
scale_x_continuous(labels=percent) +
scale_y_continuous(labels=percent) 

这是一种随机数据,但展示了基本思想——我希望能够在以下位置设置这些数据:

scale_x_continuous(labels=percent)

与使用此代码编写每 x 个单位应该有一个中断的方式相同:

scale_x_continuous(breaks=seq(0,2, by=.1))

有人有什么建议吗?

类似于:

ggplot(cars, aes(x=speed)) +
  geom_histogram(binwidth=.1, colour="white", fill="dark green", aes(y = (..count..)/sum(..count..))) + 
  scale_x_continuous(breaks= seq(0,1,.1)*max(cars$speed), labels = seq(0,100,10)) + labs(x = "speed (%)")

breaks参数和labels参数不互斥。前者用于确定中断的位置,后者用于确定中断的位置 formatted/displayed。这样你就可以

scale_x_continuous(breaks = seq(0, 2, by = 0.1), labels = percent)

指定两个方面。