Two separate Jitter Plots w/ GLM Lines in Same Plot

Hello! I was wondering if it was at all possible to display two separate jitter plots in the same plot with each one having their own statsmooth lines? I'm including two jitter plots because they represent two related but separate response variables across time. With the code that I've been using I've put in the argument for stat_smooth() but do not get lines in the plot itself, and there are no error messages. Also the two end labels of my y axis labels are not visible. What am I missing?

#jitter of Sequence Exploration & Use combined
combined <- ggplot() +
geom_jitter(data = bxint, mapping = aes(x = seq, y = expl), color = 'darkblue') +
stat_smooth(method = "glm", method.args = list(family = "binomial"),
fill = 'gray', color = 'blue') +
geom_jitter(data = bxint, mapping = aes(x = seq, y = use), color = 'darkred') +
stat_smooth(method = "glm", method.args = list(family = "binomial"),
fill = 'gray', color = 'blue') +
xlab(label = "Sequence Number") +
ylab(label = "Box Interaction") +
scale_y_continuous(labels = c("No Interaction", "0.25", "0.50", "0.75", "Interaction")) +


ggplot works most effectively with data in "long" format. In the code below, we convert your data to long format using the gather function and then map the new key column (marking whether the data values came from expl or use) to colour. You haven't provided a data sample, so I'm inferring what your data probably looks like, based on the code you provided.

In addition, for geom_smooth (or, equivalently, stat_smooth) to work, the outcome variable must have two categories and must be coerced to numeric values (if they aren't already numeric), preferably on a 0/1 scale, as described here. I haven't done that below, but I can be more specific if you provide a data sample.


bxint %>% 
  gather(key, value, expl, use) %>% 
  ggplot(aes(seq, value, colour=key)) +
    geom_jitter(height=0.1) +
    geom_smooth(method=glm, method.args=list(family="binomial"))

To get different colors for the points and the smoother lines, we use a point marker with separate border and fill colors. Then we can set the fill for the markers separately from the color for the lines:

bxint %>% 
  gather(key, value, expl, use) %>% 
  ggplot(aes(seq, value, colour=key, fill=key)) +
    geom_jitter(height=0.1, shape=21, stroke=0) +
    geom_smooth(method=glm, method.args=list(family="binomial")) +
    scale_fill_manual(values=hcl(c(15,195), 100, 40)) +
    scale_colour_manual(values=hcl(c(15,195), 100, 65))
1 Like

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.