我有以下图表,它基本上是两个分布的直方图并排绘制:
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)
Run Code Online (Sandbox Code Playgroud)

我想在每个栏中绘制属于某些第二类的比例.如果不需要按照第一类来组织它,我只会堆叠条形图.但是,我无法弄清楚如何按第二类堆叠.基本上在每个结果 - 类别1栏中,我希望类别2中的比例为更深的阴影.
这是我正在尝试创建的GIMP图像:

基础图形?!?NEVERRRR
这就是我想出来的.我承认我很难理解你的所有聚合和准备工作,所以我只是把它归结为计数而且可能已经完全错了 - 但似乎你处在一个可能更容易从一个有效的情节开始的位置然后得到正确的输入.这样做有用吗?
# 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")
Run Code Online (Sandbox Code Playgroud)
