Reputation: 2309
I have the following colors:
c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")
I would like to make simple plot so I can see the colors, either with ggplot2
or base R.
Upvotes: 3
Views: 956
Reputation: 41
A light-weight option that requires only grid
,
grid::grid.raster(t(c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")), interp=FALSE)
Upvotes: 0
Reputation: 4524
There is maybe a more elegant way, but this shows what you want:
library(ggplot2)
df <- data.frame(
x = c(1,2,3,4,5,6),
y = rep(1,6),
c = c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")
)
ggplot(df, aes(x,y, col=c))+geom_point(size=8)
This was wrong and I know the answers from above are the better ones But I wanted to make the improvements needed to offer at least a correct answer - at least what the choice of colors is concerned:
df <- data.frame(
x = c(1,2,3,4,5,6),
y = rep(1,6),
c = c("#bbb487", "#066e9f", "#e2dfcc",
"#4e766d", "#dd8047", "#d8b25c")
)
farbe <- c("#bbb487", "#066e9f", "#e2dfcc",
"#4e766d", "#dd8047", "#d8b25c")
ggplot(df, aes(x,y, col=c))+geom_point(size=8)+
theme_bw()+
scale_color_manual(
name="Colors",
values = farbe,
breaks = c("#bbb487", "#066e9f", "#e2dfcc",
"#4e766d", "#dd8047", "#d8b25c"))
Upvotes: 1
Reputation: 206197
With ggplot2
you can do
library(ggplot2)
show_colors <- function(colors) {
ggplot(data.frame(id=seq_along(colors), color=colors)) +
geom_tile(aes(id, 1, fill=color)) +
scale_fill_identity()
}
colors <- c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c")
show_colors(colors)
which returns
Or you could turn it sideways and print the color names
show_colors2 <- function(colors) {
ggplot(data.frame(id=seq_along(colors), color=colors)) +
geom_tile(aes(1, id, fill=color)) +
geom_text(aes(1, id, label=color)) +
scale_fill_identity()
}
show_colors2(colors)
This base graphics you could do
show_colors <- function(colors) {
ncol <- length(colors)
plot(0,0, ylim=c(0, 1), xlim=c(0, ncol), type="n")
rect(0:(ncol-1), 0, 1:ncol, 1, col = colors)
}
show_colors(colors)
Upvotes: 5
Reputation: 1595
You can use colorspace
for sample plot
library(colorspace)
demoplot(c("#bbb487", "#066e9f", "#e2dfcc", "#4e766d", "#dd8047", "#d8b25c"), type = "bar")
Upvotes: 9