0

I am trying to make a bar graph using ggplot2

the data looks like this (there are about 10000 observations) and i have around 60 different subgroups.

ID        subgroup            patient_group
1866      cancer              1
1877      heart failure       1
1878      cancer              2
1879      infection           1
...       ...

What I would like to do is sort the subgroups by count instead of alphabetically, so the graph is easier to read. this is my code so far.

ggplot(data = data, aes(x = subgroup, fill = patient_group)) +
  geom_bar(position = "dodge") +
  coord_flip() +
  ggtitle("Count of Subgroups of patients") +
  xlab("") +
  ylab("") +
  labs(fill = "Emerge Version")

Is there a way to reorder the bars without having to make a new dataframe for the subgroups? Thanks!

Shnetti
  • 11
  • 5

0 Answers0