如何更好地创建来自ggplot2的多个变量的堆积条形图? [英] How to better create stacked bar graphs with multiple variables from ggplot2?

查看:136
本文介绍了如何更好地创建来自ggplot2的多个变量的堆积条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常需要制作堆叠的镂空来比较变量,并且因为我在R中完成了所有的统计数据,所以我更愿意使用ggplot2在R中完成所有图形。我想了解如何做两件事:首先,我希望能够为每个变量添加适当的百分比刻度标记,而不是通过count添加刻度标记。计数会令人困惑,这就是为什么我完全取出轴标签的原因。第二,必须有一种更简单的方法来重组我的数据以实现此目的。这似乎是我应该能够用gplot2在plyR本地执行的事情,但plyR的文档不是很清楚(我已经阅读了ggplot2书籍和在线plyR文档。)

我的最佳图形如下所示,创建它的代码如下所示:



我使用的R代码如下所示:

  library(epicalc)

###将因子重新编码###
recode(c(int_newcoun ,int_newneigh,int_neweur,int_newusa,int_neweco,int_newit,int_newen,int_newsp,int_newhr,int_newlit,int_newent,int_newrel,int_newhth,int_bapo,int_wopo,int_eupo,int_educ),c(1,2,3,4,5,6,7, 8,9,NA),
c('非常感兴趣','有点感兴趣','不很感兴趣','根本不感兴趣',NA,NA,NA,NA,NA,NA))

###将重新编码的变量组合到一个公共矢量
Inte rest1< -c(int_newcoun,int_newneigh,int_neweur,int_newusa,int_neweco,int_newit,int_newen,int_newsp,int_newhr,int_newlit,int_newent,int_newrel,int_newhth,int_bapo,int_wopo,int_eupo,int_educ)


###创建第二个向量,用原始变量###
a1 <-rep(News about Bangladesh,length(int_newcoun))
a2 <-rep(Neighboring国家,长度(int_newneigh))
[...]
a17 <-rep(Education,长度(int_educ))


兴趣2 < - c(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17)

###创建一个权重适当长度的矢量###
Interest.weight< -rep(weight,17)

###从这三个向量中创建并保存一个新的数据帧###
Interest.df< -cbind(Interest1,Interest2,Interest.weight)
Interest.df< -as.data.frame(Interest.df)

write.csv(Interest。 df,'C:\\ Documents and Settings \\ [name] \\Deskto p \\Sweave\\\InterestBangladesh.csv')

###对因子水平进行排序以正确显示###

Interest.df $ Interest1< (利息$ Interest1,ref ='不是很感兴趣')
Interest.df $ Interest1 <-relevel(Interest $ Interest1,ref ='有点感兴趣')
Interest.df $ Interest1 < - (利息$ Interest1,ref ='非常感兴趣')

Interest.df $ Interest2 <-relevel(利息$ interest2,ref ='关于孟加拉国的消息')
Interest.df $ Interest2< -relevel(Interest $ Interest2,ref ='Education')

Interest.df $ Interest2 <-relevel(Interest $ Interest2,ref ='European Politics')

detach(Interest)
attach(Interest)

###最后在ggplot2中创建图###

library(ggplot2)
p <-ggplot(interest,aes(Interest2,..count ..))
p <-p + geom_bar((aes(weight = Interest.weight,fill = Interest1)))
p <-p + coord_flip()
p <-p + scale_y_continuous(,breaks = NA)
p <-p + scale_fill_manual(value = rev(brewer.pal(5,Purples)))
p
update_labels(p,list(fill ='',x ='', y =''))

我非常感谢任何提示,技巧或提示。

解决方案

您不需要 prop.tables 做100%堆积的酒吧。你只需要 + geom_bar(position =stack)


I often have to make stacked barplots to compare variables, and because I do all my stats in R, I prefer to do all my graphics in R with ggplot2. I would like to learn how to do two things:

First, I would like to be able to add proper percentage tick marks for each variable rather than tick marks by count. Counts would be confusing, which is why I take out the axis labels completely.

Second, there must be a simpler way to reorganize my data to make this happen. It seems like the sort of thing I should be able to do natively in ggplot2 with plyR, but the documentation for plyR is not very clear (and I have read both the ggplot2 book and the online plyR documentation.

My best graph looks like this, the code to create it follows:

The R code I use to get it is the following:

library(epicalc)  

### recode the variables to factors ###
recode(c(int_newcoun, int_newneigh, int_neweur, int_newusa, int_neweco, int_newit, int_newen, int_newsp, int_newhr, int_newlit, int_newent, int_newrel, int_newhth, int_bapo, int_wopo, int_eupo, int_educ), c(1,2,3,4,5,6,7,8,9, NA), 
c('Very Interested','Somewhat Interested','Not Very Interested','Not At All interested',NA,NA,NA,NA,NA,NA))

### Combine recoded variables to a common vector
Interest1<-c(int_newcoun, int_newneigh, int_neweur, int_newusa, int_neweco, int_newit, int_newen, int_newsp, int_newhr, int_newlit, int_newent, int_newrel, int_newhth, int_bapo, int_wopo, int_eupo, int_educ)


### Create a second vector to label the first vector by original variable ###  
a1<-rep("News about Bangladesh", length(int_newcoun))
a2<-rep("Neighboring Countries", length(int_newneigh))
[...]
a17<-rep("Education", length(int_educ))


Interest2<-c(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17)

### Create a Weighting vector of the proper length ###
Interest.weight<-rep(weight, 17)

### Make and save a new data frame from the three vectors ###
Interest.df<-cbind(Interest1, Interest2, Interest.weight)
Interest.df<-as.data.frame(Interest.df)

write.csv(Interest.df, 'C:\\Documents and Settings\\[name]\\Desktop\\Sweave\\InterestBangladesh.csv')

### Sort the factor levels to display properly ###

Interest.df$Interest1<-relevel(Interest$Interest1, ref='Not Very Interested')
Interest.df$Interest1<-relevel(Interest$Interest1, ref='Somewhat Interested')
Interest.df$Interest1<-relevel(Interest$Interest1, ref='Very Interested')

Interest.df$Interest2<-relevel(Interest$Interest2, ref='News about Bangladesh')
Interest.df$Interest2<-relevel(Interest$Interest2, ref='Education')
[...]
Interest.df$Interest2<-relevel(Interest$Interest2, ref='European Politics')

detach(Interest)
attach(Interest)

### Finally create the graph in ggplot2 ###

library(ggplot2)
p<-ggplot(Interest, aes(Interest2, ..count..))
p<-p+geom_bar((aes(weight=Interest.weight, fill=Interest1)))
p<-p+coord_flip()
p<-p+scale_y_continuous("", breaks=NA)
p<-p+scale_fill_manual(value = rev(brewer.pal(5, "Purples")))
p
update_labels(p, list(fill='', x='', y=''))

I'd very much appreciate any tips, tricks or hints.

解决方案

You don't need prop.tables or count etc to do the 100% stacked bars. You just need +geom_bar(position="stack")

这篇关于如何更好地创建来自ggplot2的多个变量的堆积条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆