2012-08-02 136 views
7

從以下數據框中工作:雙因素柱狀圖中

> 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)將按物種分組。有關如何創建這個的任何建議?

非常感謝!

回答

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

enter image description here 如果需要,您可以重新排列的水平。

+0

或者你可以將'天'轉換爲'物種'的一個因子和方面...... – joran 2012-08-02 05:35:59

+1

@joran我喜歡按物種分面的想法,但是隨後x軸的標記成爲一個問題。我希望「物種」成爲每個「物種」(也在底部)中的焦點x軸標籤(在圖的底部)和「日」。 – 2012-08-02 06:10:39

+0

@ MYaseen208感謝這個建議 - 我喜歡它是如何在一張圖上顯示的。無論如何,在酒吧之間創造越來越小的空間以按物種「分組」。 – 2012-08-02 19:26:45

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

的一個問題是,density水平不按正確的順序棧(對我或@ MYaseen208的答案)。堆疊在原始帖子中是正確的。有誰知道問題是什麼?

+0

很好的建議。我希望在圖表底部也有'物種'名稱(在'day'和x軸標籤之間)。我希望'天'分享一個單一的X軸,並且不要劃分每個物種的線條(因此它看起來是單個圖形)。我將爲最終產品使用Color Brewer和其他花哨技巧,但爲了簡單起見,我省略了這些細節。至於堆疊問題 - 不知道它可能是什麼,但是如果將數據保存爲.csv並以這種方式導入,它將正確堆疊。 – 2012-08-02 20:21:53

+0

我很高興能夠提供幫助(我希望有一點)。我不確定「共享單個x軸」的意義是什麼。至於沿x軸放置物種標籤 - 將PDF版本帶入Adobe Illustrator(或Inkscape)以完成定製可能是最有利的。 (我敢肯定有些人會用ggplot2或網格代碼來做你想做的事情,但有一點收益遞減)。 – bdemarest 2012-08-02 20:46:25