Last active
September 16, 2017 22:49
-
-
Save grigory93/ba4dca9636b4a6228ce5a8d5c0167968 to your computer and use it in GitHub Desktop.
How to expand color palette with ggplot and RColorBrewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
data(mtcars) | |
ggplot(mtcars) + | |
geom_bar(aes(factor(cyl), fill=factor(cyl))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(mtcars) + | |
geom_bar(aes(factor(cyl), fill=factor(cyl))) + | |
scale_fill_brewer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(mtcars) + | |
geom_bar(aes(factor(cyl), fill=factor(cyl))) + | |
scale_fill_brewer(palette="Set1") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(mtcars) + | |
geom_histogram(aes(factor(cyl), fill=factor(cyl)), stat="count") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(mtcars) + | |
geom_bar(aes(factor(hp), fill=factor(hp))) + | |
scale_fill_brewer(palette="Set2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(mtcars) + | |
geom_bar(aes(factor(hp), fill=factor(hp))) + | |
scale_fill_manual(values = getPalette(colourCount)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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