r - 向 y 轴标签添加百分比
问题描述
我确定我错过了一个明显的解决方案来解决他的问题,但我不知道如何在 y 轴标签上添加百分号。数据样本:
Provider Month Total_Count Total_Visits Procedures RX State
Roberts 2 19 19 0 0 IL
Allen 2 85 81 4 4 IL
Dawson 2 34 34 0 0 CA
Engle 2 104 100 4 4 CA
Goldbloom 2 7 6 1 1 NM
Nathan 2 221 192 29 20 NM
Castro 2 6 6 0 0 AK
Sherwin 2 24 24 0 0 AK
Brown 2 282 270 12 12 UT
Jackson 2 114 96 18 16 UT
Corwin 2 22 22 0 0 CO
Dorris 2 124 102 22 22 CO
Ferris 2 427 318 109 108 OH
Jeffries 2 319 237 82 67 OH
以下代码给出了值不准确的图形,因为 R 似乎乘以 100。
procs <- read.csv(paste0(dirdata, "Procedure percents Feb.csv"))
procs$Percentage <- round(procs$Procedures/procs$Total.Visits*100, 2)
procs$Percentage[is.na(procs$Percentage)] <- 0
procsplit <- split(procs, procs$State)
plots <- function(procs) {
ggplot(data = procs, aes(x= Provider, y= Percentage, fill= Percentage)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(x = Provider, y = Percentage, label = sprintf("%.1f%%", Percentage)), position = position_dodge(width = 0.9), hjust = .5, vjust = 0 , angle = 0) +
theme(axis.text.x = element_text(angle = 45, vjust = .5)) +
ggtitle("Procedure Percentages- February 2018", procs$State) +
theme(plot.title = element_text(size = 22, hjust = .5, family = "serif")) +
theme(plot.subtitle = element_text(size = 18, hjust = .5, family = "serif")) +
scale_y_continuous(name = "Percentage", labels = percent)
}
lapply(procsplit, plots)
我不确定是否有办法使用 sprintf 添加它,或者是否有办法将它粘贴到标签上。
解决方案
添加+ scale_y_continuous(labels = function(x) paste0(x, "%"))
到 ggplot 语句可解决此问题
推荐阅读
- python - 添加散景滑块以按年份可视化 GIS 数据
- angular - 我们可以在静态服务器上移动 node_modules 来构建 Angular 应用程序吗?
- php - 从 INSERT 语句返回 id 时 PDO::fetch 很慢
- python - 如何用按钮制作一个 tkinter 弹出窗口,等待用户点击?
- javascript - 在 JSON.parse 之后检索类原型
- sql - SQL 在数据子集上连接两个表
- c# - Windows 7 上的相机录制失败
- android - ImageView 调整大小并保持纵横比
- javascript - 如何在点击时绘制点并将它们与线条 HTML 连接起来
- ios - 我如何从 UIView 转到 UIViewController 故事板