-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Description
Hello,
I am attempting to combine boxplots and points, and connect points together. There are some old discussions saying that the input data needs to be sorted to ensure that geom_line and geom_point receive the same jittering, but this does not seem to work when there is a nested grouping:
library(tidyverse)
set.seed(77)
a1 <- tibble(ID=factor(letters[1:10]), y=runif(10), TP="A", Grp=rep(c("C","P"), 5))
a2 <- tibble(ID=factor(letters[1:10]), y=runif(10), TP="B", Grp=rep(c("C","P"), 5))
a1 <- bind_rows(a1, a2)
a1 <- mutate(a1, TP=factor(TP), Grp=factor(Grp))
a1 <- arrange(a1, ID, TP)
pj <- position_jitter(width=0.2, seed=9)
ggplot(a1, aes(x=TP, y=y, fill=Grp)) +
geom_boxplot(alpha=0.2, outlier.colour = NA) +
geom_point(position=pj, aes(colour=Grp, group=ID)) +
geom_line(position=pj, aes(group = ID, colour=Grp))
This is performing as expected. However the jittering is not separated per group (i.e pink points are not lined up with the pink boxplot). Lets use jitterdodge to achieve that.
pj <- position_jitterdodge(jitter.width=0.4, seed=9)
ggplot(a1, aes(x=TP, y=y, fill=Grp)) +
geom_boxplot(alpha=0.2, outlier.colour = NA) +
geom_point(position=pj, aes(colour=Grp, group=Grp))
Now the colours are lined up, attempt to connect together.
ggplot(a1, aes(x=TP, y=y, fill=Grp)) +
geom_boxplot(alpha=0.2, outlier.colour = NA) +
geom_point(position=pj, aes(colour=Grp, group=Grp)) +
geom_line(position=pj, aes(group = ID, colour=Grp), alpha=0.3)
As far as I can tell, none of the following combinations of sorting the input data fix the problem of lines being connected to the points:
a1 <- arrange(a1, ID, TP, Grp)
a1 <- arrange(a1, ID, Grp, TP)
a1 <- arrange(a1, Grp, ID, TP)
a1 <- arrange(a1, Grp, TP, ID)
a1 <- arrange(a1, TP, ID, Grp)
a1 <- arrange(a1, TP, Grp, ID)
Metadata
Metadata
Assignees
Labels
No labels