Search code examples
rggplot2facet

How to order one variable with respect to another while plotting a facet grid?


I have a dataframe column 'location' which is a factor with 8 levels ('site01', 'site02', 'site03', 'site04' ..... 'site08') each of which have a latitude and longitude as two other columns in the dataframe.

I need to do a facet grid and order the sites according to the latitudes of the sites.

I have tried ordering the column but it doesn't have any effect on the plot produced by ggplot2.

library(scales)
library(ggplot2)
sp <- ggplot(df, aes(x=year, y=value, col=coralType)) + geom_point(stat="identity") + facet_grid(location ~ coralType) + 
  theme(strip.text.x = element_text(size=8),
          strip.text.y = element_text(size=8, face="bold", angle = 0),
          strip.background = element_rect(colour="red", fill="#CCCCFF")) + 
  scale_x_continuous(name="Year", breaks=c(2010, 2013, 2016)) + 
  scale_y_continuous(name="Bleaching Percentage", breaks=seq(0,1,0.5), labels=percent)+
  geom_smooth(method="lm")
sp

This produces the plots but they are not ordered according to the latitude of the site.


Solution

  • @saksham-nagpal, In the absence of a minimum reproducible example, I have created some dummy data and plotted it. Let me know if this works.

    # create some dummy data
    corals<- c("red","blue","green")
    df<- data.frame(siteLoc=paste0(("siteLoc"),1:6,rep("",6)),
                    lat=runif(6), long=c(-2, -1, 0, 1, 2, 5),
                    coralType = rep(corals,6)
                    )
    
    df$mc <- cut(df$long, c(-Inf, 0, 1, Inf), c('level 1', 'level 2', 'level 3'))
    
    # plot the data
    library(ggplot2)
    plt <- ggplot(df, aes(lat,long, colour = mc)) + geom_point(stat="identity") 
    plt + facet_grid(coralType ~ mc, scales="free")
    sp<-plt + facet_grid(coralType ~ mc, scales="free_y")+
      theme(strip.text.x = element_text(size=8),
            strip.text.y = element_text(size=8, face="bold", angle = 0),
            strip.background = element_rect(colour="red", fill="#CCCCFF"))+
      scale_x_continuous(name="Year", breaks=c(2010, 2013, 2016)) + 
      scale_y_continuous(name="Bleaching Percentage", breaks=seq(0,1,0.5))+
      geom_smooth(method="lm")
    sp
    

    plt1

    Now if you look at the variable coralType levels, like levels(df$coralType). It will show as, [1] "blue" "green" "red". Assuming, you want the levels to be reordered like, [1] "red" "blue" "green". In this case, you'll need to reorder the factor levels as,

    # reorder the factors
    df$reordrFactor<- factor(df$coralType, levels = c("red","blue","green"))
    levels(df$reordrFactor)
    [1] "red"   "blue"  "green"
    

    So now, if you plot it, you'll get the desired result.

    # plot according to reordered factor levels
    plt <- ggplot(df, aes(lat,long, colour = mc)) + geom_point(stat="identity") 
    plt + facet_grid(reordrFactor ~ mc, scales="free")
    sp<-plt + facet_grid(reordrFactor ~ mc, scales="free_y")+
      theme(strip.text.x = element_text(size=8),
            strip.text.y = element_text(size=8, face="bold", angle = 0),
            strip.background = element_rect(colour="red", fill="#CCCCFF"))+
      scale_x_continuous(name="Year", breaks=c(2010, 2013, 2016)) + 
      scale_y_continuous(name="Bleaching Percentage", breaks=seq(0,1,0.5))+
      geom_smooth(method="lm")
    sp
    

    plt2

    Hope this helps.