我目前有一个ggplot,但是按字母顺序显示,我希望图表首先显示最“重要分数”,然后按降序显示。请参阅附件的图和代码。
library(ggplot2)
ggplot(data= VIMP, aes(x=(VIMP$Y),y=VIMP$X)) +
geom_bar(position="dodge",stat="identity",width = 0, color =
"black") +
coord_flip() + geom_point(color='skyblue') +
xlab("Variables")+ylab(" Importance Score")+
ggtitle("Variable Importance") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white', colour =
'black'))
答案 0 :(得分:0)
要解决此问题,您可以使用library(forcats)
软件包。 Forcats是用于处理R中因素的软件包。
该代码可能对您有用。
VIMP <- VIMP %>%
mutate(Y = forcats::fct_reorder(Y, X)) ##reorder the Y variable based on X, it's also possible to change to a descending order using desc(X).
ggplot(data= VIMP, aes(x=(VIMP$Y),y=VIMP$X)) +
geom_bar(position="dodge",stat="identity",width = 0, color =
"black") +
coord_flip() + geom_point(color='skyblue') +
xlab("Variables")+ylab(" Importance Score")+
ggtitle("Variable Importance") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white', colour = 'black'))