Home > other >  How can I use the ggplot function to visualise grouped data?
How can I use the ggplot function to visualise grouped data?

Time:02-01

I have a data set which has the time taken for individuals to read a sentence (response_time) under the experimental factors of the condition of the sentence (normal or visually degraded) and the number of cups of coffee (caffeine) that an individual has drunk. I want to visualise the data using ggplot, but with the data grouped according to the condition of the sentence and the coffee drunk - e.g. the response times recorded for individuals reading a normal sentence and having drunk one cup of coffee. This is what I have tried so far, but the graph comes up as one big blob (not separated by group) and has over 15 warnings!!

participant condition response_time caffeine 
        <dbl> <fct>             <dbl>    <fct>            
1           1 Normal              984      1                
2           2 Normal             1005      1                
3           3 Normal              979      3                
4           4 Normal             1040      2                
5           5 Normal             1008      2                
6           6 Normal              979      3                
> 

tidied_data_2 %>%
  ggplot(aes(x = condition:caffeine, y = response_time, colour = condition:caffeine))  
  geom_violin()  
  geom_jitter(width = .1, alpha = .25)  
  guides(colour = FALSE)  
  stat_summary(fun.data = "mean_cl_boot", colour = "black")  
  theme_minimal()  
  theme(text = element_text(size = 13))  
  labs(x = "Condition X Caffeine", y = "Response Time (ms)")

Any suggestions on how to better code what I want would be great.

CodePudding user response:

As a wiki answer because too long for a comment.

Not sure what you are intending with condition:caffeine - I've never seen that syntax in ggplot. Try aes(x = as.character(caffeine), y = ..., color = as.character(caffeine)) instead (or, because it is a factor in your case anyways, you can just use aes(x = caffeine, y = ..., color = caffeine)

If your idea is to separate by condition, you could just use aes(x = caffeine, y = ..., color = condition), as they are going to be separated by x anyways.

of another note - why not actually plotting a scatter plot? Like making this a proper two-dimensional graph. suggestion below.

library(ggplot2)
library(dplyr)

tidied_data_2  <- read.table(text = "participant condition response_time caffeine        
1           1 Normal              984      1                
2           2 Normal             1005      1                
3           3 Normal              979      3                
4           4 Normal             1040      2                
5           5 Normal             1008      2                
6           6 Normal              979      3", head = TRUE)

tidied_data_2 %>%
  ggplot(aes(x = as.character(caffeine), y = response_time, colour = as.character(caffeine)))  
## geom_violin does not make sense with so few observations
#  geom_violin()  
## I've removed alpha so you can see the dots better
  geom_jitter(width = .1)  
  guides(colour = FALSE)  
  stat_summary(fun.data = "mean_cl_boot", colour = "black")  
  theme_minimal()  
  theme(text = element_text(size = 13))  
  labs(x = "Condition X Caffeine", y = "Response Time (ms)")
              

enter image description here

what I would rather do

tidied_data_2 %>%
## in this example as.integer(as.character(x)) is unnecessary, but it is necessary for your data sample
  ggplot(aes(x = as.integer(as.character(caffeine)), y = response_time))  
  geom_jitter(width = .1)  
  theme_minimal()  

enter image description here

  •  Tags:  
  • Related