0

I have the below code:

library(ggplot2)
theme_set(theme_classic())

g <- ggplot(EYAnmut_3rdParty, aes(QuestionNumber))
g + geom_bar(aes(fill=stemmed), width = 0.5) + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) + 
  labs(title="Histogram Plot") 

Which provides this plot:

enter image description here

How can I sort the Questions in the Y-axis to be in ascending order (smallest to largest) ? At the moment, it looks very scrambled.

The structure of my dataframe looks like this:

enter image description here

I tried changing the structure of my QuestionNumber variable to factor but it doesn't work.

Dinesh
  • 654
  • 2
  • 9
  • 31

2 Answers2

1

With no sample data, an illustration is made using the famous iris: It can be assumed that Species is question number.

library(tidyverse)

iris %>% 
  arrange(desc(Species), Sepal.Length) %>% 
  ggplot(aes(fct_rev(fct_infreq(Species)), Sepal.Length, fill=Species)) +
    geom_col() +
    labs(x="Species")
OTStats
  • 1,820
  • 1
  • 13
  • 22
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
1
Data$QuestionNumber <- ordered(Data$QuestionNumber, 
                                           levels = c("Q1", "Q2", "Q5", "Q6", "Q7","Q8","Q9","Q10","Q11","Q12","Q13","Q14", "Q15", "Q16", "Q17"))      

This code worked for me. I re-ordered the column based on levels.

pogibas
  • 27,303
  • 19
  • 84
  • 117
Dinesh
  • 654
  • 2
  • 9
  • 31