1

Here's my R code

ggplot(dat = Table, aes(x = Group, y = value, fill = Type)) +
    geom_boxplot(alpha=0.08)+
    geom_jitter()+
    scale_fill_brewer(palette="Spectral")+
    theme_minimal()

enter image description here

Like you can see the dots are in the middle of the boxplots. What can I add in geom_jitter to have each point in the righ boxplot and not in the middle like this ? I also tried geom_point, it gave the same result !


Thanks to the help now It works, but I wanted to add a line to connect the dots and I got this.. can someone tell how to really connect the dots with lines

enter image description here

learners
  • 195
  • 1
  • 12
  • Does each point appear once in each Group, with the same Type? – Jon Spring Dec 08 '22 at 21:03
  • Each point correspond to ONE type and appear two times in Group. It's the same subject who is always ONE type (for example a Male or Female) but he takes the placebo and the treatment – learners Dec 08 '22 at 21:06
  • (including sample data in your question is a great way to make these things explicit upfront, and to save potential answerers from having to guess) – Jon Spring Dec 08 '22 at 21:12

2 Answers2

2

I think if you group by interaction(Group, Type) and use position_jitterdodge() you should get what you're looking for.

ggplot(mtcars, aes(as.character(am), mpg, color = as.character(vs),
                   group = interaction(as.character(vs), as.character(am)))) +
  geom_boxplot() +
  geom_jitter(position = position_jitterdodge())  # same output with geom_point()

enter image description here


Edit - here's an example with manual jittering applied to data where the each subject appears once in each Group.

I looked for a built-in way to do this, and this answer comes close, but I couldn't get it to work in terms of using position_jitterdodge with position defined by the groups of Group/Type, but line grouping defined by id alone and not by Group/Type. Both aesthetics (position adjustment and series identification) rely on the same group parameter, but they each need a different value for it.

Table = data.frame(id = 1:4, 
                   value = rnorm(8),
                   Group = rep(c("a","b"), each = 4),
                   Type = c("1", "2"))
library(dplyr)
Table %>%
  mutate(x = as.numeric(as.factor(Group)) + 
           0.2 * scale(as.numeric(as.factor(Type))) +
           rnorm(n(), sd = 0.06)) %>%
  ggplot(aes(x = Group, y = value, fill = Type, group = interaction(Group, Type))) +
  geom_boxplot(alpha=0.2)+
  geom_point(aes(x = x)) +
  geom_line(aes(x = x, group = id), alpha = 0.1) +
  scale_fill_brewer(palette="Spectral")+
  theme_minimal()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Ah this looks like a nice plot - I realise that mine removes the jitter entirely. If this is what you're looking for then I'd recommend Jon's answer – Andy Baxter Dec 08 '22 at 20:41
  • greaaat ! thank you. I want to connect the point with a line (since it's the same subject in different condition). I used 'geom_line(aes(group=ID))' but the line also start in the center just like the point before. Do you know how to fix this? – learners Dec 08 '22 at 20:42
  • You want to connect the jittered points? If so, here's a way: https://stackoverflow.com/questions/57781581/lines-to-connect-geom-jitter-points-in-ggplot2/57782271#57782271 – Jon Spring Dec 08 '22 at 20:45
  • Yes I know. To connect the dots we only need to add "geom_line(aes(group=ID))" but the line are not really connecting the dot in a good way (see the figure i added in my question above). Do you know how to fix that? – learners Dec 08 '22 at 20:52
1

Best to use position_dodge instead if you want them to line up:

library(ggplot2)

Table <- tibble::tibble(
  Group = rep(c("A", "B"), each = 20),
  Type = factor(rep(c(1:2, 1:2), each = 10)),
  value = rnorm(40, mean = 10)
)

ggplot(dat = Table, aes(x = Group, y = value, fill = Type)) +
  geom_boxplot(alpha=0.08)+
  geom_point(position = position_dodge(width = 0.75))+
  scale_fill_brewer(palette="Spectral")+
  theme_minimal()

To add a line, make sure group = ID goes in both the geom_point and geom_line calls:

library(ggplot2)

Table <- tibble::tibble(
  Group = rep(c("A", "B"), each = 20),
  Type = factor(rep(c(1:2, 1:2), each = 10)),
  ID = factor(rep(1:20, times = 2)),
  value = rnorm(40, mean = 10)
)

ggplot(dat = Table, aes(x = Group, y = value, fill = Type)) +
  geom_boxplot(alpha = 0.08) +
  geom_point(aes(group = ID), position = position_dodge(width = 0.75))+
  geom_line(aes(group = ID), position = position_dodge(width = 0.75), colour = "grey")+
  scale_fill_brewer(palette = "Spectral") +
  theme_minimal()

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22
  • greaaat ! thank you. I want to connect the point with a line (since it's the same subject in different condition). I used 'geom_line(aes(group=ID))' but the line also start in the center just like the points before. Do you know how to fix this? – learners Dec 08 '22 at 20:45
  • look at the figure I added in my question above – learners Dec 08 '22 at 20:52
  • Nice! I think I prefer this way, cleaner. – Jon Spring Dec 09 '22 at 04:46