How to use custom colors

How can I use my custom colors "my_cols" within the codes extracted from https://www.stat.auckland.ac.nz/~paul/Reports/GraphicsEngine/vecpat/vecpat.html

library("ggplot2")
library("gggrid")
#> Loading required package: grid

my_cols <- c("Black", "Blue")

gradientPoints <- function(data, coords) {
  gradients <- lapply(data$colour,
                      function(x) {
                        radialGradient(c("white", x),
                                       cx1=.7, cy1=.7,
                                       group=FALSE)
                      })
  pointsGrob(coords$x, coords$y, pch=21,
             gp=gpar(fill=gradients, col=NA))
}
gradientKey <-  function(data, ...) {
  gradient <- radialGradient(c("white", data$colour), cx1=.7, cy1=.7,
                             group=FALSE)
  pointsGrob(.5, .5, pch=21,
             gp=gpar(fill=gradient, col=NA))
}
mtcars$am <- as.factor(mtcars$am)
ggplot(mtcars) +
  grid_panel(gradientPoints,
             mapping=aes(x=disp, y=mpg, colour=am),
             key_glyph=gradientKey, show.legend=TRUE)

The same as any ggplot, + scale_color_manual(values = my_cols).

2 Likes

Perfect. I never imagined it could work out. Thank you.

Any idea about changing the shape/pch to 17 or 15 both for legend and plot. Or use multiple shapes like pch = c(21, 17), or shape = c(21, 17)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.