weblover
weblover

Reputation: 371

separate colors in plot

I have a geom_tile plot with different colors and each one represent a value in the data.

I want to separate each range of colors in the plot by a line in a color different from the plot colors.

e.g(-0.5<=0<0.5 colored red)
e.g(0<0.5<1 colored blue)

Can this be done in R?

i need these ranges to be separated by a line on geom_tile. Thanks in advance.

Upvotes: 0

Views: 523

Answers (1)

Richie Cotton
Richie Cotton

Reputation: 121067

Use the colour argument to change the colour of the borders.

An example adapted from one on ?geom_tile.

pp <- function (n,r=4) {
      x <- seq(-r*pi, r*pi, len=n)
      df <- expand.grid(x=x, y=x)
      df$r <- sqrt(df$x^2 + df$y^2)
      df$z <- cos(df$r^2)*exp(-df$r/6)
      df
     }
p <- ggplot(pp(20), aes(x=x,y=y)) + 
  geom_tile(aes(fill = z), colour = "black")
p

Or did you mean "I want to specify the fill colours for different groups of tiles". One way to do this is to convert the fill variable to be a factor and call scale_fill_manual.

dfr <- pp(20)
dfr$discrete_z <- cut(dfr$z, c(-1, 0, 1)) # makes z a factor

p <- ggplot(dfr, aes(x=x,y=y)) + 
  geom_tile(aes(fill = discrete_z)) + 
  scale_fill_manual(
    values = c("red", "blue")
  )
p

Upvotes: 1

Related Questions