Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying colours other than blue to bin2d

Tags:

r

colors

ggplot2

How can I apply colours different from the default blues to geom_bin2d plots?

library(ggplot2)
x <- rnorm(100000)
y <- rnorm(100000)
df <- data.frame(x,y)
p <- ggplot(df, aes(x, y)) 
p <- p + stat_bin2d(bins = 200)
p + scale_colour_gradientn(limits=c(0,50), breaks=c(0,10,20,30,40), colours=rainbow(4))

but apparently ggplot2 just adds a second scale to the plot without actually using it. I intended to replace the default-scale...

like image 859
user3021207 Avatar asked Nov 22 '13 10:11

user3021207


People also ask

How do I specify colors in ggplot2?

To specify colors of the bar in Barplot in ggplot2, we use the scale_fill_manual function of the ggplot2 package. Within this function, we need to specify a color for each of the bars as a vector. We can use colors using names as well as hex codes.

How do you add color to R studio?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.

How do I change the color of my palette in R?

It is also possible to set up a new color palette with the palette() function. This can be achieved by specifying an argument to palette() that is either a character vector of colors (color names or hex colors) or a single character value that gives the name of a predefined palette.

How do you change the color of a graph in R?

Change ggplot colors by assigning a single color value to the geometry functions ( geom_point , geom_bar , geom_line , etc). You can use R color names or hex color codes. Set a ggplot color by groups (i.e. by a factor variable). This is done by mapping a grouping variable to the color or to the fill arguments.


1 Answers

You need to use scale_fill_gradientn(), since stat_bin2d has a fill aesthetic:

p + scale_fill_gradientn(limits=c(0,50), breaks=seq(0, 40, by=10), colours=rainbow(4))

scale_fill_gradientn

like image 110
rcs Avatar answered Sep 28 '22 01:09

rcs