R barplot旁边和堆叠

时间:2017-11-07 16:28:14

标签: r ggplot2 bar-chart

我尝试创建一个条形图,它同时使用并堆叠。我有一些类似于我想要的方面:

Similar to the searched feature

tmp <- morley
tmp$loc <- paste("No", tmp$Run %/% 2, sep="")
tmp$group <- as.logical(tmp$Run %% 2)
tmp$year <- tmp$Expt + 2000
tmp$value <- tmp$Speed
tmp <- subset(tmp, loc %in% c("No1", "No2", "No3"))

ggplot(tmp, aes(x=loc, y=value, fill=group)) + 
  geom_bar(stat ='identity', position ='stack') + 
  facet_grid (~year)

我希望没有方面和2个传说(绿色:No1,红色:No2,蓝色:No3和TRUE:0%透明度,FALSE 40%透明度)x轴上的年份,旁边的位置和群体堆积。还有6个条目的传说No1为真,No1为假,No2为真......也没关系。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:3)

ggplot(tmp, aes(x=loc, y=value, fill=loc, alpha=group)) + 
  geom_bar(stat ='identity', position ='stack') +
  scale_alpha_manual(values=c('TRUE'=1, 'FALSE'=0.6)) +
  facet_grid(~year, switch='x') +
  theme(axis.title.x=element_blank(), strip.placement='outside')

然后调整主题并适当填充比例。

enter image description here