2016-01-24 48 views
0

如何修改以下代碼以顯示頻率> 10%的級別。在我的真實數據集中,分類變量有35個級別,爲了更好的可視化,我沒有把所有級別都帶入這個圖。 2-如何管理情節的大小,因爲一些級別的頻率非常高,而有些級別非常低。數據組來自汽車配套。將條件置入頻率表中。 (修改代碼)

table_ <- ggplot(States, aes(region ,fill=region)) 
table_ <- table_ + geom_bar() 
table_ <- table_ + theme(legend.position="none") 
table_ <- table_ + coord_flip() 
table_ <- table_ + geom_text(aes(y = (..count..),label = ifelse((..count..)==0,"",scales::percent((..count..)/sum(..count..)))), 
                              stat="count",colour="black",vjust=1.0,size=4) 
+0

@ mtoto,謝謝你的回覆。我得到了這個錯誤:在quantile.default(國家$ region,0.1)中的錯誤:因素是不允許的 – shoorideh

回答

0

你可以先創建您是否想在x軸上的變量的levels向量:

# Extract the factor levels with > 0.1 relative frequencies 
levels <- names(which(summary(States$region)/sum(summary(States$region)) >0.1)) 

# If you just want for example the top ten variables (question 2) 
levels <- names(sort(summary(States$region),decreasing=TRUE)[1:10]) 

然後你可以使用它定義limit說法scale_x_discrete

table_ + ... + scale_x_discrete(limit = levels) 
+0

@ mtoto,感謝您的解決方案,但我想要的是有百分比基於整個數據集,如果我刪除一些級別從數據集中,百分比會有所不同。我想要的只是從情節中降低一些等級,並用真實百分比顯示其他等級。 – shoorideh

+1

@shoorideh then'scale_x_discrete(limit = levels)'? – mtoto

+0

@ mtoto,正好,很棒 – shoorideh