-2

I have data.frame of value between -10 to 10, my data.frame has 2 columns.I wanted to create a ggplot graph.

I need to give color to points which have values more than 8 or less than -8.

How can I do this by ggplot in geom_point()?

user3616494
  • 63
  • 10
  • 6
    I think what you are looking for is already in that answer here http://stackoverflow.com/questions/11838278/plot-with-conditional-colors-based-on-values-in-r - connect colour with an ifelse statement – PhiSeu Sep 14 '16 at 07:06
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Sep 14 '16 at 07:07
  • see for example http://stackoverflow.com/questions/21536835/ggplot-colour-points-by-groups-based-on-user-defined-colours or http://stackoverflow.com/questions/9827193/color-ggplot-points-based-on-defined-color-codes – Cath Sep 14 '16 at 07:11
  • use 'aes(colour)' in 'geom_point' read more in [geom_point docs](http://docs.ggplot2.org/current/geom_point.html) – cccmir Sep 14 '16 at 07:12

1 Answers1

2

I agree with the comments above, anyway I think this is what you are looking for

p <- runif(100, min=-10, max=10)
g <- 1:100
dat <- data.frame(p, g)
dat$colors <- 1
dat[which(dat$p < (-8) | dat$p > 8),"colors"] <- 0

library(ggplot2)

ggplot(dat, aes(x=g, y=p, group=colors)) + geom_point(aes(color=as.factor(colors)))

Which results in this: enter image description here

Edit: In a previous version of this answer the different colors were expressed as a continuous variable. I changed this to a dichotomous format with as.factor.

DUWUDA
  • 342
  • 5
  • 13