r - 如何将文本粘贴到 R 中 ggplot2 图形的数字 y 轴刻度中?

标签 r ggplot2

例如,使用任意数据,如果您运行以下命令,您会注意到 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 中使用 pastestr_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/

相关文章:

R笔记本: opts_chunk has no effect

r - 带对数刻度的条形图

r - 错误栏末端缺失

r - 如何将错误和警告写入日志文件?

r - 如何输出数据框中行范围内的最大值?

r - 摆脱那些灰色框上的 facet_grid 标签?

r - ggplot XY 散点图 - 如何更改选定点的 alpha 透明度?

r - 减小树状图(或 y 轴)ggplot 的大小

r - 如何更改绘图中的轴特征?

r - 在包中包含一个 "Hash Table"