Pernii
Pernii

Reputation: 31

Control jitter width to allow overlapping of categories ggplot2

I have a dataset looking like this:

   Flowers Sun_Exposition Value  Repl
1:  Tulipe            mid  87.9   Aa1
2:  Tulipe            mid  92.8   Aa2
3:  Tulipe            mid  86.4   Aa3
4:  Tulipe            mid  83.3   Aa4
5:  Tulipe            mid  91.3 Aa5-1
6:  Tulipe            mid  91.4 Aa5-2

Flowers having two categories and there is 4 different Sun exposition. For each combination I have a different number of replicates.

I would like to plot a barplot (with sd) with also the points shaped by replicates. Here is my code:

# summarize data
dataSum <- data[, .(M = mean(Value, na.rm = T), S = sd(Value, na.rm = T)),
                by = .(Flowers, Sun_Exposition)]
# the plot
p <- ggplot(dataSum, aes(x = Sun_Exposition, y = M, fill = Flowers)) + 
  geom_bar(stat = "identity", color = "black", 
           position = position_dodge(.9)) +
  geom_errorbar(aes(ymin = M, ymax = M + S), width = .2,
                position = position_dodge(.9)) +
  geom_jitter(data = data, 
              mapping = aes(x = Sun_Exposition, y = Value,
                            fill = Flowers, 
                            shape = Repl, color = Flowers),
              size = 2, 
              position = position_dodge(width = 0.9)) +
  scale_shape_manual(values = c(19,17,18,15,7,8,6,5,4,2,13,12,3))+
  scale_fill_manual(values = c("gray", "lavender")) +
  scale_color_manual(values = c("gray30", "mediumpurple"))

which gives me this plot: enter image description here

My problem is that the "spread" of the points is not as width as the width of the bar. I have tried many combination, such as using geom_point, and position_jitterdodge, putting the jitter width to 0 or negative but it never gave the results I wanted.

Thank you very much for your help!

Upvotes: 0

Views: 438

Answers (1)

Joao Pedro Macalos
Joao Pedro Macalos

Reputation: 378

You have to add a group mapping to the aesthetics of geom_jitter. On your data, I suppose you would have to add group = Flowers.

Here is an example with mtcars:

library(magrittr)
library(dplyr)
library(ggplot2)
library(forcats)

mt1 <- mtcars %>%
  group_by(cyl, am) %>%
  summarize(mean = mean(hp, na.rm = T),
            sd = sd(hp, na.rm = T))

mt2 <- mtcars %>%
  mutate(rep = paste0("rep", rep(1:8, each = 4)))

# the plot
ggplot(mt1, aes(x = as_factor(cyl), 
                y = mean, 
                fill = as_factor(am))) + 
  geom_bar(stat = "identity", color = "black", 
           position = position_dodge(.9)) +
  geom_errorbar(aes(ymin = mean, ymax = mean + sd), width = .2,
                position = position_dodge(.9)) +
  geom_jitter(data = mt2, 
              aes(x = as_factor(cyl),
                  y = hp,
                  fill = as_factor(am), 
                  shape = as_factor(rep), 
                  color = as_factor(am),
                  group = as_factor(am)),
              size = 1,
             position = position_jitterdodge(jitter.width = 0.9)) +
  scale_shape_manual(values = c(19,17,18,15,7,8,6,5,4,2,13,12,3))+
  scale_fill_manual(values = c("gray", "lavender")) +
  scale_color_manual(values = c("red", "blue"))

enter image description here

Upvotes: 1

Related Questions