Combining geom_point and geom_line with position_jitterdodge in ggplot2 for two grouping factors

I tried several suggestions based on a few posts here, as well as reading the ggplot2 documentation, but this question is a little different and I haven't found a solution yet.

Here's a snippet of code to make the molten data frame look like the one I'm working with:

a <- c(1,2,3,4,5,6,1,2,3,4,5,6) b <- c("loss", "draw", "win", "draw", "loss", "win", "loss", "draw", "win", "draw", "loss", "win") c <- c(2,3,5,4,4,5,4,4,3,5,2,4) d <- c(rep("x", 6), rep("y", 6)) temp <- data.frame(a,b,c,d) 

I want to create a scatter plot with b on the x-axis, c on the y-axis, with points on the x-axis, grouped by d, and the lines between them, grouped by a. If we start by simply placing the points on the chart, it will work out fine:

 ggplot(temp, aes(x=b, y=c, fill=d, colour=d))+ geom_point(position=position_jitterdodge()) 

This is the point I get, and this is how I want it to look. The points are color coded according to the coefficient d, and in addition, they tremble on one side, so that x is on the left and y is on the right.

Now all I want is to connect the dots with the lines according to the coefficient a. This is the incorrect version I made in MS Paint , how it should look. Adding geom_line and setting the geom_line group to should work ...

 ggplot(temp, aes(x=b, y=c, fill=d, colour=d))+ geom_point(position=position_jitterdodge())+ geom_line(aes(group=a),position=position_jitterdodge()) 

... but it is not. The lines he creates are the correct length to connect the correct points, but they barely touch them, it looks like they are on a graph at random. I would add another screenshot, but I don't have privileges yet.

Also, if I change the groupโ€™s aesthetics to a general aesthetics, for example:

 ggplot(temp, aes(x=b, y=c, fill=d, colour=d, aes(group=a)))+ geom_point(position=position_jitterdodge())+ geom_line(position=position_jitterdodge()) 

... then the points switch so that they are in the wrong position. And in any case, the lines are still not completely connected with the dots.

I also tried to specify the width and jitter.width values โ€‹โ€‹at position_jitterdodge (), and the lines and dots still did not connect properly. I also read the ggplot2 documentation, a few previous stackoverflow related questions, and tried out most combinations of position_jitter, position_dodge, geom_jitter, etc., but no luck so far.

Any help would be greatly appreciated.

+5
source share
2 answers

One possible solution is to manually set the jitter values:

 library(ggplot2) a <- c(1,2,3,4,5,6,1,2,3,4,5,6) # b <- c("loss", "draw", "win", "draw", "loss", "win", "loss", "draw", "win", "draw", "loss", "win") b <- c(2, 1, 3, 1, 2, 3, 2, 1, 3, 1, 2, 3) c <- c(2, 3, 5, 4, 4, 5, 4, 4, 3, 5, 2, 4) d <- c(rep("x", 6), rep("y", 6)) temp <- data.frame(a,b,c,d) set.seed(2016) jitterVal <- runif(12, max = 0.25) jitterVal <- jitterVal * ifelse(temp$d == "x", -1, +1) ggplot(temp, aes(x = b + jitterVal, y = c, fill = d, colour = d)) + geom_point() + geom_line(aes(group = a)) + scale_x_continuous(breaks = c(1, 2, 3), labels = c("draw", "loss", "win")) + xlab(NULL) + expand_limits(x = c(0.5, 3.5)) 
+1
source

You can use the interaction between d and b :

 p <- ggplot(temp, aes(x=interaction(d, b), y=c, fill=d, colour=d))+ theme_classic()+ geom_point() p + geom_line(aes(group=a),colour=1) 

with the correct x axis. Convert x to a numeric value and set new labels

 p <- ggplot(temp, aes(x=as.numeric(interaction(d,b)), y=c, fill=d, colour=d))+ theme_classic()+ geom_point() p <- p + geom_line(aes(group=a),colour=1) p + scale_x_continuous(breaks = c(1.5,3.5,5.5), labels = levels(temp$b)) 

enter image description here

+1
source

All Articles