简体   繁体   English

R barplot旁边和堆叠

[英]R barplot beside and stacked

I try to create a barplot, that uses besides and stacked at the same time. 我尝试创建一个barplot,它使用另外并同时堆叠。 I have something that is similar to what I want with facet: 我有一些类似于我想要的方面:

与搜索功能类似

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)

I would want that without facets and 2 legends (Green: No1, Red: No2, Blue: No3 and TRUE: 0% transparency, FALSE 40% transparency) with the years on the x-axis, the locations beside and the groups stacked. 我想要没有方面和2个图例(绿色:No1,红色:No2,蓝色:No3和TRUE:0%透明度,FALSE 40%透明度),x轴上的年份,旁边的位置和堆叠的组。 Also a legend with 6 entries No1 true, No1 false, No2 true... would be okay. 另外一个传说有6个条目No1 true,No1 false,No2 true ......没关系。

Is there a way to do this? 有没有办法做到这一点?

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')

And then adjust themes and fill scale appropriately. 然后调整主题并适当填充比例。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM