I'm making a plot of Y vs X for some discrete points in R. There is a third variable Z that has a value of either A or B, so I want to identify the third variable by pch in the plot. I can do the following.
plot(X, Y, pch=c(3,4)[Z])
However, I don't know whether Z=="A" is assigned to 3 or 4 in this case. How do I specify that Z=="A" is plotted as 4 and that Z=="B" is plotted as 3?
Thanks in advance.
You can subset from c(3, 4)
based on value of Z
like :
c(3,4)[(Z == "A") + 1]
so when Z <- "A"
Z <- "A"
c(3,4)[(Z == "A") + 1]
#[1] 4
and when Z <- "B"
Z <- "B"
c(3,4)[(Z == "A") + 1]
#[1] 3
So the code would be
plot(X, Y, pch = c(3,4)[(Z == "A") + 1])
Another option is to use ifelse
plot(X, Y, pch = ifelse(Z == "A", 4, 3))