Search code examples
rggplot2facet

Axis tick labels won't show up in ggplot facet


I am struggling to get my x-axis tick labels to show up as the day the sample was taken. I am also struggling with my grouping reordered, currently, it is showing up as Afternoon coming before Pre-Dawn, I would like Pre-Dawn to be first in order.

Data http://www.sharecsv.com/s/f7079be36f5fc5035029ae105f96d560/VR_Sonde_Data_May_2017%20(1).csv

DO=read.csv("VR_Sonde_Data_May_2017 (1).csv")  

DOmelt <- melt(DO, id.vars=c("Month", "Day", "TimeofDay"), measure.vars = c("AverageDO")) 




ggplot(DOmelt, aes((x=Day), group=interaction(Month, TimeofDay), fill=TimeofDay)) +  
       geom_bar(aes(y=value), stat="identity", position=position_dodge()) +  
              facet_grid(~Month, scales = "free_x") +  

      ggtitle("Dissolved Oxygen in Ventura River") +  
      labs(subtitle = "2017") +  

      theme(plot.title = element_text(size=30, face="bold", vjust=2, hjust=.5), plot.subtitle = element_text(size=20, face="bold", vjust=2, hjust=.5))+  

      scale_x_discrete("day") +  
       scale_y_continuous(name ="Average Dissolved Oxygen")+  
            theme(axis.text.x =element_text(angle=90))

Solution

  • You can use the following code

    library(tidyverse)
    
    DOmelt %>%
      arrange(AverageDO) %>%
      mutate(TimeofDay = factor(TimeofDay, levels=c("Pre-Dawn", "Afternoon"))) %>%
      ggplot(aes(x=Day, y=AverageDO, group=interaction(Month, TimeofDay), fill=TimeofDay)) +
      geom_bar(position=position_dodge(), stat="identity") +  
      facet_grid(~Month, scales = "free_x") +  
      ggtitle("Dissolved Oxygen in Ventura River") +  
      labs(subtitle = "2017") +  
      theme(plot.title = element_text(size=30, face="bold", vjust=2, hjust=.5), plot.subtitle = element_text(size=20, face="bold", vjust=2, hjust=.5))+  
      xlab("Day") +  
      scale_y_continuous(name ="Average Dissolved Oxygen")+  
      theme(axis.text.x =element_text(angle=90))
    

    enter image description here

    Data

    DOmelt = structure(list(Month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("May", "September"), class = "factor"), 
        Day = c(11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 
        16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 
        22L, 22L, 23L, 23L, 24L, 24L, 25L, 6L, 6L, 7L, 7L, 8L, 8L, 
        9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 
        15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L), TimeofDay = structure(c(2L, 
        1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
        2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 
        2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
        1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Afternoon", 
        "Pre-Dawn"), class = "factor"), AverageDO = c(6.99, 12.24, 
        6.61, 12.05, 6.51, 11.94, 6.63, 12.12, 6.67, 12.28, 6.68, 
        12.14, 6.87, 11.94, 6.64, 10.77, 6.47, 9.3, 6.21, 10.71, 
        5.92, 10.95, 5.85, 11.46, 5.98, 11.31, 6.12, 10.27, 6.38, 
        6.61, 8.97, 6.88, 9.08, 7.01, 9.18, 7.2, 9.39, 7.25, 9.61, 
        6.97, 8.87, 6.77, 8.8, 6.88, 8.92, 7.1, 9.25, 7.34, 9.26, 
        7.44, 9.46, 7.59, 9.66, 7.74, 9.72, 7.77, 9.54, 7.71)), class = "data.frame", row.names = c(NA, 
    -58L))