我有以下代码:
library(ggplot2) ggplot(cars, aes(x = speed, y = dist)) + geom_line()
其中创建了以下情节
我想要做的是将x轴(speed
)值替换5,10,15,20,25
为以下向量:c("-5000", "-2500", "FOO", "2500", "5000")
.
但为什么这会失败?
ggplot(cars, aes(x = speed, y = dist)) + geom_line() + scale_x_discrete(labels = c("-5000", "-2500", "TSS", "2500", "5000"))
它产生了这个:
什么是正确的方法呢?
您应该使用scale_x_continuous()
因为速度是一个连续变量.您还需要指定中断,即在哪里放置刻度.
ggplot(cars, aes(x = speed, y = dist)) + geom_line() + scale_x_continuous(breaks = c(5, 10, 15, 20, 25), labels = c("-5000", "-2500", "TSS", "2500", "5000"))