2016-05-30 136 views
1

我正在研究一個條形圖,顯示各個國家/地區不同的貓和狗的數量。貓和狗是以不同因素/變量存儲的等級。我想繪製每個動物數量在另一個(即2層)之上的條形圖,然後我想根據每個國家的動物頻率從最高(即最高計數)到最低。在ggplot2條形圖中訂購多個geom_bar

這裏是我所做的:根據每個國家

plot <- within(plot, country <- factor(country, 
levels=names(sort(table(country), decreasing=TRUE)))) 
  • 繪製圖形

    gg <- ggplot(data = plot, aes(x=country)) 
    
  • 加巴狗動物計數

    1. 訂單數據表

      dogs <- gg + 
      geom_bar(data = plot[plot$animal1 == 'dog',], #select dogs from animal1 variable 
      stat="count") 
      

    如果我這樣做,我得到這個(與一個geom_bar):

    img

    到目前爲止,一切都很好。接下來,我添加第二個geom_bar的貓:

    走調(之後的第二geom_bar
    dogs_cats <- gg + 
    geom_bar(data = plot[plot$animal1 == 'dog',], #select dogs from animal1 variable 
    stat="count") + 
    geom_bar(data = plot[plot$animal2 == 'cat',], #select cats from animal2 variable 
    stat="count") 
    

    現在的順序被改變和:

    img

    我怎樣才能維持秩序酒吧要遵循最初的geom_bar

    非常感謝!

  • 回答

    2

    我建議你使用merge創建一個新的數據幀:

    1.Sum起來(ddplymelt

    require(plyr) #ddply 
    require(reshape2) # melt 
    
    df = ddply(plot, "country", summarize, dogs = sum(animal1 == "dog"), 
    cats = sum(animal2 == "cat")) 
    dogs_and_cats = melt(df, id = "country") 
    

    你可能有一個新的數據幀有3列:

    • 國家
    • 變量:「狗」或「貓」
    • 值:狗/貓的數量(每個國家)

    2.Plot

    ggplot(dogs_and_cats , aes(x = reorder(country, -value), y = value, fill = variable)) + 
    geom_bar(stat = "identity", position = "dodge") 
    

    3。例如:

    這裏是與diamonds數據集的例子,沒有重複的例子:

    df = ddply(diamonds, "cut", summarize, J = sum(color == "J"), 
    D = sum(color == "D")) 
    plot = melt(df, id = "cut") 
    
    ggplot(plot, aes(x = reorder(cut, -value), y = value, fill = variable)) + 
    geom_bar(stat = "identity", position = "dodge") 
    

    enter image description here

    +0

    非常感謝您的建議! – Sophie

    0

    Hoom,我不喜歡你的代碼,但酒吧的順序沒有改變。 也許你在某個地方犯了一個簡單的錯誤。

    library(ggplot2) 
    # make a sample data 
    set.seed(1); d <- data.frame(animal1 = sample(c("dog", "other"), replace=T, 10000, prob=c(0.7,0.3)), 
              animal2 = sample(c("cat", "other"), replace=T, 10000, prob=c(0.3,0.7)), 
              country = sample(LETTERS[1:15], replace=T, 10000, prob=runif(15,0,1))) 
    levels(d$country)  # [1] "A" "B" "C" "D" ... 
    plot <- within(d, country <- factor(country, levels=names(sort(table(country), decreasing=TRUE)))) 
    levels(plot$country) # [1] "N" "O" "L" "F" ... 
    
    gg <- ggplot(data = plot, aes(x=country)) 
    dogs <- gg + geom_bar(data = plot[plot$animal1 == "dog",], stat="count", fill="darkblue") 
    dogs_cats <- gg + 
        geom_bar(data = plot[plot$animal1 == "dog",], stat="count", fill="darkblue") + 
        geom_bar(data = plot[plot$animal2 == "cat",], stat="count", fill="blue") 
    
    print(dogs) 
    print(dogs_cats)  # I made below img using library(grid) to form two graphs. 
    

    plot

    +0

    非常感謝您檢查代碼。你的情節讓我意識到我的錯誤:animal2包含了NAs,導致了geom_bar順序的變化,因爲它假設了不同的總樣本量。雖然我不確定,但我已經解釋清楚了,將問題記錄重新編碼爲使geom_bar保留整個示例的問題後,問題消失。非常感謝! – Sophie