3

I am trying to connect the points from geom_jitter().

 df<-data.frame(x = c(1, 1, 2, 2, 3, 3), 
           y = c(1, 1, 2 , 3, 6, 5), 
           z = c("A", "B", "A", "B", "A", "B")) 
ggplot(df, aes(x = x, y = y, col = z)) + 
  geom_jitter() + 
  geom_line()

Now the lines and points are not connected.

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • 1
    I might be misunderstanding this but if you want to connect them then why do you use `geom_jitter` instead of `geom_point` ? `ggplot(df, aes(x = x, y = y, col = z)) + geom_point() + geom_line()` – Ronak Shah Sep 04 '19 at 04:43
  • `geom_jitter` is used to visualize the distribution of values for a categorical variable. – neilfws Sep 04 '19 at 04:51
  • Thanks, @RonakShah. My real graph has error bars, I would like to show them. – Zhiqiang Wang Sep 04 '19 at 05:09

2 Answers2

6
library(dplyr)
ggplot(df %>% mutate(x = jitter(x), y = jitter(y)), 
       aes(x = x, y = y, col = z)) + 
  geom_point() + 
  geom_line()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

Add jitter (some bias) to your data and then plot biased one.