R语言22-绘制百分比图

主要有两种方法:
1.ggplot,包裹器里添加fill
2.使用ggbarstats,会显示占比

library(ggplot2) #加载ggplot2包
library(dplyr) #加载dplyr包
library(ggstatsplot) #加载ggstatsplot包

1.包裹器里添加fill函数,使其等于要对比的变量

#使用ggplot包的geom_bar函数
ggplot(aes(x=InquiriesLast6Months,fill=newLoanStatus),
       data=subset(data,!(is.na(data$InquiriesLast6Months)|
                            is.na(data$newLoanStatus))))+
  geom_bar(position = position_fill())+
  scale_x_continuous(limits =c(-1,11))+ylab("Propotion") 

在这里插入图片描述
2.使用ggbarstats
使用filter过滤data

data <- filter(data, !(is.na(data$InquiriesLast6Months)|
                            is.na(data$newLoanStatus)))
#使用ggstatsplot的ggbarstats函数
data %>%
	ggbarstats(main = InquiriesLast6Months, condition =newLoanStatus,
           bar.proptest = F, #不显示p值的显著与否
           palette = 'Set3',#设置颜色板
           results.subtitle = F #副标题不显示统计结果
            ) +
	 coord_flip() #旋转坐标轴

是从这篇文章里面学习的
指路:https://blog.csdn.net/weixin_42933967/article/details/96200319
他使用的diamonds数据集,方便实践

发布了28 篇原创文章 · 获赞 0 · 访问量 413

猜你喜欢

转载自blog.csdn.net/xiuxiuxiu666/article/details/104245376