Skipping X Axis Labels GGPLOT

Hi. I am having trouble getting my x axis worked out on this graph. It is a nine day period with 24 hours in each, split in to half hour ticks. I was hoping to just have every two hours marked off on the x axis to clear space and make it possible to read. I have tried a few different options (pretty:breaks and scale_x_continous) to get it worked out, but I keep getting errors. Would appreciate any suggestions.

DATA:

data.frame(
  stringsAsFactors = FALSE,
           DAY_NUM = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2),
                         TIME = c("22:00",
                                  "22:30","23:00","23:30","23:30","0:00",
                                  "0:00","0:30","0:30","1:00"),
                    Treatment = c("FOUR",
                                  "FOUR","FOUR","FOUR","ONE","FOUR","ONE",
                                  "FOUR","ONE","FOUR"),
                         Mean = c(0,0,0,
                                  4.09090909090909,2.45454545454545,0,2,0,
                                  0.272727272727273,0)

PLOT CODE:

circa_plot <- ggplot(ALL_DATA, aes(x = TIME, y = Mean, col = Treatment, group = Treatment))+ 
  geom_line(show.legend = FALSE) +
  facet_grid(Treatment~DAY_NUM, scales = "free_y") + 
  labs(x = "Time", y = "Mean Number of Hops") +
  theme_minimal() 

circa_plot

PLOT.pdf (25.1 KB)

Turn your difftime variable into a POSIXct variable

library(tidyverse)

ALL_DATA <- data.frame(
    stringsAsFactors = FALSE,
    DAY_NUM = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2),
    TIME = c("22:00",
             "22:30","23:00","23:30","23:30","0:00",
             "0:00","0:30","0:30","1:00"),
    Treatment = c("FOUR",
                  "FOUR","FOUR","FOUR","ONE","FOUR","ONE",
                  "FOUR","ONE","FOUR"),
    Mean = c(0,0,0,
             4.09090909090909,2.45454545454545,0,2,0,
             0.272727272727273,0))
ALL_DATA %>% 
    mutate(TIME = as.POSIXct(paste0("1980-01-01 ", TIME, ":00"))) %>% 
    ggplot(aes(x = TIME, y = Mean, col = Treatment, group = Treatment))+ 
    geom_line(show.legend = FALSE) +
    facet_grid(Treatment ~ DAY_NUM, scales = "free_y") + 
    scale_x_datetime(date_breaks = "2 hour", date_labels = "%H:%M") +
    labs(x = "Time", y = "Mean Number of Hops") +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
#> geom_path: Each group consists of only one observation. Do you need to adjust
#> the group aesthetic?

Created on 2021-05-01 by the reprex package (v2.0.0)

It is possible to use a difftime variable but getting fine control over the axis breaks is tricky.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.