1

I have a scatterplot of mtcars and I was wondering if there is way to specify the color of the points corresponding to the maximum and minimum mpg values. I guess a broader way to ask the question would be is there a way to assign point color based on subgroups, ie 3 most efficient cars in green and 3 least efficient cars in red?

plot(mpg~hp, col= "red", data= mtcars)

thank you!

regents
  • 600
  • 6
  • 15

1 Answers1

0

You need to pass in a vector of colors. As soon as you know which one is min/max, this is pretty straightforward. Also notice how formula works. You should not be specifying the data.frame.

with(mtcars, ifelse(mpg %in% c(min(mpg), max(mpg)), yes = "red", no = "black"))

# used directly in plot() because we specified data = mtcars
plot(mpg ~ hp, col = ifelse(mpg %in% c(min(mpg), max(mpg)), "red", "black"), data = mtcars)
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197