Search code examples
rggplot2facet

R, ggplot2, facetted histogram, decending order, factor x-axis labels


I have an interesting problem where I need to produce a facetted histogram series which I want to put in decending order for each panel. This below code replicates the problem.

library(ggplot2)
set.seed(1)
nGroup  = 10; nSample = 10
df <- data.frame(Group=rep(1:nSample,nGroup),
                 Category = factor(sample(LETTERS,
                                          nGroup*nSample,
                                          replace=TRUE),
                                   levels=LETTERS),
                 Amount   = runif(nGroup*nSample))
ggplot(df,aes(x=Category,y=Amount)) + 
  geom_bar(stat='identity') +
  facet_wrap(~Group,scales='free_x') +
  labs(title="Some Random Histogram with Facets")

Which produces the below output:

Example

I have tried the following:

library(ggplot2)
set.seed(1)
nGroup  = 10; nSample = 10
df <- data.frame(Group=rep(1:nSample,nGroup),
                 Category = factor(sample(LETTERS,
                                          nGroup*nSample,
                                          replace=TRUE),
                                   levels=LETTERS),
                 Amount   = runif(nGroup*nSample))
ggplot(df,aes(x=reorder(Category,-Amount),y=Amount)) + 
  geom_histogram(stat='identity') +
  facet_wrap(~Group,scales='free_x') +
  labs(title="Some Random Histogram with Facets")

Which is a slight improvement (see below), but no-where near where it should be.

Example2

Any suggestions would be much appreciated? I fully understand that the x-axis labels will NOT correspond between panels, this is fine for my application.


UPDATE: Fixing the Labels.

Mamoun's answer did the job, however, for those of you interested in fixing the x-axis labels to remove the interaction suffixes, it can be done like this (including some other desired formatting options):

library(ggplot2)
set.seed(1)
nGroup  = 10; nSample = 10
df <- data.frame(Group=factor(rep(1:nSample,nGroup)),
                 Category = factor(sample(LETTERS,
                                          nGroup*nSample,
                                          replace=TRUE),
                                   levels=LETTERS),
                 Amount   = runif(nGroup*nSample))
df <- ddply(df,c("Group","Category"),function(d){ data.frame(Amount=sum(d$Amount)) })
df$Interaction = interaction(df$Category,df$Group)
df$XLabel      = as.character(df$Category)

ggplot(df,aes(x=reorder(Interaction,-Amount),y=Amount,fill=Amount)) +  
  geom_bar(stat='identity',color="black",width=1.0) +
  scale_x_discrete(breaks=df$Interaction,labels=df$XLabel) + 
  facet_wrap(~Group,scales='free_x') +
  theme_bw() + theme(legend.position=c(1,0),legend.justification=c(1,0)) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust=0.5, size=6)) +
  scale_fill_gradient(low="blue",high="red") + 
  labs(title="Some Random Histogram with Facets",x="Category")

Which produces the following, as desired:

Example3


Solution

  • you can use reorder and interaction between groups and Category like this

    ggplot(df,aes(x=reorder(interaction(Category, Group), -Amount, sum),y=Amount)) + 
      geom_bar(stat='identity') +
      facet_wrap(~Group,scales='free_x') +
      labs(title="Some Random Histogram with Facets")