2017-02-27 49 views
0

我有一個data.frame有幾個因素,如:R:按級別排序頻因子和策劃

df<-data.frame(Var1=as.factor(sample(c("AB", "BC", "CD", "DE", "EF"), 1000, replace=TRUE))) 

summary(df$Var1) 
AB BC CD DE EF 
209 195 178 221 197 

我要繪製的水平頻率data.frame如下:

ggplot(df, aes(x=factor(1), fill=factor(Var1)))+ 
     geom_bar(width=1, colour="black")+ 
     coord_polar(theta="y")+ 
     theme_void() 

但是,級別的順序是按字母順序排列的,而不是按頻率排列。從庫(plyr)使用計數我可以創建一個新data.frame,讓我每一級的頻率:

df_count <-count(df, "Var1") 
Var1 freq 
1 AB 209 
2 BC 195 
3 CD 178 
4 DE 221 
5 EF 197 

,我可以再重新安排使用

df_count$Var1<-factor(df_count$Var1, levels=df_count$Var1[order(df_count$freq, decreasing=TRUE)]) 

,當繪製給我我想要的,每個級別的排序頻率。

1.)這是最優雅的解決方案嗎?它給了我原始data.frame中每個因子/列的額外data.frame,我覺得必須有一個更簡單的方法。

2.)當繪圖時,如何重命名圖例標籤並確保它們分配正確的因子水平?如果我使用

scale_fill_manual(labels=c("Name of AB", "Name of BC", "Name of CD", "Name of DE","Name of EF")) 

標籤與正確的級別無關。這裏圖例中的第一個條目將是「DE」,因爲它是具有最高頻率的水平,但標籤將會說明scale_fill_manual中定義的「AB的名稱」。我可以每次手動檢查標籤的順序,但必須有自動方式嗎?

回答

1

你想reorder(),我想。通常,reorder(x,y,FUN)根據將函數FUN應用於第二變量y的結果來改變因子x的等級順序。在這種情況下,您可以使用FUN=length,這與y的用途無關。

設置數據(我選擇修改的可能性,使結果更清晰):

set.seed(101) 
df <- data.frame(Var1=as.factor(sample(c("AB", "BC", "CD", "DE", "EF"), 
          prob=c(0.1,0.5,0.2,0.05,0.15), 
            1000, replace=TRUE))) 

基本情節(錯誤的順序):

library(ggplot2) 
print(g1 <- ggplot(df, aes(x=factor(1), fill=Var1))+ 
    geom_bar(width=1, colour="black")+ 
    coord_polar(theta="y")+ 
    theme_void()) 

enter image description here

現在重新排序:

df$Var1 <- reorder(df$Var1,df$Var1,FUN=length) 
levels(df$Var1) 
## [1] "DE" "AB" "EF" "CD" "BC" 

檢查順序是正確的:

sort(table(df$Var1)) 
## DE AB EF CD BC 
## 46 105 163 189 497 

打印新的圖形(粘在新的數據與%+%guide_legend()翻轉傳奇的順序:你也可以使用function(x) -length(x)作爲FUN改變順序在第一位的水平)。

print(g1 %+% df + 
    scale_fill_discrete(guide=guide_legend(reverse=TRUE))) 

enter image description here

+0

這很好,謝謝!結合aosmith的提示,如何正確使用命名向量,它就像一個魅力:-) – user45017

3

功能包強制可以幫助因素順序。特別地,fct_infreq將根據每個級別的頻率設置級別的順序。

library(forcats) 

df$Var1 = fct_infreq(df$Var1) 

您可以使用命名向量來避免scale_*_manual函數中的順序。

scale_fill_manual(labels = c(AB = "Name of AB", 
         BC = "Name of BC", 
         CD = "Name of CD", 
         DE = "Name of DE", 
         EF = "Name of EF")) 

所以,你的陰謀代碼可能看起來像

ggplot(df, aes(x = factor(1), fill = fct_infreq(Var1)))+ 
    geom_bar(width = 1, colour = "black")+ 
    coord_polar(theta = "y")+ 
    theme_void() + 
    scale_fill_discrete(labels = c(AB = "Name of AB", 
          BC = "Name of BC", 
          CD = "Name of CD", 
          DE = "Name of DE", 
          EF = "Name of EF")) 
+0

感謝您的回答!我決定採用本博爾克的答案,因爲它只使用基本功能,但是關於如何正確使用標籤載體的解釋非常有用:-) – user45017

0

您還可以使用該庫forcats一個簡單的解決方案,並且功能fct_infreq

library(forcats) 
ggplot(df, aes(x = factor(1), fill = fct_infreq(Var1)))+ 
    geom_bar(width = 1, colour = "black")+ 
    coord_polar(theta = "y")+ 
    theme_void() + 
    guides(fill = guide_legend(title = "Var1")) 

Pie chart

注意,餅圖被認爲是邪惡的(你可以在google說),你可以用一個簡單的柱狀圖傳達了同樣的信息:

ggplot(df, aes(x = fct_infreq(Var1), fill = fct_infreq(Var1))) + 
    geom_bar(width = 1, colour = "black", show.legend = FALSE) + 
    xlab("Var1") 

Bar chart