本文介绍了在ggplot中显示酒吧的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! M4 = matrix(sample(1:2,20) * 5,replace = TRUE),20,5) M4< - as.data.frame(M4) M4 $ id< - 1:20 我使用下面的代码制作了一个堆积条形图 library(reshape) library(ggplot2) library(scales) M5 M5 $ value < - as.factor(M5 $ value) ggplot(M5,aes(x = variable))+ geom_bar(aes(fill = value),position ='fill')+ scale_y_continuous = percent_format()) 现在我想要显示每个栏中每个字段的百分比图表,以便每个小节达到100%。我已尝试 1 a>, 2 , 3 和几个类似的问题,但我找不到适合我的情况的任何示例。我怎样才能管理这个任务? 解决方案您可以使用中的 sjp.stackfrq href =http://cran.r-project.org/web/packages/sjPlot/index.html =nofollow noreferrer> sjPlot-package (参见 M4 =矩阵(样本(1:2,20 * 5,替换= TRUE),20,5) M4 sjp.stackfrq(M4) #替代颜色:sjp.stackfrq(M4,barColor = c(aquamarine4,brown3)) 地块外观可以被各种参数定制... I have a dataset with binary variables like the one below.M4 = matrix(sample(1:2,20*5, replace=TRUE),20,5)M4 <- as.data.frame(M4)M4$id <- 1:20I have produced a stacked bar plot using the code belowlibrary(reshape)library(ggplot2)library(scales)M5 <- melt(M4, id="id")M5$value <- as.factor(M5$value)ggplot(M5, aes(x = variable)) + geom_bar(aes(fill = value), position = 'fill') + scale_y_continuous(labels = percent_format())Now I want the percentage for each field in each bar to be displayed in the graph, so that each bar reach 100%. I have tried 1, 2, 3 and several similar questions, but I can't find any example that fits my situation. How can I manage this task? 解决方案 You could use the sjp.stackfrq function from the sjPlot-package (see examples here).M4 = matrix(sample(1:2,20*5, replace=TRUE),20,5)M4 <- as.data.frame(M4)sjp.stackfrq(M4)# alternative colors: sjp.stackfrq(M4, barColor = c("aquamarine4", "brown3"))Plot appearance can be custzomized with various parameters... 这篇关于在ggplot中显示酒吧的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-19 04:04