Search code examples
rggplot2facetfacet-wrapfacet-grid

How do I order x-axis within facet_nested


I have created a barplot and arranged the x-axis accordingly to how I want it. However, once I run it through facet_nested, it changes the order.

This is what my file looks like:

file:

drug         P_resistant    G_resistant   
DrugA        18             16           
DrugB1       430            425          
DrugB2       156            154          
DrugB3       0              0            
DrugC1       60             56           
DrugC2       66             64           
DrugC3       113            111          
DrugC4       250            248         

To arrange the order according type, class and drug order:

library(ggplot2)
library(tidyr)
library(dplyr)
library(ggrepel)
library(forcats)
library(data.table)
library(ggnomics)
library(scales)
library(survival)
library(tidytext)

file.g <- gather(file, type, value, -drug)

##this is for facet "group"
file.g$group <-c("87.51%","98.85%","98.71%","NA","93.2%","96.9%","98.2%","99%","87.51%","98.85%","98.71%","NA","93.2%","96.9%","98.2%","99%")

##this is for facet "class"
file.g$class <- c(rep("Class A",1),rep("Class B",3),rep("Class C",4),rep("Class A",1),rep("Class B",3),rep("Class C",4))

## the order of drug appearance on x-axis
file.g$drug_order<- c(4,1,2,3,5,6,7,8,4,1,2,3,5,6,7,8)

So when you look at file.g, it will look like this

drug        type           value     group       class      drug_order
DrugA       P_resistant    18        87.51%      Class A    4
DrugB1      P_resistant    430       98.85%      Class B    1
DrugB2      P_resistant    156       98.71%      Class B    2
DrugB3      P_resistant    0         NA          Class B    3
DrugC1      P_resistant    60        93.2%       Class C    5
DrugC2      P_resistant    66        96.9%       Class C    6
DrugC3      P_resistant    113       98.2%       Class C    7
DrugC4      P_resistant    250       99%         Class C    8
DrugA       G_resistant    16        87.51%      Class A    4
DrugB1      G_resistant    425       98.85%      Class B    1
DrugB2      G_resistant    154       98.71%      Class B    2
DrugB3      G_resistant    0         NA          Class B    3
DrugC1      G_resistant    56        93.2%       Class C    5
DrugC2      G_resistant    64        96.9%       Class C    6
DrugC3      G_resistant    111       98.2%       Class C    7
DrugC4      G_resistant    248       99%         Class C    8

The following code arranges the x-axis to how I want it:

DrugB1, DrugB2, DrugB3, DrugA, DrugC1, DrugC2, DrugC3, DrugC4

file.g$type <- factor(file.g$type, levels=c("P_resistant","G_resistant"))

file.g$class <- factor(file.g$class, levels= c("Class B", "Class A", "Class C"))

##main script
p<-ggplot(file.g, aes(fill=type, x=reorder_within(drug, drug_order, class), y=value)) + 
  geom_bar(aes(fill = type), stat = "identity", position = "dodge", colour="white") + 
  geom_text(aes(label=value), position=position_dodge(width=1.2), vjust=-0.5)+ 
  scale_fill_manual(values=c("#af8dc3","#7fbf7b")) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, 500)) +
  theme(title = element_text(size = 18), legend.text=element_text(size=12), axis.text.x=element_text(size=9), axis.text.y =element_text(size=15)) +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_reordered()

Image 1

But when I run this through facet nested

p+facet_nested(.~class+group, scales= "free_x", space= "free_x")+
  theme(strip.text.x = element_text(size = 7.5))

Image2

it changes the x-axis order to

DrugB2, DrugB1, DrugB3, DrugA, DrugC1, DrugC2, DrugC3, DrugC4

I've been stuck on this for a couple of hours now..I appreciate any help or ideas.


Solution

  • The problem is not the x-axis, the x-axis only has one value within each facet. More likely, the problem is the ordering of the factor levels within file.g$class and file.g$group that you use for facetting. This is not an issue specific to facet_nested, you'd have the same ordering problem with facet_grid on which it is based.

    The following gets me the order you mentioned you wanted:

    # Reordering factors
    file.g$class <- factor(file.g$class, 
                           levels = c("Class B", "Class A", "Class C"))
    file.g$group <- factor(file.g$group, 
                           levels = c("98.85%", "98.71%", "87.51%", "93.2%", "96.9%", "98.2%", "99%"))
    
    # Plotting
    ggplot(file.g, aes(fill = type, x = drug, y = value)) + 
      geom_bar(aes(fill = type), stat = "identity", position = "dodge", colour="white") + 
      geom_text(aes(label = value), position = position_dodge(width = 1.2), vjust = -0.5)+ 
      scale_fill_manual(values = c("#af8dc3", "#7fbf7b")) + 
      scale_y_continuous(expand = c(0, 0), limits = c(0, 500)) +
      theme(title = element_text(size = 18), 
            legend.text = element_text(size = 12), 
            axis.text.x = element_text(size = 9), 
            axis.text.y =element_text(size = 15),
            plot.title = element_text(hjust = 0.5)) +
      facet_nested(.~class + group, scales = "free_x", space= "free_x")
    

    enter image description here