r - 在特定的 ggplots 中添加一个百分比符号来标记,而不是在其他图中
问题描述
我有一个闪亮的应用程序,有多个 ggplot,很少显示百分比标签其他四舍五入的数字。所有都由创建 ggplot 的相同函数返回。percent_flag
作为参数传递给相同的函数。我尝试了以下逻辑,但它不起作用。我没有选择创建额外的列来满足条件。
geom_text(aes(label = ifelse(percent_flag == 1,
paste(round(BIAS_contribution, 2), "%", sep = ""),
comma(BIAS_contribution)),
y = (start + end) / 2)
解决方案
我建议使用基本的 if 子句:
library(ggplot2)
percent_flag <- 1
# base plot
p <- iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width))
# with percent flag
if(percent_flag == 1){
p2 <- p +
geom_text(aes(label = paste0(Petal.Width, "%")))
# withput percent flag
} else {
p2 <- p +
geom_text(aes(label = Petal.Width))
}
p2
推荐阅读
- javascript - 验证提示条目是否是 JS 中的数字的最佳方法是什么?
- php - WordPress 模板中使用的嵌套 If else 语句不起作用
- azure-data-factory - 如何迭代 azure 数据工厂中 excel 文件中存在的所有 excel 工作表
- javascript - JS:总是得到空数组?
- swift - 在 viewCell,UIViewCollection 中使按钮可点击
- django - Google App Engine GitHub 操作:错误:位置 0 的 JSON 中的意外令牌
- clang - clang 不生成调试符号
- java - Junit 使用 coffeeMaker 练习测试用户输入
- go - 使用 Go iamcredentials 客户端 API 生成 Google API 访问令牌时出现 400 错误请求
- windows - 使用 Tortoisegit 在 C 盘中的 Commit Window 上“请稍候...”阶段需要很长时间