Reordering ggplot stacked barchat

37 Views Asked by At

Trying to reorder columns in my based on total blank and 'N/A' values.

ggplot(quality_melted, aes(x = Column, y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  labs(title = "Data Quality Inspection: Blanks and 'N/A' Responses", x = "Columns", y = "Count")

Tried using something like x = reorder(Column, NA_blank_Count) but that wasn't working for me. Sorry I cannot provide the visualization output; I'm working with sensitive data.

1

There are 1 best solutions below

0
On

reorder() works on factor only

You need to convert your Column as factor and you can actually define the order during the operation, like:

#sample column with NA. change Column values according to your actual data
Column<- c(NA, 'Blank', 'Blank')
Column%>%factor(levels=c('Blank', NA), exclude = NULL)
#level order defined
[1] <NA>  Blank Blank
Levels: Blank <NA>

Now, if you plot, you should got the order starting with "Blank".