Change default colors in ggplot bar plot

369 Views Asked by At

I use the following code to generate the following plot:

ggplot(CellSite_means, aes(x=CellSite, y=MYCA, fill=Cell)) + geom_col() + theme(axis.text.x = element_text(angle=90))

My original plot with default colors

But the default blue color scheme is way too subtle. Instead I want a wider variety of colors for each cell (cells are numbered and there is an A and B site within each). Preferably I would like to use the colorblind palette. Even better, I would want the color-blind palette to be my default for all ggplots. I tried using the following

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
ggplot(CellSite_means, aes(x=CellSite, y=MYCA, fill=Cell)) + geom_col() + theme(axis.text.x = element_text(angle=90)) + scale_fill_manual(name="Cell",values=setnames(cbPalette, 1:6)) and a bunch of other iterations and palettes, but can't make it change. 

This must be really simple, please help.

1

There are 1 best solutions below

0
On

You can set the default colour scale for ggplot2 in the global options. Example below.

library(ggplot2)

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

default_colour <- function(...) {
  scale_colour_manual(
    values = cbPalette,
    ...
  )
}

options("ggplot2.discrete.colour" = default_colour)

ggplot(mpg, aes(cty, hwy, colour = class)) +
  geom_point()

The same thing also works for "ggplot2.discrete.fill", "ggplot2.continuous.colour" and "ggplot2.continuous.fill".