2017-02-27 89 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