r - 如何将文本粘贴到 R 中 ggplot2 图中的数字 y 轴刻度上?
问题描述
例如,使用任意数据,如果您运行以下命令,您会注意到 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
with paste
orstr_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')))
推荐阅读
- c# - 如何在我的 startup.cs 中使用 appsettings.json 中的值?- 不实例化但实际使用该值
- c# - 如果它包含在列表中,我如何才能从开关盒中专门添加一个项目
- r - a,根据字符向量中的元素删除重复项
- c# - IApplicationBuilder.Map 破坏了 UseSpa 的功能(未找到捆绑包)
- android - AndroidViewModel 无法创建实例 Kotlin
- ios - 从自定义 swift 文件导入动画功能失败
- google-apps-script - 使用时间戳过滤 gmail
- javascript - 如何在 pino.js 记录器中旋转日志文件
- c# - C# BitConverter.ToInt64 - 目标数组不够长
- ubuntu - 树莓派 4 上 Ubuntu 20.10 上的 .net Core 控制台应用程序在尝试执行程序时给出 No such file or directory