0

My data looks something like this:

x = A, A, B, C, D, E, E, F, G y = 1, 0, 2, 3, 4, 5, 0, 6, 7

How do I have every alternate x-axis data point a different colour? For example, in my data, I would like to have the A, C, E and G points one colour, B, D, F another and then have the points where the value is 0 a third colour? I've used pch = and col= to change the colour and shape of everything, but is it possible to change specific points?

Or would I have to plot two different graphs on the same plot, and then change the colour of each graph, then have a third rule for the zeroes? The only problem with this is that since my variables are not numerical, and the data needs to be alternating, I can't seem to figure out how to plot it.

  • 1
    Just make `col` be a _vector_ of colors instead of a single value. – G5W Apr 05 '20 at 14:23
  • @G5W , so if i set blue<-c("A", "C", "E", "G") where in my code would I put that? I've been putting col = "blue' within my plot function plot(x, y, pch=16, col="blue"), if i had to set col as a vector how would that work? I tried points(blue, col = "blue") but it doesn't work and I'm pretty sure that's for plotting new points. – codingnewbie Apr 05 '20 at 14:51

2 Answers2

0

If this is your data:

x = c('A', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
y = c(1, 0, 2, 3, 4, 5, 0, 6, 0)

Then decide on the three colours,

cols <- c("orange2","green4","tomato3")

And create the colour vector based on your criteria. You can use ifelse to determine the index to use for the cols vector.

col <- cols[ifelse(x %in% c('A','C','E','G'), 1,  # orange
              ifelse(x %in% c('B','D','F'), 2,    # green
                  ifelse(y==0, 3,                 # tomato
                                  4)))] # no 4th colour, so NA (the points won't appear)

col
# "orange2" "orange2" "green4"  "orange2" "green4"  "orange2" "green4" "orange2" "tomato3"

The length of col is the same as your data, and because R vectorises, everything works.

plot(1:length(y), y, xaxt="n", las=1, xlab="x", col=col, pch=20, cex=3)
axis(side=1, at=1:length(y), labels=x)

enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26
  • Thank you! Is there any chance of making when y = 0 it's a different colour from the other two (and maybe even shape)? – codingnewbie Apr 05 '20 at 16:21
  • You mean override the letters in x? If so, then put the `ifelse` clause for y before the xs. Shapes are also possible with `pch=`, and the concept is the same. – Edward Apr 05 '20 at 16:25
  • see also here : https://stackoverflow.com/questions/12919816/plotting-in-different-shapes-using-pch-argument – user12256545 Apr 05 '20 at 16:29
0

It is of course possible in base R plot, I would do it using factor for your grouping variable, you wouldn't even need to give R specific colour values, because it would do it automatic for factors :

x = 1:10
y = sample(1:20, 10, replace= T)
group = factor(sample(LETTERS[1:9],10, replace= T ))
df<-data.frame(x,y,group)

#> head(df)
#  x  y group
#1 1 16     B
#2 2  7     D
#3 3 10     D
#4 4 19     I
#5 5  3     C
#6 6 13     I


plot(x,y, col=group, pch=20, cex= 2)


enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user12256545
  • 2,755
  • 4
  • 14
  • 28