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...
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.
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.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With