Gerg Gerg - 1 year ago 127
R Question

Setting up color manual in ggplot

I am trying to get different color for
a) -inf to 1 (excluding 1)
b) 1 (excluding 1) to inf
c) and then values with value 1
I know how to get for a) and b) , but dont know about getting for c)..

Here is what I have

scale_color_manual(name = "df",
values = c("(-Inf,1]" = "red",
# "1" = "yellow", #doesnt work
"(1, Inf]" = "green"))

Answer Source

Group your data (group COLOR) if it's == 1, < 1 or > 1. And specify color in aesthetics by this group.
Specify wanted colors in scale_color_manual.

d <- data.frame(x = sample(c(Inf, -Inf, 1), 100, replace = TRUE),
                y = rnorm(100))
d$COLOR <- "One"
d[d$x < 1, ]$COLOR <- "-INF"
d[d$x > 1, ]$COLOR <- "+INF"

library(ggplot2)
ggplot(d, aes(x, y, color = COLOR)) +
    geom_point() +
    scale_color_manual(name = "My colors",
                       values = c("red", "green", "yellow"))

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download