Jon Nguyen Jon Nguyen - 6 months ago 53
R Question

Graphing 3 Variable Scatterplot R

I imported some data from Islander and am trying to graph something with 3 variables. I'm thinking of trying to graph 2 numeric variables with a nominal category (gender). The plot I'm trying to do therefore is a regular scatterplot, but color-coded.

I looked at this starter tutorial on R: Scatterplots, but didn't see any mention of 3 variable plotting.

Can anyone help me out? My variables hold values pertaining to number of balls bounced, minutes of physical activity per week, and gender.

Picture of the data:



Since gender is a binary variable (usually, otherwise ternary), I would plot a 2D scatterplot with color encoding the gender.

Dummy data:

a = data.frame(x=runif(100), y = runif(100)+2, group = round(runif(100))+1 )

Now I would plot y against x using a$group to select the color:

plot(a$y, a$x, pch = 16, col = c('cornflowerblue', 'springgreen')[a$group])



If you do have missingness I would add a third group to the color vector. Here is a bunch of other solutions for 2D scatter with color