-2

enter image description hereI currently have a ggplot however it is shown in alphabetical order, I want the graph to show the most 'important score' first and order in descending order. See image of plot attached and code.

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'))

enter image description here

Kate English
  • 55
  • 1
  • 6

1 Answers1

0

To solve this problem you might use the library(forcats) package. Forcats is a package that was made to deal with factors in R. This code might work for you.

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'))
John P. S.
  • 367
  • 3
  • 17