learners
learners

Reputation: 235

R ggplot2 : geom_jitter and fill, problem to have the dots on the right boxplot

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

Upvotes: 1

Views: 1623

Answers (2)

Jon Spring
Jon Spring

Reputation: 66870

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

Upvotes: 2

Andy Baxter
Andy Baxter

Reputation: 7636

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()

Upvotes: 1

Related Questions