1

We would like to explain a continuous variable Y in terms of X1,X2,X3,X4,X5 (continuous grades from 0/20 to 20/20).

When plotting Y vs. X1, I would like to color the points in terms of the means (X1+X2+X3+X4+X5)/5 to see if the candidates that are bad in X1 are globally bad.

So, mean, varying from 0 to 20, would be blood red and gradually going to bright green for the ones having (and also indicate this in a legend). How could one proceed this?

Here is how I usually draw my scatter plots:

scatter.smooth(x=data$X1, y=data$Y1, main="Y1 ~ X1", xlab="X1", ylab="Y1")

Even better would be that each point is colored in terms of its X1 value, and has a colored "ring" around it corresponding to the color of its mean.

M--
  • 25,431
  • 8
  • 61
  • 93
W. Volante
  • 143
  • 4
  • 1
    welcome to stack. If you want someone to help you, please follow the advices here : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa or https://stackoverflow.com/help/mcve. You should provide an example of your data or a dummy example so the people can try and give you working code. – denis Apr 12 '18 at 17:22
  • STHDA has a good guide on how to set up ggplot, and [here is a link to doing the continuous color scale you are describing](http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually). – Anonymous coward Apr 12 '18 at 17:26

1 Answers1

1

If you have no objection to ggplot, it'll make these things easy.

To shade by a continuous variable aes(color=myvar), has the behavior you want straight out of the box.

Customize the colors with +scale_color_gradient(low='red', high='green')

To do the rings, draw two sets of points: first one with size 3 (or whatever) in the ring color, then dot the centers with a point of size 1.

steveLangsford
  • 646
  • 5
  • 9
  • Thank you. Really helpful. I still have a problem: myvar is the mean. But the worst mean that I have is around 6. I would like the color to start at 0, not 6 (and to finish at 20). How can I do that ? – W. Volante Apr 12 '18 at 18:20
  • I think you can just pass `limits=c(0,20)` as an argument to scale_color_gradient. – steveLangsford Apr 12 '18 at 20:11
  • Do you know how to add a layer of points using ggplot in order use your idea for the rings ? – W. Volante Apr 12 '18 at 20:33
  • You would +geom_point() twice, and pass aes() in each to give them different colors. They are layered in the order you add them. If you edit your question to include a toy data set I can just show you, but I don't want to guess what your data looks like. – steveLangsford Apr 13 '18 at 13:25