1

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.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
blob
  • 15
  • 5

3 Answers3

2

You could factor your Z column. For example:

df = data.frame(x = 1:12, y = 12:1, z = c('A','B','C'))

df$z = as.factor(df$z)

plot(df$x, df$y, pch = c(22,23,24)[df$z])

will give you: enter image description here

R. Schifini
  • 9,085
  • 2
  • 26
  • 32
1

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))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for your help. The first option works. However, the second option gives an error: “the condition has length > 1 and only the first element will be used”. – blob Oct 08 '19 at 04:15
  • @S.J. If you have multiple value for `Z` you need `ifelse`. Updated the answer. – Ronak Shah Oct 08 '19 at 04:18
  • Or a subset from a named vector - `plot(df$x, df$y, pch=c("B"=3,"A"=4,"C"=5)[as.character(df$z)])` – thelatemail Oct 08 '19 at 04:37
1

What you can do is to plot X and Y first,

plot(X, Y)

After that, plot Z on the same plot using points(). Do it separately based on different pch.

points(X, Z[Z == "A"], pch = 4)
points(X, Z[Z == "B"], pch = 3)
TheN
  • 523
  • 3
  • 7