Lines do not fully connect in line graph in R

Viewed 29

I am trying to create a line graph that shows response times for a Go/No-go task. I am having trouble with the lines as they are not connecting to the intended values. I just want the 'go' to all be one line and the 'no-go' to be one line. Does anyone know how to correct this? I have included how the line graph looks currently and an example of the dataset. thank you!

enter image description here enter image description here

> ggplot(response_times, aes(x = day, y = mean, group = 2)) +
  geom_line(aes(color = trialtype)) +
  geom_point(aes(color = trialtype)) +
  theme(axis.text.x = element_text(angle = 60)) +
  labs(title="Average response time of Go/No-go Trials",
       x = "Date of training",
       y = " Average Response Time ")
# Groups:   day [28]
   day        trialtype  mean
   <chr>      <chr>     <dbl>
 1 2022-08-06 go        0.497
 2 2022-08-06 nogo      0.548
 3 2022-08-07 go        0.449
 4 2022-08-07 nogo      0.407
 5 2022-08-08 go        0.444
 6 2022-08-08 nogo      0.350
 7 2022-08-09 go        0.443
 8 2022-08-09 nogo      0.453
 9 2022-08-10 go        0.462
10 2022-08-10 nogo      0.426
# … with 46 more rows

EDIT:

So I have used the code which you have provided(thank you) but now i think there is an issue with missing values.

Warning messages:
1: Removed 1 row(s) containing missing values (geom_path). 
2: Removed 9 rows containing missing values (geom_point). 

enter image description here

1 Answers

I tried to reproduce your example from the photo, I didn't get all numbers right because I've copied it by hand, but, still, take a look:

day <- rep(c("2022-06", "2022-07", "2022-08", "2022-09", 
             "2022-10", "2022-11", "2022-12", "2022-13",
             "2022-14", "2022-15", "2022-16", "2022-17", 
             "2022-18", "2022-19", "2022-20", "2022-21"))
trialtype <- as.factor(rep(c("go", "no-go"), 16, by = 2))
# mean <- data$CONT_Y[1:8]
mean <- c(0.49, 0.54, 0.44, 0.40, 0.44, 0.34, 0.45, 0.46,
          0.42, 0.47, 0.46, 0.45, 0.26, 0.47, 0.41, 0.48)

mydata <- data.frame(day = day,
                     trialtype = trialtype,
                     mean = mean)

mydata %>% 
  ggplot(., aes(x = day, y = mean, group = trialtype)) +
  geom_line(aes(color = trialtype)) +
  geom_point(aes(color = trialtype)) +
  theme(axis.text.x = element_text(angle = 60)) +
  labs(title="Average response time of Go/No-go Trials",
       x = "Date of training",
       y = " Average Response Time ")

the plot:

plot

Next time, instead of a picture, if it's possible, then maybe you could share the dataframe via github or dput() (I still don't know how to use dput, but I've been asked to upload via it as well :)

Following Ben's comment, I've just changed group = 2 to group = trialtype

(I'm sorry, I've just checked the comment after posting, I may delete the answer if it's good practice, I'm new to the forum too)

  • edit: following Ben's suggestion, this is what you'd get with group = 2

group2

:)

Related