Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color in R geom_raster?

I am trying to change the color in a heatmap graph. I want the Yellow, Red color scheme instead of the default Blue-Darkblue.

My code

a=as.matrix(volcano, ncol=ncol(volcano))
row.names(a)= 1:nrow(a)
library(reshape2)
a1 = melt(a)
colnames(a1) = c('X','Y','value')
head(a1)

library(ggplot2)
ggplot(a1,aes(x = X, y = Y)) +
  geom_raster(aes(fill = value),interpolate=TRUE) +
  scale_colour_gradient2(low="yellow", high="red", guide="colorbar")

I am getting the following output, which I want in a different color scheme. I am not sure on why the last line - scale_colour_gradient is not working. All other examples here have this line but with geom_tile function.

Output plot of above code

like image 576
Raghav Agarwal Avatar asked Nov 18 '22 13:11

Raghav Agarwal


1 Answers

When you are using fill in aesthetics, you have to use scale_fill_gradient for filling with color. When color is specified in aesthetics, you have to use scale_color_gradient.

like image 147
Gevorg Atanesyan Avatar answered Jan 06 '23 02:01

Gevorg Atanesyan