[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.

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.

Continue reading: Simple custom colour palettes with R ggplot graphs

Long-Term Implications and Future Developments of Colour Palette in R Using ggplot Library

In the original article, the author outlines an effective method for setting custom colour palettes in R using the ggplot library, showcasing how this technique can be applied to various types of graphs such as scatter plots, bar/histograms, box plots, and even more unique types of charts such as 3.14 chart. R is understood to be a continual anchor in the development of data science and data visualization, with ggplot2 being a particularly popular choice for creating comprehensive and aesthetically pleasing data visualizations.

Future Developments

With the ever-evolving field of data science, it is not too presumptive to assume that there will be continual enhancements to the functionalities of R and its libraries, including ggplot2. This might include more aesthetic flexibility, optimizations for larger datasets, and perhaps even integrations with other programming languages or tools.

Additionally, with increasing interest in data visualization in other emerging technology areas such as machine learning and AI, the ways in which tools like ggplot2 in R are used may expand considerably.

Actionable Advice

R-users, particularly those often engaged in creating data visualizations, should make an effort to stay updated with the latest improvements and updates to the R language and ggplot2 library. This could be done through following relevant blogs, participating in user groups or online forums, or taking refresher courses as needed.

Secondly, considering the importance of aesthetic appeal in data presentation, users are encouraged to learn and apply custom color palettes when generating their ggplot2 visualizations. This aids in making their work not only accurate but also visually appealing, thereby increasing its potential impact.

Lastly, users should also look into how to effectively embed these visualizations in other digital outputs such as websites or applications, given how data consumption is increasingly moving towards these avenues.

Read the original article