rggplot2anova

how to use ggplot to map data with two factors?


df <- data.frame(mean_rt = c(270.4860, 377.4106, 274.7234, 371.7627),  sd_rt = c(33.66426, 53.43924, 35.04275, 51.0443), block = c(1, 1, 2, 2), task = c(1, 2, 1, 2)) 

Above is a summary table I made for my data. I'm trying to map it onto a line graph. Here's a picture to show you what I want it to look like:

Image 1

Basically, I want the block variable on the x axis and the rt variable on the y axis with error bars.

To achieve this, I wrote the following code:

ggplot(summary_rt, aes(block, mean_rt, color = task)) +
  geom_line() +
  geom_errorbar(aes(ymin=mean_rt-sd_rt, ymax=mean_rt+sd_rt))

And here's what I ended up with: Image 2

How do I fix this? Thanks in advance.


Solution

  • You want to treat block and task as factors. Since the columns in the data frame are numeric, ggplot2 is using them as continuous variables. You could mutate() them to coerce the types to character or factor, or call factor() in your plot function like:

    library(ggplot2)
    library(dplyr)
    
    
    df %>%
      mutate(across(c(block, task), factor)) %>%
      ggplot(aes(block, mean_rt, color = task, group = task)) +
      geom_line() +
      geom_errorbar(aes(ymin = mean_rt-sd_rt,
                        ymax = mean_rt+sd_rt),
                    width = 0.2) +
      geom_point() +
      labs(x = 'block',
           color = 'task')
    

    Created on 2023-09-23 with reprex v2.0.2