双因素条形图

8

从以下数据框中工作:

    > foo
        species density day percent  
    1  species1    high   1    0.40  
    2  species1     low   1    0.20  
    3  species1  medium   1    0.40  
    4  species2    high   1    0.35  
    5  species2     low   1    0.10  
    6  species2  medium   1    0.55  
    7  species3    high   1    0.35  
    8  species3     low   1    0.20  
    9  species3  medium   1    0.45  
    10 species4    high   1    0.30  
    11 species4     low   1    0.20  
    12 species4  medium   1    0.50  
    13 species1    high 100    0.50  
    14 species1     low 100    0.40  
    15 species1  medium 100    0.10  
    16 species2    high 100    0.40  
    17 species2     low 100    0.05  
    18 species2  medium 100    0.55  
    19 species3    high 100    0.65  
    20 species3     low 100    0.05  
    21 species3  medium 100    0.30  
    22 species4    high 100    0.40  
    23 species4     low 100    0.20  
    24 species4  medium 100    0.40  

我已经创建了以下的分面条形图:
require(ggplot2)

foo$density<-factor(foo$density,levels=c('low','medium','high'))

d <- ggplot(foo, aes(x=species, y=percent, fill=density)) +
    geom_bar(aes(width=.65), stat="identity") +
    facet_grid(. ~ day)

enter image description here

然而,我想将这些图表合并成一个双因素条形图。在x轴上,每一天-1和100-都将按物种分组。有没有什么建议如何创建这个图表呢?

非常感谢!

2个回答

4

试试这个

foo$species_day <- with(data = foo, expr = paste(species, day))
d <-ggplot(foo, aes(x=species_day, y=percent, fill=density)) +
      geom_bar(aes(width=.65), stat="identity")

这里输入图片描述如果您愿意,您可以重新安排级别。


1
@joran 我喜欢按物种分面的想法,但是x轴标签的命名成了一个问题。我希望“物种”成为焦点x轴标签(在图形底部),并且“天数”设置在每个“物种”内部(也在底部)。 - Drosera aliciae
@MYaseen208 感谢您的建议-我喜欢它全部在一个图表上的方式。有没有办法在条形之间创建更多或更少的空间以按物种“分组”? - Drosera aliciae

1

这里有另一种方法,结合了@Joran的建议和您的要求。

我将day更改为因子,并将默认x轴标签更改为“物种”。此外,由于density显然是一个连续的因子,我使用了来自Color Brewer(http://colorbrewer2.org/)的连续颜色比例尺。您可以通过更改palette编号或按名称调用调色板scale_fill_brewer(palette="GnBu")来尝试不同的颜色比例尺。在Color Brewer网页上找到调色板名称。

foo <- read.table(header=TRUE,
                 text="species  density day percent  
                    1  species1    high   1    0.40  
                    2  species1     low   1    0.20  
                    3  species1  medium   1    0.40  
                    4  species2    high   1    0.35  
                    5  species2     low   1    0.10  
                    6  species2  medium   1    0.55  
                    7  species3    high   1    0.35  
                    8  species3     low   1    0.20  
                    9  species3  medium   1    0.45  
                    10 species4    high   1    0.30  
                    11 species4     low   1    0.20  
                    12 species4  medium   1    0.50  
                    13 species1    high 100    0.50  
                    14 species1     low 100    0.40  
                    15 species1  medium 100    0.10  
                    16 species2    high 100    0.40  
                    17 species2     low 100    0.05  
                    18 species2  medium 100    0.55  
                    19 species3    high 100    0.65  
                    20 species3     low 100    0.05  
                    21 species3  medium 100    0.30  
                    22 species4    high 100    0.40  
                    23 species4     low 100    0.20  
                    24 species4  medium 100    0.40")

foo$density <- factor(foo$density, levels=c("low", "medium", "high"))
foo$day <- factor(paste("Day", foo$day, sep="_"))

library(ggplot2)

d2 <- ggplot(foo, aes(x=day, y=percent, fill=density)) +
      theme_bw() +
      geom_bar(width=0.95, stat="identity") +
      scale_fill_brewer(type="seq", palette=15) +
      xlab("Species") +
      opts(axis.text.x=theme_text(size=6)) +
      facet_grid(. ~ species)

ggsave("barplot_1.png", d2, width=6, height=4)

enter image description here

我还没有解决的一个问题是,密度的级别没有按正确顺序堆叠(对我或@MYaseen208的答案而言)。在原始帖子中,堆叠是正确的。有人知道问题出在哪里吗?


好的建议。我希望在图的底部也有“物种”名称(位于“天”和x轴标签之间)。我希望“天”共享一个单一的x轴,并且没有线来划分每个物种(因此它看起来像是一个单一的图形)。最终产品我将使用Color Brewer和其他花哨的技巧,但为了简单起见,我省略了这些细节。至于堆叠问题-不确定可能是什么,但如果您将数据保存为.csv并以这种方式导入,则会正确堆叠。 - Drosera aliciae
我很高兴能够帮忙(希望有所帮助)。我不确定“day共享单个x轴”是什么意思。至于沿x轴放置物种标签,最方便的方法可能是将pdf版本导入Adobe Illustrator(或Inkscape)以完成自定义。(我相信有一些hack可以使用ggplot2或grid代码实现您想要的效果,但会有收益递减的点)。 - bdemarest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接