交错和堆叠geom_bar在同一图中? [英] Staggered and stacked geom_bar in the same figure?

查看:123
本文介绍了交错和堆叠geom_bar在同一图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的图表,它基本上是两个分布的直方图,彼此并排:

  my.barplot< - 函数(df,title =,...){
df.count< - aggregate(df $ outcome,by = list(df $ category1,df $ outcome),FUN = length)
colnames(df.count)< -c(category1,outcome,n)
df.total< - aggregate(df.count $ n,by = list(df.count $ category1),FUN = sum)
colnames(df.total)< -c(category1,total)
df.dens< - merge(df.count,df.total )
df.dens $ dens < - with(df.dens,n / total)
p < - ggplot(df.dens,aes(x =结果,填充=类别1),... )
p <-p + geom_bar(aes(y = dens),position =dodge)
p <-p + opts(axis.text.x = theme_text(angle = -90,hjust = 0),title = title)
p
}

N < - 50 *(2 * 8 * 2)
结果< - sample(ordered seq(8)),N,replace = TRUE,prob = c(seq(4)/ 20,rev(seq(4)/ 20)))
category2 < - ifelse (C( 是, 不是 ),prob = c(.95,.05)),sample(c(yes,not),prob = c(.35,.65)))
dat < - data.frame (
category1 = rep(c(in,out),each = N / 2),
category2 = category2,
outcome = results


my.barplot(dat)



我想在每个小节中绘制属于某个第二类的比例。如果没有按照第一类来组织它的需要,我只会堆叠酒吧。但是,我无法弄清楚如何通过第二类进行堆叠。基本上,在每个结果类别1栏中,我希望category2中的比例为较暗的阴影。



以下是我试图创建的GIMP图像: p>

解决方案

基础图形?!? NEVERRRR



这是我想到的。我承认我很难理解你所有的聚合和准备,所以我只是聚集在一起,可能已经得到了全部的错误 - 但是看起来你可能更容易从一个有效的情节开始,然后获得正确的输入。这是否有诀窍?

 #汇总
dat.agg< - ddply(dat,.var = c (类别1,结果),.fun =汇总,
cat1.n =长度(结果),
是=总计(类别2%,%是),
不是=总数(类别2%以%表示不)



#图表 - 两个图层的结果都是x
ggplot(dat.agg,aes( x =结果))+

#第一层酒吧 - 对于类别1总计的结果
geom_bar(aes(weight = cat1.n,fill = category1),position =dodge) +

#第二层酒吧 - 结果和类别1的是数量
geom_bar(aes(weight = yes,fill = category1),position =dodge)+

#透明度使总数小于是 - 我的颜色很差
scale_fill_manual(value = c(alpha(#1F78B4,0.5),alpha(#33A02C,0.5 ))+

#标题
opts(title =一个漂亮的阴谋3)


I have the following graph, which is essentially two distributions' histograms plotted alongside each other:

my.barplot <- function( df, title="", ... ) {
  df.count <- aggregate( df$outcome, by=list(df$category1,df$outcome), FUN=length )
  colnames( df.count ) <- c("category1","outcome","n")
  df.total <- aggregate( df.count$n, by=list(df.count$category1), FUN=sum )
  colnames( df.total ) <- c("category1","total")
  df.dens <- merge(df.count, df.total)
  df.dens$dens <- with( df.dens, n/total )
  p <- ggplot( df.dens, aes( x=outcome, fill=category1 ), ... )
  p <- p + geom_bar( aes( y=dens ), position="dodge" )
  p <- p + opts( axis.text.x=theme_text(angle=-90,hjust=0), title=title )
  p
}

N <- 50*(2*8*2)
outcome <- sample(ordered(seq(8)),N,replace=TRUE,prob=c(seq(4)/20,rev(seq(4)/20)) )
category2 <- ifelse( outcome==1, sample(c("yes","not"), prob=c(.95,.05)), sample(c("yes","not"), prob=c(.35,.65)) )
dat <- data.frame(
  category1=rep(c("in","out"),each=N/2),
  category2=category2,
  outcome=outcome
  )

my.barplot(dat)

I'd like to plot within each bar the proportion belonging to some second category. Absent the need to organize it by the first category, I would just stack the bars. However, I can't figure out how to stack by a second category. Basically within each outcome-category1 bar I want the proportion in category2 to be darker shaded.

Here's a GIMP'd image of what I'm trying to create:

解决方案

Base graphics?!? NEVERRRR

Here's what I've come up with. I admit I had a hard time understanding all your aggregation and prep, so I just aggregated to counts and may have gotten that all wrong - but it seems like you're in a position where it might be easier to start from a functioning plot and then get the inputs right. Does this do the trick?

# Aggregate
dat.agg <- ddply(dat, .var = c("category1", "outcome"), .fun = summarise,
                 cat1.n = length(outcome),
                 yes = sum(category2 %in% "yes"),
                 not = sum(category2 %in% "not")
)


# Plot - outcome will be x for both layers
ggplot(dat.agg, aes(x = outcome)) +

    # First layer of bars - for category1 totals by outcome
    geom_bar(aes(weight = cat1.n, fill = category1), position = "dodge") +

    # Second layer of bars - number of "yes" by outcome and category1
    geom_bar(aes(weight = yes, fill = category1), position = "dodge") +

    # Transparency to make total lighter than "yes" - I am bad at colors
    scale_fill_manual(value = c(alpha("#1F78B4", 0.5), alpha("#33A02C", 0.5))) +

    # Title
    opts(title = "A pretty plot <3")

这篇关于交错和堆叠geom_bar在同一图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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