Sofia Martinez
Sofia Martinez

Reputation: 23

Order of x-axis variables in barplot

I have numeric data corresponding to subcategories and categories:

Category <- c("Cell", "Cell", "Nucleus", "Nucleus", "Metabolism", "Metabolism")
Subcategory <- c ("c","r","f","z","a","e")
Val <- c(1,5,34,89,23,12)
df <- data.frame(Category,Subcategory,Val)

I tried:

ggplot(df, aes(x=Subcategory, y=Val)) +
geom_bar(stat="identity", aes(fill=Category)) +
coord_flip() + 
xlim(rev(levels(df$Subcategory)))  

The problem is the order in xlim: xlim always puts the letters in alphabetical array, begins with "a" if I use rev(...), or begins with "z" if: xlim(levels(...).

Is there any way to take the order that I have in the df?

I'd like that the order in x corresponds to the order of the categories, although this implies that the letters of the subcategories remain non-alphabetical.

Upvotes: 2

Views: 4778

Answers (2)

Mthir
Mthir

Reputation: 141

Change levels in xlim(rev(levels(df$Subcategory))) with as.character as xlim(rev(as.character(df$Subcategory)))

so this is the final code:

ggplot(df, aes(x=Subcategory, y=Val)) +
     geom_bar(stat="identity", aes(fill=Category)) +
     coord_flip() + 
     xlim(rev(as.character(df$Subcategory)))

Upvotes: 0

neilfws
neilfws

Reputation: 33782

If I understand correctly, you would like the bars to be ordered by Category and by Subcategory as they appear in the dataframe. That is Cell(c, r); Nucleus(f, z); Metabolism(a, e).

To do that you need to reorder the levels of both the Category and Subcategory factors. Then use Subcategory as the axis labels.

I left out coord_flip as I think it makes things worse for this example.

df %>% 
  mutate(Category = factor(Category,
                           levels = unique(Category)),
         Subcategory = factor(Subcategory,
                              levels = unique(Subcategory))) %>%
  ggplot(aes(Subcategory, Val)) + 
  geom_col(aes(fill = Category)) + 
  scale_x_discrete(labels = df$Subcategory)

Result:

enter image description here

Upvotes: 2

Related Questions