Possible to combine position_jitter with position_dodge?

Possible to combine position_jitter with position_dodge?

geom_point(position = jitter)
geom_point(position dodge)
position_jitterdodge
geom_point jitter width
ggplot jitter boxplot
position=dodge not working
geom_violin(position dodge)
geom_histogram(position=dodge)

I've become quite fond of boxplots in which jittered points are overlain over the boxplots to represent the actual data, as below:

set.seed(7)
l1 <- gl(3, 1, length=102, labels=letters[1:3])
l2 <- gl(2, 51, length=102, labels=LETTERS[1:2]) # Will use this later
y <- runif(102)
d <- data.frame(l1, l2, y)

ggplot(d, aes(x=l1, y=y)) + 
  geom_point(position=position_jitter(width=0.2), alpha=0.5) +
  geom_boxplot(fill=NA) 

(These are particularly helpful when there are very different numbers of data points in each box.)

I'd like to use this technique when I am also (implicitly) using position_dodge to separate boxplots by a second variable, e.g.

ggplot(d, aes(x=l1, y=y, colour=l2)) + 
  geom_point(position=position_jitter(width=0.2), alpha=0.5) +
  geom_boxplot(fill=NA)

However, I can't figure out how to dodge the points by the colour variable (here, l2) and also jitter them.


Here is an approach that manually performs the jittering and dodging.

# a plot with no dodging or jittering of the points 
dp <- ggplot(d, aes(x=l1, y=y, colour=l2)) + 
  geom_point(alpha=0.5) +
  geom_boxplot(fill=NA)

# build the plot for rendering
foo <- ggplot_build(dp)
# now replace the 'x' values in the data for layer 1 (unjittered and un-dodged points)
# with the appropriately dodged and jittered points
foo$data[[1]][['x']] <- jitter(foo$data[[2]][['x']][foo$data[[1]][['group']]],amount = 0.2)
# now draw the plot (need to explicitly load grid package)
library(grid)
grid.draw(ggplot_gtable(foo))
# note the following works without explicitly loading grid
plot(ggplot_gtable(foo))

Simultaneously dodge and jitter, 1 and earlier). See also. Other position adjustments: position_dodge() , position_identity() , position_jitter() , position_nudge()  degree of jitter in y direction. Defaults to 0. the amount to dodge in the x direction. Defaults to 0.75, the default position_dodge() width. A random seed to make the jitter reproducible. Useful if you need to apply the same jitter twice, e.g., for a point and a corresponding label.


I don't think you'll like it, but I've never found a way around this except to produce your own x values for the points. In this case:

d$l1.num <- as.numeric(d$l1)
d$l2.num <- (as.numeric(d$l2)/3)-(1/3 + 1/6)
d$x <- d$l1.num + d$l2.num

ggplot(d, aes(l1, y, colour = l2)) + geom_boxplot(fill = NA) +
  geom_point(aes(x = x), position = position_jitter(width = 0.15), alpha = 0.5) + theme_bw()

It's certainly a long way from ideal, but becomes routine pretty quickly. If anyone has an alternative solution, I'd be very happy!

Combine position_jitter and position_dodge so points can align with , Combine position_jitter and position_dodge so points can align with boxplots in ggplot2 - position_jitterdodge.R. Combine position_jitter and position_dodge so points can align with boxplots in ggplot2.


The new position_jitterdodge() works for this. However, it requires the fill aesthetic to tell it how to group points, so you have to specify a manual fill to get uncolored boxes:

ggplot(d, aes(x=l1, y=y, colour=l2, fill=l2)) + 
  geom_point(position=position_jitterdodge(width=0.2), alpha=0.5) +
  geom_boxplot() + scale_fill_manual(values=rep('white', length(unique(l2))))

`position_nudge` doesn't work when called as `position = "nudge , position = "jitter" - does what you expect; position = "dodge" - does what be able to teach nudge at a higher (e.g. theoretical) level because it  degree of jitter in y direction. Defaults to 0. the amount to dodge in the x direction. Defaults to 0.75, the default position_dodge() width. A random seed to make the jitter reproducible. Useful if you need to apply the same jitter twice, e.g., for a point and a corresponding label.


I'm using a newer version of ggplot2 (ggplot2_2.2.1.9000) and I was struggling to find an answer that worked for a similar plot of my own. @John Didon's answer produced an error for me; Error in position_jitterdodge(width = 0.2) : unused argument (width = 0.2). I had previous code that worked with geom_jitter that stopped working after downloading the newer version of ggplot2. This is how I solved it below - minimal-fuss code....

ggplot(d, aes(x=l1, y=y, colour=l2, fill=l2)) + 
  geom_point(position = position_jitterdodge(dodge.width = 1, 
                                             jitter.width = 0.5), alpha=0.5) +
  geom_boxplot(position = position_dodge(width = 1), fill = NA)

Using position_dodge on point geoms, Is it possible to use position_dodge() to avoid the points overlapping? I have tried to geom_jitter(size=3, position=position_jitter(w=.01,h=.01)) # So I want '​merge' solution (which should allow parts of clusters to overlap). position_jitter.Rd Counterintuitively adding random noise to a plot can sometimes make it easier to read. Jittering is particularly useful for small datasets with at least one discrete position.


Another option would be to use facets:

set.seed(7)
   l1 <- gl(3, 1, length=102, labels=letters[1:3])
   l2 <- gl(2, 51, length=102, labels=LETTERS[1:2]) # Will use this later
   y <- runif(102)
   d <- data.frame(l1, l2, y)

   ggplot(d, aes(x=l1, y=y, colour=l2)) + 
     geom_point(position=position_jitter(width=0.2), alpha=0.5) +
     geom_boxplot(fill=NA) +
     facet_grid(.~l2) +
     theme_bw()

Sorry, don´t have enough points to post the resulting graph.

Extending the Linear Model with R: Generalized Linear, Mixed , + geom_histogram(position="dodge" ↩→ , binwidth=5, aes(y=..density. a smaller number of points, it is possible to combine both levels of the response in a position= ↩→ position_jitter())+facet_grid(~ chd) We would like to predict the​  position_dodge requires the grouping variable to be be specified in the global or geom_* layer. Unlike position_dodge, position_dodge2 works without a grouping variable in a layer. position_dodge2 works with bars and rectangles, but is particulary useful for arranging box plots, which can have variable widths.


Be Awesome in ggplot2: A Practical Guide to be Highly Effective, It's also possible to combine different geoms (e.g.: geom = c(“point”, colors by sex a + geom_histogram(aes(color = sex), fill = "white", position = "dodge") The function geom_jitter() is a convenient default for geom_point(position = 'jitter'). ETA: I found a recent question about this on the ggplot google group with the suggestion to add alpha=0.5 to see what's going on. It isn't that ggplot is taking the max value from each grouping; it's actually over-plotting bars on top of one another for each value.


GGPlot Error Bars Best Reference, ymax = len+sd) ). Possible error plots: Combine with jitter points ggplot(df, aes(dose, len)) + geom_jitter(position = position_jitter(0.2), color = "darkgray") +  I'm trying to use position_dodge, but it's not working, and I suspect this is because I'm feeding it values from a different dataset. Possible to combine position


GGplot2: layers, making it possible to create sophisticated plots that display data from multiple sources. You've specified using the aes() function and combined with the plot defaults as position_dodge() : place overlapping bars (or boxplots) side-by-​side. ggplot(mpg, aes(displ, hwy)) + geom_point(position = "jitter")  Categorical data is aligned on the integers, so a width or height of 0.5 will spread the data so it's not possible to see the distinction between the categories. seed A random seed to make the jitter reproducible.