例如,使用任意数据,如果您运行以下命令,您会注意到 y 轴刻度为 2、4、6 和 8:
ggplot(iris, aes(Species)) +
geom_line(aes(y = Sepal.Length, colour = "Sepal.Length"), size=1.25) +
geom_line(aes(y = Sepal.Width, colour = "Sepal.Width"), size=1.25)
如何让 y 轴刻度改为显示 2x、4x、6x 和 8x?
最佳答案
我们可以在 scale_y_continous
中使用 paste
或 str_c
传递一个函数
library(ggplot2)
library(stringr)
ggplot(iris, aes(Species)) +
geom_line(aes(y = Sepal.Length, colour = Sepal.Length), size=1.25) +
geom_line(aes(y = Sepal.Width, colour = Sepal.Width), size=1.25) +
scale_y_continuous(labels = function(x) str_c(x, 'x'))
# // or use as_mapper
# scale_y_continuous(labels = purrr::as_mapper(~ str_c(., 'x')))
关于r - 如何将文本粘贴到 R 中 ggplot2 图形的数字 y 轴刻度中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66431963/