0

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

JacobHad
  • 33
  • 4
  • 1
    Welcome to Stack Overflow! Can you please edit your question to include the data description in a text rather than an image format? (You can use `dput()`, or cut-and-paste from a printout in the R console ... – Ben Bolker Sep 23 '22 at 18:12
  • 2
    I think your mistake **may** be in setting `group = 2`. What happens if you delete that? What did you intend for that setting to do ? – Ben Bolker Sep 23 '22 at 18:13

1 Answers1

1

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

:)

Larissa Cury
  • 806
  • 2
  • 11
  • @Ben, should I delete it? – Larissa Cury Sep 23 '22 at 21:56
  • Why don't you also post the results when `group = 2` is used (which are wonky, not quite as wonky as the OP's graph, but this does more or less confirm that that's the problem ...) I think deleting the question as "caused by a typo" would also have been reasonable, but now that your answer is posted, I think it's useful ... – Ben Bolker Sep 23 '22 at 22:02
  • Sure, I'll do that! – Larissa Cury Sep 23 '22 at 22:05
  • thank you so much for doing this! so it has worked for one line but not the other. it still doesn't seem to connect. This may be due to missing values? If there are missing values, is there a way to keep the line connecting? – JacobHad Sep 24 '22 at 02:31
  • hi, yeah, I believe so. Take a look at this https://stackoverflow.com/questions/15533212/how-to-connect-dots-where-there-are-missing-values discussion, maybe it's helpful. As far as I'm concerned, you can either delete/omit the Nas values or imput them (but I'm still not used to performing this tecnique, I'm sorry) this one too: https://stackoverflow.com/questions/28775036/ggplot-line-graph-with-na-values n fact, I've found a lot of posts discussing the NAs issue with lineplots, I hope you can find an answer that helps you! – Larissa Cury Sep 24 '22 at 02:46
  • http://bip.bard.edu:8080/ibi_html/javaassist/intl/EN/help/gui_gamsng.htm#:~:text=In%20a%20line%20graph%2C%20missing,(transparent)%20bar%20or%20area. this discusses using dashed lines for Nas, I've never seen this approach, tho (but I'm very new to stats) – Larissa Cury Sep 24 '22 at 02:52
  • thank you so much for helping, i think ive figured it out now! slowly learning each time with R!!! :) – JacobHad Sep 24 '22 at 03:29
  • that's the spirit! I'm on the same path! :) – Larissa Cury Sep 24 '22 at 13:20