如何在已经堆叠的条形图中分组 (m/f)(丰度与时间序列)?

How to group (m/f) in an already stacked bar (abundance vs time series)?

我对 R 很陌生,并且设法使用 google 来使用 ggplot2。 ;) 我想 "stack-plot" 相对丰度与时间块 (1-8)。

现在的情节是怎样的:

现在说说我的目标和问题: 我有男性和女性的数据。我的目标是将每个时间块 m/f 分组在一起。所以每个时间序列 1 - 8 有 2 个堆叠的条形图(m 和 f),彼此相邻(不幸的是不能添加第二张图片)

下载link到数据(txt文件): https://www.wetransfer.com/downloads/559769b71aa32356457293161f5448f220161124101155/eb3b1f6c78d3145a1ad68d31a07e0c5c20161124101155/173f77

family_abundance<-read.table("family_abundance.txt", header=T)
ggplot(family_abundance, aes(x=row, y=value, fill=factor(variable))) 
        + geom_bar(stat="identity", )
        + scale_fill_manual(values=c("#523A00","#143952","#0B1E0B","#112D10","#163C16","#1C4B1B","#215A20","#2D782B","#389636","#7071b6","#390528","#4B0636","#5E0843","#710950","#970C6B","#BD0F86","#BD0F86","#E212A1","#F042B9","#5CC45A","#f7d899","#A26F3F","#C6986C","#32dcd0","#7071b6","#35bfd7","#faa756","#D4D125","#048c92","#bc94e3","#22776e","#f294d1","#c64b3f","#fac049","#491209","#A42913","#E54124","#7f8ba7","#2972A3","#EBFEF4","#c9aba5","#1f7366","#7A5800","#8F6600","#B88400","#D89A00","#FFBA0A","#A1C8E3","#B1ADA0","#996836","#58a56d","#f5a05f"))
        + xlab("Week") 
        + ylab("Abundance") 
        + facet_grid(. ~sex)

现在,我知道有 postion=dodge 并且我试过了。但是,它将所有堆叠的条形图分解为单独的条形图。我的想法是以某种方式告诉 dodge 只为性而做 (m/f)?但是我不知道该怎么做。

有人能帮帮我吗?

干杯 西欧

ggplot(mpg, aes(interaction(year, class))) + 
  geom_bar(aes(fill = drv), position = "stack")

ggplot(mpg, aes(as.factor(year))) + 
  geom_bar(aes(fill = drv), position = "stack") +
  facet_grid(~class, scales = 'free')

(我不想下载你的数据。)

你的数据应该是这样的(与 Axeman 的想法几乎相同):

ggplot(family_abundance, aes(x=interaction(sex,row), y=value, group=sex,fill=factor(variable))) +
  geom_bar(stat="identity")+
  facet_grid(.~row, scales = 'free')+
  scale_x_discrete("Week",labels=levels(family_abundance$sex))