R堆叠的频率直方图百分比,基于 [英] R stacked % frequency histogram with percentage of aggregated data based on
本文介绍了R堆叠的频率直方图百分比,基于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我相信我的问题与
这就是我走了多远
set.seed(123)
n = 100
LoanStatus = sample(c('Chargedoff', 'Completed', 'Current', 'Defaulted', 'PastDue'), n, replace = T, prob = NULL)
ProsperScore = sample(1:11, n, replace = T, prob = NULL)
df = data.frame(ProsperScore,factor(LoanStatus))
df = data.frame(ProsperScore,LoanStatus)
probs = data.frame(prop.table(table(df),1))
推荐答案
堆积条形图的代码如下所示:
Code for the stacked bar plot could look something like this:
library(ggplot2)
brks <- c(0, 0.25, 0.5, 0.75, 1)
ggplot(data=probs,aes(x=ProsperScore,y=Freq,fill=LoanStatus)) +
geom_bar(stat="identity") +
scale_y_continuous(breaks = brks, labels = scales::percent(brks)) +
scale_x_discrete(breaks = c(3,6,9))
更完整的代码,在这里演示了如何将百分比添加到绘图中:
More complete code, demonstrating how you would go about adding percentages to the plot, is here:
library(ggplot2)
library(plyr)
brks <- c(0, 0.25, 0.5, 0.75, 1)
probs <- probs %>% dplyr::group_by(ProsperScore) %>%
dplyr::mutate(pos=cumsum(Freq)-(Freq*0.5)) %>%
dplyr::mutate(pos=ifelse(Freq==0,NA,pos))
probs$LoanStatus <- factor(probs$LoanStatus, levels = rev(levels(probs$LoanStatus)))
ggplot(data=probs,aes(x=ProsperScore,y=Freq,fill=LoanStatus)) +
geom_bar(stat="identity") +
scale_y_continuous(breaks = brks, labels = scales::percent(brks)) +
scale_x_discrete(breaks = c(3,6,9)) +
geom_text(data=probs, aes(x = ProsperScore, y = pos,
label = paste0(round(100*Freq),"%")), size=2)
要仅在图表的第一列中显示百分比,请添加%&%;%dplyr :: mutate(pos = ifelse(ProsperScore == 1,pos,NA))
到 dplyr
调用.
To only show the percentages in the first column of the graph, add %>%
dplyr::mutate(pos=ifelse(ProsperScore==1,pos,NA))
to the dplyr
calls.
这篇关于R堆叠的频率直方图百分比,基于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文