Simple custom colour palettes with R ggplot graphs
[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A simple, yet effective way to set your colour palette in R using ggplot library.
library(ggplot2) set.seed(2908) my_palette <- c("red", "limegreen", "#3357FF", "goldenrod1", "#33FFFF", "brown") data <- data.frame( x = 1:25, y = rnorm(25), group = rep(c("A", "B", "C", "D", "E"), each = 5) )
After that, we can start “chaining” ggplot graphs.
Scatter plot
ggplot(data, aes(x = x, y = y, color = group)) + geom_point(size = 3) + scale_color_manual(values = my_palette, na.value = "grey45") + theme_minimal()
Barchart / Histogram
ggplot(data, aes(x = x, y = y, color = group, fill=group)) + geom_bar(stat = "identity") + scale_color_manual(values = my_palette, na.value = "grey45") + theme_minimal()
Boxplot
ggplot(data, aes(x = x, y = y, fill=group)) + geom_boxplot() + scale_color_manual(values = my_palette, na.value = "grey45") + theme_minimal()
3.14 chart
data2 <- data.frame ( y = c(2,15,24,9,17,2), group = LETTERS[1:6]) ) ggplot(data2, aes(x='', y=y, fill=group)) + geom_bar(stat="identity", width=1, colour="white") + scale_color_manual(values = my_palette, na.value = "grey45") + coord_polar("y", start=0) + theme_void()
Finally custom colours! As always, the complete code is available on GitHub in Useless_R_function repository.
Enjoy R-scripting and stay healthy!
To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.