0

I am trying to create a plot to track results over days for multiple factors. Ideally I would like my xaxis to be Day, with the day number centered in the middle of the reps for that particular day, the y axis to be result, and the facet will be the Lot (1-4). I am having difficulty making the day centered on the bottom using repeatable text, as the number of reps may vary.

I was using ideas shown in this post: Multi-row x-axis labels in ggplot line chart but have been unable to make any progress.

Here is some code I have been using and the plot that I have so far. The x axis is far too busy and I am trying to consolidate it.

data <- data.frame(System = rep(c("A", "B"), each = 120), Lot = rep(1:4, each = 30),
               Day = rep(1:5, each = 6), Rep = rep(1:6, 40), Result = rnorm(240))

library(ggplot2)

ggplot(data, aes(x = interaction(Day, Rep, lex.order = TRUE), y = Result, color = System, group = System)) +
geom_point() +
geom_line() +
theme(legend.position = "bottom") + 
facet_wrap(~Lot, ncol = 1) +
geom_vline(xintercept = (which(data$Rep == 1 & data$Day != 1)), color = "gray60")

Plot so far

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
kr32
  • 63
  • 1
  • 4

1 Answers1

0

I'm not 100% sure if this is exactly what you are after but this will center the day on the x-axis.

library(dplyr)
library(tidyr)
library(ggplot2)

df <- data.frame(System = rep(c("A", "B"), each = 120), Lot = rep(1:4, each = 30),
                   Day = rep(1:5, each = 6), Rep = rep(1:6, 40), Result = rnorm(240))

df <- df %>% 
     unite(Day_Rep, Day, Rep, sep = ".", remove = F) %>% 
     mutate(Day_Rep = as.numeric(Day_Rep))


ggplot(df, aes(x = Day_Rep, y = Result, color = System, group = System)) +
     geom_point() +
     geom_line() +
     theme(legend.position = "bottom") + 
     facet_wrap(~Lot, ncol = 1) +
     scale_x_continuous(labels = df$Day, breaks = df$Day + 0.5)+
     geom_vline(xintercept = setdiff(unique(df$Day), 1))
TBT8
  • 766
  • 1
  • 6
  • 10