r - Imposing a condition into frequency table. (Modifying th code) -


how can modify below code showing levels frequency > 10%. in real data set, categorical variable has 35 levels , better visualization, not bring levels plot. 2- how can manage size of plot, since levels have high frequencies , low. data set car package.

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) 

you first create levels vector of variables want on x-axis:

# extract factor levels > 0.1 relative frequencies levels <- names(which(summary(states$region)/sum(summary(states$region)) >0.1))  # if want example top ten variables (question 2) levels <- names(sort(summary(states$region),decreasing=true)[1:10]) 

then can use define limit argument of scale_x_discrete:

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

Comments