0

I am making a scatterplot using two quantitative variables on the axes and I want to differentiate the plot points based on a third categorical variable called group that has four options. (B, I, IB, or BI)

plot(x,y,ylab="Accuracy",xlab="Category Learning Judgement (%)",ylim=c(0,45),xlim=c(0,90), col=group)

When I use this code I get the error message: Error in plot.xy(xy, type, ...) : invalid color name 'B'

Is there a different way to assign shape/colour by a variable? I know col=ifelse but that seems to only create two groups. From what I have tried col=c("blue","purple","red","yellow") separates them into four colours but randomly and not according to the groups I need.

I am using R studio without ggplot.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Maybe this already helps: https://stackoverflow.com/questions/7721262/colouring-plot-by-factor-in-r. Maybe add `col=factor(group)`. Coloring by group is much "messier" in base R, that's one of the main reasons people tend to use ggplot2. – MrFlick Mar 08 '21 at 19:44

1 Answers1

0

The problem is that the names of your groups are not color names and using the col= argument tells plot to take each color in turn, repeating as necessary so the the first color is the first observation, the second color is the second observation. You need to link your colors to your groups. The easiest way to do that is to use a factor to index the categories. First we need some data:

set.seed(42)
x <- runif(40)
y <- runif(40)
group <- sample(c("B", "I", "IB", "BI"), 40, replace=TRUE)

Now create the factor and use it to indicate which color to use for each group:

group <- factor(group, c("B", "I", "IB", "BI"))
clr=c("blue","purple","red","yellow")
idx <- as.numeric(group)
plot(x, y, col=clr[idx], pch=16, cex=2)

Since the data are random, the groups are not separated.

Plot

dcarlson
  • 10,936
  • 2
  • 15
  • 18
  • Thanks so much for the answer! I am very new to R so i don't totally understand what is going on here. I input what you wrote, and it came up with an error: Error in plot.xy(xy, type, ...) : object 'clr' not found. I am wondering if I was supposed to not write exactly what you put but edit it according to my dataset? If so could you point that out for me? Or if you have any idea what I am doing wrong... Thanks so much – Paige Harris Mar 08 '21 at 23:06
  • Sorry, I left a line of code out. I've added it `clr=c("blue","purple","red","yellow")`. – dcarlson Mar 09 '21 at 04:18