Question

I have been banging my head on the wall trying to figure out how to code what should be a very simple little R script. All I want to do is make a series of bars that are all equal height but different colors.

So, lets say I want to have ten equal height and width bars side-by-side but with colors picked from a list of hexadecimal color codes.

For example, I want ten bars (and I can choose a single height and width to use for all bars, let's say 10px by 2px) using the following 10 hexadecimal encodings: #646464, #62cd64, #b04664, #396164, #f6f664, #f5a664, #176b64, #00ff64, #1c0064, #921964

enter image description here

And I really want to find a way to do this using ggplot2 so that I can manipulate the theme settings. Any help is appreciated!

EDIT: I tried one of the suggestions using this code:

library(ggplot2)
library(grid)
df <- data.frame(x=1:19, y=1)
colors <- c("#ffffff", "#ffffff", "#ffffff", "#ffffff", "#aaaaaa", "#555555", "#000000", "#000000", "#000000", "#000000", "#000000", "#000000", "#000000", "#555555", "#aaaaaa", "#ffffff", "#ffffff", "#ffffff", "#ffffff")
ggplot(df, aes(x=x, y=y, fill=x)) + geom_bar(stat='identity', width=1) +
 scale_color_manual(colors) + theme_minimal() + theme(legend.position='none')

However, this for some reason arranges the colors in increasing order, instead of in the order I placed them in the vector. See here:

enter image description here

Does anyone see why this is happening?

Was it helpful?

Solution

A slightly different approach to @ilir:

dat <- data.frame(x=factor(1:10), y=1)
gg <- ggplot(dat, aes(x=x, y=y, fill=x))
gg <- gg + geom_bar(stat='identity', width=1)
gg <- gg + scale_fill_manual(values=c("#646464", "#62cd64", "#b04664", "#396164", "#f6f664", "#f5a664", "#176b64", "#00ff64", "#1c0064", "#921964"))
gg <- gg + theme_minimal()
gg <- gg + theme(legend.position='none')
gg

enter image description here

You need to use scale_fill_manual vs scale_color_manual. Also, doing the ggplot calls this way makes it way easier to adjust the code later.

UPDATE based on your new color ramp:

dat <- data.frame(x=factor(1:19, y=1))
colors <- c("#ffffff", "#ffffff", "#ffffff", "#ffffff", "#aaaaaa", "#555555", 
            "#000000", "#000000", "#000000", "#000000", "#000000", "#000000",
            "#000000", "#555555", "#aaaaaa", "#ffffff", "#ffffff", "#ffffff",
            "#ffffff")
gg <- ggplot(dat, aes(x=x, y=y, fill=x))
gg <- gg + geom_bar(stat='identity', width=1)
gg <- gg + scale_fill_manual(values=colors)
gg <- gg + theme_minimal()
gg <- gg + theme(legend.position='none')
gg

enter image description here

OTHER TIPS

You can just make a barplot from dummy data and a manual palette:

df <- data.frame(x=factor(1:10), y=1)
ggplot(df, aes(x=x, y=y, fill=x)) + geom_bar(stat='identity', width=1) + 
  scale_fill_manual(<your color vector here>) + 
  theme_minimal() + theme(legend.position='none')

You can add more options to theme to suppress axes and the rest of the elements.

Edit: Fixed an error in specifying the scale, and changed x to factor as suggested in @hrbmstr's answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top