Search code examples
rggplot2line-plot

Error in plotting line plot in ggplot2, I think it somehow is not grouping my groups properly


I'm just trying to make a simple line plot with two conditions: Standard and Deviant.

The Data in the csv originally look something like this:

enter image description here

And yes, time is supposed to be negative. Time is a variable here that goes from -100 ms (100 ms before the event happened) to 1500 ms (1500 ms after the event happened). Essentially, what I am trying to do is plot how the values (which I will later call amplitude) change over time for both Standard and Deviant Conditions. Something that looks kind of like this: enter image description here

Unfortunately, what I'm getting is this:

enter image description here

Here is my code:

# Libraries
library(ggplot2)

# Plotting
ggplot(data=PupilERP, aes(x=Pt, y=Amplitude, group=Condition)) +
  geom_line() + scale_y_continuous(breaks = seq(-5,15,1)) + scale_x_continuous(breaks = seq(-100,1500,100)) 

Edit: As asked for in the comments, here is the sample data once I have gotten past line 8- occurs after this line colnames(PupilERP) <- c("Pt","Deviant","Standard")

enter image description here

Also, someone else asked for the dput output. Tt was way too long at this point to give you the data (after the colnames line), even up to just 20 points, so after I have done ALL of the reshaping, after here is the actual dput output.

structure(list(Pt = c(13L, 110L, 109L, 108L, 107L, 106L, 105L, 
104L, 103L, 102L, 101L, 99L, 98L, 97L, 96L, 95L, 94L, 93L, 92L, 
91L), Condition = c("Deviant", "Deviant", "Deviant", "Deviant", 
"Deviant", "Deviant", "Deviant", "Deviant", "Deviant", "Deviant", 
"Deviant", "Deviant", "Deviant", "Deviant", "Deviant", "Deviant", 
"Deviant", "Deviant", "Deviant", "Deviant"), Amplitude = c(0.0089, 
-0.0066, -0.0076, 0.0105, 0.0514, 0.111, 0.178, 0.2396, 0.2851, 
0.306, 0.2999, 0.2708, 0.2277, 0.1796, 0.1318, 0.085, 0.0399, 
0.0012, -0.0264, -0.0413)), row.names = c(NA, 20L), class = "data.frame")

Solution

  • The problem has probably arisen in your data processing. Your as.integer call on the Pt column is creating the wrong numbers. This is because after your transposition of it, the Pt variable has become a factor, so '-11' for example has been interpreted as a factor of level 15 (for example) - this has probably in your data led to a duplication of points (you'll notice there are no negative numbers in your graph).

    To solve this, before calling as.integer, coerce Pt to a character vector. I've used dummy data to do the following (your problem was not reproducible from the dput part above):

    library(ggplot2)
    library(tidyr)
    
    # dummy data
    df <- read.csv("test.csv")
    
    df <- t(df)
    df <- as.data.frame(df)
    df <- df[-1,]
    colnames(df) <- c("Pt","Deviant","Standard")
    df$Pt <- as.integer(as.character(df$Pt))  # the key change - will read neg. numbers
    df <- gather(df, Condition, Amplitude, Deviant:Standard)
    df$Amplitude <- as.numeric(df$Amplitude)
    
    ggplot(df, aes(Pt, Amplitude, colour = Condition)) + geom_line()
    
    

    Hopefully that will help solve some problems.