简体   繁体   English

如何将文本粘贴到 R 中 ggplot2 数字中的数字 y 轴刻度上?

[英]How can I paste text onto numerical y-axis ticks in ggplot2 figures in R?

Using arbitrary data for instance, if you run the following, you notice the y-axis ticks are 2, 4, 6, and 8:例如,使用任意数据,如果您运行以下命令,您会注意到 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) 

How can I make the y-axis ticks instead display 2x, 4x, 6x, and 8x?如何使 y 轴刻度改为显示 2x、4x、6x 和 8x?

We can pass a function in scale_y_continous with paste or str_c我们可以使用paste或 str_c 在scale_y_continous中传递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')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM