1

I am new to the R and practicing. I have created a bar chart by using "ggplot2" package. But it is in the order that I did not like.

I am including my data frame and command here. Please tell me what should I do?

library(ggplot2)

x = data.frame(Depression_Level = c("Normal", "Mild", "Moderate", "Mod.Severe", "Severe"), 
               Percentage = c(32.4, 34.6, 28.4, 3.1, 1.1))

Depression_Level Percentage 1           Normal       32.4 2            Mild       34.6 3         Moderate       28.4 4       Mod.Severe      
3.1 5           Severe        1.1

x1 = ggplot(data=x, aes(x=Depression_Level, y = Percentage)) +
       geom_bar(stat="identity", fill="darkred") +
       theme_minimal() +
       geom_text(aes(label=Percentage), vjust=1.4, color="white", size=3.5)

The Image I have Got

I want to present the levels of the variables in the order of "Normal", "Mild", "Moderate", "Mod. Severe", "Severe".

But instead, it gave me the order like this "Mild", "Moderate", "Mod. Severe", "Normal", "Severe". It appears to me as alphabetical order. How can I fix this and customize it on my own way?

camille
  • 16,432
  • 18
  • 38
  • 60
  • I tried to format and indent your code for you, but don't know what you're trying to do with the data printout so I left that as is – camille Aug 28 '19 at 18:15

1 Answers1

1

Make Depression_Level a factor with the desired order of levels:

x$Depression_Level <- factor(x$Depression_Level, levels = x$Depression_Level)

and then run your ggplot command.

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341