ggplot2中的多个直方图 [英] Multiple histograms in ggplot2

查看:877
本文介绍了ggplot2中的多个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据的一小部分:

pre $ dat <-structure(list(sex = structure(c( 1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L) 男,
女),class =factor),A = c(1,2,0,2,1,2,2,0,2,
0,1, 2,2,0,0,2,0,0,0,2),B = c(0,0,0,0,0,2,0,
0,1,1,0,0 ,0,0,1,1,0,0,0),C = c(1,2,1,0,0,
2, 1,2,1,2,0,2,1,2),D = c(2,2,0,
2,2,2,1,0,1,1,1,0,1 ,2,0,0,1,1,1,0),E = c(0,
0, 2,0,0,1,2,2),F = c(2,
2,1,2,1,2,2,0,1,2,0,1,2,2 ,0,1,2,2,2,2)),.Names = c(sex,
A,B,C,D,E,F ),variable.labels = structure(c(sex,
zenuwac,panieke,gespann,rustelo,angstig,onzeker
),.Names = c(sex,anx01,anx02,anx03,anx04,anx05,
anx06)),codepage = 20127L,row.names = c(NA,20L ),class =data.frame )

一个数据框,包含六个三点变量的男性和女性得分。现在我想创建一个图表,显示网格中男性和女性的每个变量的分数的直方图。例如,我可以这样做:

  layout(matrix(1:12,6,2,byrow = TRUE))
par(mar = c(2,1,2,1))
for(in in 1:6)for(s in c(male,female))hist(dat [dat $性别== s,i + 1],main = paste(item,names(dat)[i + 1],s))

其结果如下:





我可以使这看起来更好,但我更有兴趣学习如何使用ggplot2。所以我的问题是,如何使用ggplot2创建一个漂亮的版本?我工作的一件事是:

$ p $ library(ggplot2)
grid.newpage()
(1:2)
($ 1 $ 6)
{(1:6)
{viewport(layout = grid.layout(6,2)))
p <-qplot(dat [dat $ sex == c(male,female)[s],i + 1] +0.5,geom =histogram,binwidth = 1)
print(p,vp = viewport(layout.pos.row = i,layout.pos.col = s))
}
}

但是我想有一个更简单的方法可以做到这一点?

您可以从 grid.arrange() .htmlrel =nofollow noreferrer> gridExtra 包;即将您的图存储在列表中(例如 qplt ),并使用

  do.call(grid.arrange,qplt)

其他想法:在ggplot2中使用facetting( sex * variable ),通过考虑data.frame(使用 melt )。

作为旁注,最好使用堆叠条形图或Cleveland的点图来显示项目响应频率,IMO。 (我对 CrossValidated 提出了一些建议。)






为了完整起见,以下是一些实现方案:

 #simple barchart 
ggplot(melt(dat),aes(x = as.factor(value),fill = as.factor(value)))+
geom_bar ()+ facet_grid(variable〜sex)+ xlab()+ coord_flip()+
scale_fill_discrete(Response)

  my.df < -  ddply(melt(dat),c(sex,variable),summary,
count = table(value))
my.df $ resp< - gl(3,1,length = nrow(my.df),labels = 0:2)

#堆叠条形图
ggplot( my.df,aes(x = variable,y = count,fill = resp))+
geom_bar()+ facet_wrap(〜sex)+ coord_flip()
pre>


 #dotplot 
ggplot(my.df ,aes(x = count,y = resp,color = sex))+ geom_point()+
facet_wrap(〜变量)


Here is a short part of my data:

dat <-structure(list(sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("male", 
"female"), class = "factor"), A = c(1, 2, 0, 2, 1, 2, 2, 0, 2, 
0, 1, 2, 2, 0, 0, 2, 0, 0, 0, 2), B = c(0, 0, 0, 0, 0, 2, 0, 
0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0), C = c(1, 2, 1, 0, 0, 
2, 1, 1, 0, 1, 1, 0, 1, 2, 1, 2, 0, 2, 1, 2), D = c(2, 2, 0, 
2, 2, 2, 1, 0, 1, 1, 1, 0, 1, 2, 0, 0, 1, 1, 1, 0), E = c(0, 
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 2, 2), F = c(2, 
2, 1, 2, 1, 2, 2, 0, 1, 2, 0, 1, 2, 2, 0, 1, 2, 2, 2, 2)), .Names = c("sex", 
"A", "B", "C", "D", "E", "F"), variable.labels = structure(c("sex", 
"zenuwac", "panieke", "gespann", "rustelo", "angstig", "onzeker"
), .Names = c("sex", "anx01", "anx02", "anx03", "anx04", "anx05", 
"anx06")), codepage = 20127L, row.names = c(NA, 20L), class = "data.frame")

A data frame with scores of males and females on six 3-point variables. Now I want to create a plot which shows the histograms of the scores of each variable of both males and females in a grid. For example, I can do:

layout(matrix(1:12,6,2,byrow=TRUE))
par(mar=c(2,1,2,1))
for (i in 1:6) for (s in c("male","female")) hist(dat[dat$sex==s,i+1],main=paste("item",names(dat)[i+1],s))

which results in:

I could make this look better but I am more interested in learning how to use ggplot2. So my question is, how do I create a pretty version of this using ggplot2? One thing I got working is:

library("ggplot2")
grid.newpage()
pushViewport(viewport(layout = grid.layout(6, 2)))   
for (s in 1:2)
{
    for (i in 1:6)
    {
        p <- qplot(dat[dat$sex==c("male","female")[s],i+1]+0.5, geom="histogram", binwidth=1)
        print(p, vp = viewport(layout.pos.row = i, layout.pos.col = s))
    }
}

But I guess there is a much easier way to do this?

解决方案

You can try grid.arrange() from the gridExtra package; i.e., store your plots in a list (say qplt), and use

do.call(grid.arrange, qplt)

Other ideas: use facetting within ggplot2 (sex*variable), by considering a data.frame (use melt).

As a sidenote, it would be better to use stacked barchart or Cleveland's dotplot for displaying items response frequencies, IMO. (I gave some ideas on CrossValidated.)


For the sake of completeness, here are some implementation ideas:

# simple barchart
ggplot(melt(dat), aes(x=as.factor(value), fill=as.factor(value))) + 
  geom_bar() + facet_grid (variable ~ sex) + xlab("") + coord_flip() + 
  scale_fill_discrete("Response")

my.df <- ddply(melt(dat), c("sex","variable"), summarize, 
               count=table(value))
my.df$resp <- gl(3, 1, length=nrow(my.df), labels=0:2)

# stacked barchart
ggplot(my.df, aes(x=variable, y=count, fill=resp)) + 
  geom_bar() + facet_wrap(~sex) + coord_flip()

# dotplot
ggplot(my.df, aes(x=count, y=resp, colour=sex)) + geom_point() + 
  facet_wrap(~ variable)

这篇关于ggplot2中的多个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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