R code of scatter plot for four variables

I tried plotting ASB vs YOI for each Child grouped by Race

I got something like:

library(tidyverse)
Antisocial <- structure(list(Child = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,  4L), ASB = c(1L, 1L, 1L, 0L, 0L, 0L, 5L, 5L, 5L, 2L), Race = c(1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Y92 = c(0L, 1L, 0L, 0L,  1L, 0L, 0L, 1L, 0L, 0L), Y94 = c(0L, 0L, 1L, 0L, 0L, 1L, 0L,  0L, 1L, 0L), YOI = c(90L, 92L, 94L, 90L, 92L, 94L, 90L, 92L,  94L, 90L)), row.names = c(NA, 10L), class = "data.frame")

ggplot(data = Antisocial, aes(x = YOI, y = ASB)) + 
  geom_point( colour = "Black", size = 2) + 
  geom_line(data = Antisocial, aes(x= Child), size = 1) +
  facet_grid(.~ Race)

Plot Image I generated: https://drive.google.com/file/d/1sZVsRFiGC0dIGg0GWhHhNDCaiW2iB-ky/view?usp=sharing

Full dataset- https://drive.google.com/file/d/1UeVTJ1M_eKQDNtvyUHRB77VDpSF1ASli/view?usp=sharing

I want to use 2 charts side by side Race=0, Race= 1 to plot ASB vs YOI for each Child grouped by Race. The line, however, should only connect to dots of the same child. As it is right now, all the dots are connected. Furthermore the scale of YOI should be (90,94).

Can you suggest what change should I do?

Thanks!

Upvotes: 1

Views: 541

Answers (1)

Elias
Elias

Reputation: 736

Thanks for providing the data. I changed 4 observations to race 0 to have some variation:

library(tidyverse)
Antisocial <- structure(list(Child = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,  4L), ASB = c(1L, 1L, 1L, 0L, 0L, 0L, 5L, 5L, 5L, 2L), Race = c(1L,  1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L), Y92 = c(0L, 1L, 0L, 0L,  1L, 0L, 0L, 1L, 0L, 0L), Y94 = c(0L, 0L, 1L, 0L, 0L, 1L, 0L,  0L, 1L, 0L), YOI = c(90L, 92L, 94L, 90L, 92L, 94L, 90L, 92L,  94L, 90L)), row.names = c(NA, 10L), class = "data.frame")
        
ggplot(data = Antisocial, aes(x = YOI, y = ASB, , group = Child)) + 
    geom_point( colour = "Black", size = 2) + 
    geom_line()+
    facet_grid(.~ Race)

To connect the dots for each child, you need to include group = Child in the code. I think this is what you want? Let me know if this solved your problem :)

Upvotes: 1

Related Questions