I am trying to plot a line over a bunch of points by certain groups. For example, I have:
x = rep(c('fruit', 'vegetable'), 15)
z = c(rep(c(rep('fresh', 2), rep('ripe', 2), rep('rotten', 2)), 5)
s = c(rep('pick1', 6), rep('pick2', 6), rep('pick3', 6), rep('pick4', 6), rep('pick5', 6))
y = runif(30, min=11, max=15)
low = rep(c(0, 1), 15)
y[low == 1] = y[low == 1] - 10
y[low == 0] = y[low == 0] - 5
groceries_df = data.frame(type=x, value=y, age=z, species=s)
where type is either "fruit" or "vegetable", age is either "fresh", "ripe", or "rotten", and species is a unique list of different picks from a store, so there are 2*3 = 6 values for each species. I am grouping these values on a discrete x-axis defined by their age, and they are grouped by their type (and thus dodged by their type).
dodge = position_dodge(width=0.9)
p = ggplot(groceries_df, aes(factor(age), value, color=factor(type), shape=factor(type))) +
geom_jitter(position=dodge) +
geom_line(data=groceries_df[groceries_df$type == 'fruit',], aes(group=factor(species)), position=dodge) +
geom_line(data=groceries_df[groceries_df$type == 'vegetable',], aes(group=factor(species)), position=dodge)
This code produces a plot extremely close to what I want, but I noticed that geom_line dodges by species, not by type. It seems as though I just need to redirect the dodge, even though I need the actual lines to be grouped by species (so I can see the transition of one species' value from one type to the next at all ages). Is there any way to redirect dodge?