Skip to content

Instantly share code, notes, and snippets.

@grigory93
Last active September 16, 2017 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grigory93/ba4dca9636b4a6228ce5a8d5c0167968 to your computer and use it in GitHub Desktop.
Save grigory93/ba4dca9636b4a6228ce5a8d5c0167968 to your computer and use it in GitHub Desktop.
How to expand color palette with ggplot and RColorBrewer
library(ggplot2)
data(mtcars)
ggplot(mtcars) +
geom_bar(aes(factor(cyl), fill=factor(cyl)))
ggplot(mtcars) +
geom_bar(aes(factor(cyl), fill=factor(cyl))) +
scale_fill_brewer()
ggplot(mtcars) +
geom_bar(aes(factor(cyl), fill=factor(cyl))) +
scale_fill_brewer(palette="Set1")
ggplot(mtcars) +
geom_histogram(aes(factor(cyl), fill=factor(cyl)), stat="count")
ggplot(mtcars) +
geom_bar(aes(factor(hp), fill=factor(hp))) +
scale_fill_brewer(palette="Set2")
colourCount = length(unique(mtcars$hp))
getPalette = colorRampPalette(brewer.pal(9, "Set1"))
ggplot(mtcars) +
geom_bar(aes(factor(hp)), fill=getPalette(colourCount)) +
theme(legend.position="right")
ggplot(mtcars) +
geom_bar(aes(factor(hp), fill=factor(hp))) +
scale_fill_manual(values = getPalette(colourCount))
ggplot(mtcars) +
geom_bar(aes(factor(hp), fill=factor(hp))) +
scale_fill_manual(values = colorRampPalette(brewer.pal(8, "Accent"))(colourCount),
guide = guide_legend(nrow=2)) +
theme(legend.position="bottom")
ggplot(mtcars) +
geom_bar(aes(factor(hp), fill=factor(hp))) +
scale_fill_manual(values = getPalette(colourCount),
guide = guide_legend(nrow=2)) +
theme(legend.position="bottom")
library(ggthemes)
library(scales)
show_col(solarized_pal()(8))
ggplot(mtcars) +
geom_bar(aes(factor(hp), fill=factor(hp))) +
scale_fill_manual(values = colorRampPalette(solarized_pal()(8))(colourCount),
guide = guide_legend(nrow=2)) +
theme(legend.position="bottom")
library(RColorBrewer)
display.brewer.all()
display.brewer.pal(9, "Set1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment