2017-04-15 94 views
0

我想,對於我來說看起來很好的原因,可以繪製堆積的條形圖,條形圖以特定的數據相關順序排列。由於我不明白的原因,它似乎不起作用。具體來說,雖然我可以很容易地按照正確的順序排列我的數據框的行,並使標識欄的名稱列成爲有序因子,因此按照我所需的順序獲取條形圖,但圖形沒有列出數據框的列按照我的願望。堆疊的geom_bar中的物品的訂購

一個例子

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame") 

Title <- 'Plot title' 
ResponseLevels <- c("Not at all", "Somewhat", "Don't know", "Confident", "Very confident") # Labels for bars 

pal.1 <- brewer.pal(category, 'BrBG') # Colours 

tab <- tab %>% arrange(.[,2]) # Sort by first columns of responses 
tab$Item <- factor(tab$Item, levels = tab$Item[order(tab[,2])], ordered = TRUE) # Reorder factor levels 

tab.m <- melt(tab, id = 'Item') 
tab.m$col <- rep(pal.1, each = items) # Set colours 

g <- ggplot(data = tab.m, aes(x = Item, y = value, fill = col)) + 
    geom_bar(position = "stack", stat = "identity", aes(group = variable)) + 
    coord_flip() + 
    scale_fill_identity("Percent", labels = ResponseLevels, 
         breaks = pal.1, guide = "legend") + 
    labs(title = Title, y = "", x = "") + 
    theme(plot.title = element_text(size = 14, hjust = 0.5)) + 
    theme(axis.text.y = element_text(size = 16,hjust = 0)) + 
    theme(legend.position = "bottom") 

g 

stacked bar chart, running in the wrong direction

棒的堆疊的塊由右至左「根本不」運行,從爲「非常有信心」。這些項目按照正確的順序排列,從'多媒體'到'個人',按每個項目「完全不」的比例排序。

我想要得到的是這張圖的響應順序與傳說相同,即從左邊的'Not all',到右邊的'Very confidence'。我無法弄清楚這個順序是如何設置的,也不知道如何改變它。

我已經讀過'類似的問題',但可以看不到這個特定查詢的答案。建議,使用ggplot,不是基於R的圖形,歡迎。

好,建設從allstaire有用的,並大加讚賞的答案,我嘗試以下

library(tidyverse) 

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame") 

tab <- tab %>% select(1,6,5,4,3,2,1) ## Re-order the columns of tab 

tab.m <- tab %>% arrange(`Not at all`) %>% 
mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
gather(variable, value, -Item, factor_key = TRUE) 

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
geom_col() + 
coord_flip() + 
scale_fill_brewer("Percent", type = 'cat', palette = 'BrBG', 
        guide = guide_legend(reverse = TRUE)) + 
labs(title = 'Plot title', y = NULL, x = NULL) + 
theme(legend.position = "bottom") 

而這正是我想要的圖形,所以我的急待解決的問題就解決了。

Stacked bar plot laid out correctly

但是,如果我說的不是

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
geom_col() + 
coord_flip() + 
scale_fill_brewer("Percent", type = 'cat', palette = 'BrBG', 
        guide = guide_legend(reverse = FALSE)) + 
labs(title = 'Plot title', y = NULL, x = NULL) + 
theme(legend.position = "bottom") 

圖片我得到的是這種

Stacked bar chart, legend going in wrong direction

下面圖表的身體是正確的,但傳說是走錯了方向。

這解決了我的問題,但並沒有完全回答我的問題。我從一個數據框開始,爲了得到我想要的結果,我必須顛倒數據列的順序,並將指南圖例顛倒過來。這顯然有效,但這是不正常的。

那麼,堆疊條形圖如何決定以何種順序呈現堆疊物品呢?這與它們在融化數據集中的順序顯然有關,但只是改變順序就會使得圖例沿着錯誤的方向前進。從頂部到底部查看熔化的數據集tab.m,響應按照'非常自信'到'完全不'的順序,但默認圖例是相反順序'完全不','非常自信' 。

回答

1

如果您通過guide_legend而不是僅字符串,則可以將其參數reverse設置爲TRUE。簡化了一下,

library(tidyverse) 

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame") 

tab.m <- tab %>% arrange(`Not at all`) %>% 
    mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
    gather(variable, value, -Item, factor_key = TRUE) 

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
    geom_col() + 
    coord_flip() + 
    scale_fill_brewer("Percent", palette = 'BrBG', 
         guide = guide_legend(reverse = TRUE)) + 
    labs(title = 'Plot title', y = NULL, x = NULL) + 
    theme(legend.position = "bottom") 


對於編輯:

酒吧順序是由因子水平順序,這在上面被列順序由於所確定的確定使用gather來創建該因子,但coord_flip使其不太明顯。不過,通過levels<-或者重新組裝這個因素很容易扭轉等級順序。要保持相同級別的顏色,請將direction = -1更改爲scale_fill_brewer以顛倒其順序。

tab.m <- tab %>% arrange(`Not at all`) %>% 
    mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
    gather(variable, value, -Item, factor_key = TRUE) %>% 
    mutate(variable = factor(variable, levels = rev(levels(variable)), ordered = TRUE)) 

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
    geom_col() + 
    coord_flip() + 
    scale_fill_brewer("Percent", palette = 'BrBG', direction = -1, 
         guide = guide_legend(reverse = TRUE)) + 
    labs(title = 'Plot title', y = NULL, x = NULL) + 
    theme(legend.position = "bottom") 

+0

這是一個偉大的建議,並感謝你爲它畫上了我的注意。它並沒有完成我所需要的,因爲圖表仍然運行錯誤。在我的大部分工作中,都有自然的排序和迴應。在這種情況下,從「完全不」到「非常有信心」。正如讀者在x軸上期望更大的數字,所以他們期望在右邊而非左邊的'非常自信'。現在我將嘗試以相反的順序放置ResponseLevels,以查看圖形是否以正確的順序排列。再次感謝! – astaines

+1

完美,我現在明白了。再次感謝您的清晰解釋。 – astaines

相關問題