在y轴标签上添加百分比

时间:2018-05-31 15:11:44

标签: r ggplot2 axis-labels

我确定我错过了一个明显的问题解决方案,但我无法弄清楚如何在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)

enter image description here

我不确定是否可以使用sprintf添加它,或者是否可以将其粘贴到标签上。

1 个答案:

答案 0 :(得分:0)

+ scale_y_continuous(labels = function(x) paste0(x, "%"))添加到ggplot语句可修复此问题

相关问题