2

Is there any function in r that allows the plotting of this kind of scatter plots, which separates the dots by group?

enter image description here

Here is what I have done so far:

hours = c(0.00  ,-1.78  ,-0.50  ,-2.00  ,-2.80  ,2.00   ,-0.16  ,-0.34  ,1.00   ,1.00   ,2.00   ,-1.34  ,-1.00  ,-1.10  ,-0.43  ,-0.49  ,-0.02  ,-0.91,  0.48   ,2.33   ,1.00   ,0.00   ,1.18   ,1.29   ,-1.07  ,-0.26  ,1.96   ,0.36   ,2.00   ,-0.63  ,-0.80  ,-0.70  ,-2.00  ,1.17   ,0.67   ,-3.00)
group = c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
df = data.frame(hours,group)

ggplot(df, aes(group, hours)) + geom_point(shape = 16, size = 5, position = position_jitter(w=0.3,h=0.3))

But it turns out weird:

enter image description here

Any help is really appreciated!

TYL
  • 1,577
  • 20
  • 33
  • 1
    What is the x-axis? Is the variation just added jitter? – G5W Oct 07 '17 at 11:58
  • 1
    What have you tried so far? it is typically frowned upon to post a picture of a graph and say "Make this for me please". You should take a look at [making a reprex example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – tbradley Oct 07 '17 at 12:01
  • I have tried using ggplot2 and using jitter, but it does not turn out the way I wanted it to look. – TYL Oct 07 '17 at 12:14
  • The x-axis is just group A or group B – TYL Oct 07 '17 at 12:15
  • 1
    If the x axis was just group A or group B all the points would be in a line. But I think that you answered with your previous comment, you want to add jitter to that. – G5W Oct 07 '17 at 12:17

1 Answers1

3

Here is an approach using cars data

library(tidyverse)
library(ggplot2)
data(cars)

cars %>%
  gather(variable, value) %>%
  ggplot()+
  geom_jitter(aes(x = variable, y = value), width = 0.2, height = 0)+
  geom_errorbar(data = cars %>%
            gather(variable, value) %>%
            group_by(variable) %>% 
            summarise(value  = mean(value)), aes(x = variable, ymin= value, ymax = value))+
  geom_hline(aes(yintercept = mean(value)), lty = 2)+
  theme_bw()

enter image description here

With just posted data:

ggplot(df)+
  geom_jitter(aes(x = as.factor(group), y = hours), width = 0.2, height = 0)+
  geom_errorbar(data = df%>%
                  group_by(group) %>% 
                  summarise(hours  = mean(hours)), aes(x = group, ymin= hours, ymax = hours))+
  geom_hline(aes(yintercept = mean(hours)), lty = 2)+
  theme_bw()

enter image description here

missuse
  • 19,056
  • 3
  • 25
  • 47
  • Hi, is there a way to simplify the geom_errorbar code? I have been trying to understand it for quite some time to adjust it. – TYL Oct 07 '17 at 22:06
  • `geom_errorbar` takes as input `x`, `ymin`, `ymax` and some other aesthetics, by setting `ymin == ymax`, `geom_errorbar` produces one line. The data here is just mean per group hours calculated by `df%>% group_by(group) %>% summarise(hours = mean(hours))` you can calculate this prior the plot and store in an object (like `df_mean`) and just call that object `data = df_mean`. If you are not comfortable with tidyverse you can use `aggregate` for such a task. `df_mean = aggregate (hours ~ group, data = df, mean)` and use this data when calling `geom_errorbar`. – missuse Oct 08 '17 at 07:36