Multiple histograms in ggplot2

若如初见. 提交于 2019-11-27 22:19:48
chl

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)

To follow up on chl's example - here's how to duplicate your base graphic with ggplot. I would heed his advice in looking to dotplots as well:

library(ggplot2)
dat.m <- melt(dat, "sex") 

ggplot(dat.m, aes(value)) + 
  geom_bar(binwidth = 0.5) + 
  facet_grid(variable ~ sex)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!