1

I would like to make a stacked histogram in ggplot, where each of the bars (and stacked bars) have a unique colour - using a provided hex value.

For example, take this dataframe.

Pct <- c(0.8026200, 0.1973800, 0.8316421, 0.1683579)
Site <- c("A","A","B", "B")
hex <- c("#53412F", "#B4A383", "#4E3B29", "#B6A37E")
bin <- rep(c(1,2), 2)

df <- as.data.frame(cbind(Site,Pct,hex,bin))

I would like to use the hex colours specified to colour the corresponding bars.

I have tried variations along these lines:

ggplot()+
  geom_bar(aes(y=Pct, x=as.character(Site), fill=bin), data=df, stat="identity")+
  theme_bw() +
  scale_fill_manual("Subject", values=df$hex)

but this produces a green and red colour for each plot?

Any help would be greatly appreciated. Sorry if it is a simple solution - I have not got much experience with stacked barcharts.

Thank you in advance!

camille
  • 16,432
  • 18
  • 38
  • 60
QPaps
  • 312
  • 1
  • 14
  • Very similar to this SO question: https://stackoverflow.com/questions/59430088/best-way-to-make-a-black-and-white-ggplot-bar-plot-with-multiple-stacked-section/59430529#59430529 – TheSciGuy Feb 21 '20 at 15:06
  • Does this answer your question? [Best way to make a black and white ggplot bar plot with multiple stacked sections in your bars?](https://stackoverflow.com/questions/59430088/best-way-to-make-a-black-and-white-ggplot-bar-plot-with-multiple-stacked-section) – TheSciGuy Feb 21 '20 at 15:06

1 Answers1

2

your issue comes from a little contradiction I think : you say to ggplot to attribute the aesthetic "fill" using the variable "bin". Since "bin" only have two possibility ("1" or "2"), ggplot uses only 2 colors. It uses the two firsts, which are green and red.

I am not sure what you exactly want, but if you want a distinct color for each bar, then you have either to change "bin" like in the example below, or to give another argument to "fill", for example you can just replace "fill = bin" by "fill = hex". But if you want 4 colors, then the variable used in "fill" has to have 4 different values (below, I choosed "bin", and the values are 1,2,3,4).

Example :

Pct <- c(0.8026200, 0.1973800, 0.8316421, 0.1683579)
Site <- c("A","A","B", "B")
hex <- c("#53412F", "#B4A383", "#4E3B29", "#B6A37E")

##bin is defined in order it has a different value for each bar
bin <- c(1,2,3,4)
df <- as.data.frame(cbind(Site,Pct,hex,bin))

ggplot()+
  geom_bar(aes(y=Pct, x=as.character(Site), fill=bin)
           , data=df, stat="identity")+
  theme_bw() +
  scale_fill_manual(values=hex)

Result:

enter image description here

Hope it will clarify your problem!

Francois51
  • 381
  • 2
  • 15
  • Hello @Francois51 - thank you so much for your help. Although this does nto make each section of the barplot the stated hex colour? – QPaps Feb 21 '20 at 17:01
  • My bad, I edited the code. You need to specify "values = df$hex" at the last line, like you did. I replaced it by "values = hex", since it was the same values, but it seems this doesn't work. Don't know why. Anyway, it should be fixed now? – Francois51 Feb 22 '20 at 18:30
  • Hi @Francois51, sorry for my delay in response. I have edited as you say, but still get the same output, it does not use the hes values to colour the plot. Any other ideas? I'm at a loss – QPaps Feb 25 '20 at 11:20
  • I do not get what is the problem. I just copy/pasted the code of my answer and run it again. I added the result in my answer : is this what you are looking for? If so, maybe check if you have loaded the ggplot2 library ? If you want something else, then could you re-explain what? Maybe could you show us how it looks like on your machine, if you have a different result? – Francois51 Feb 25 '20 at 13:46
  • Hi @Francois15, my appologies, perhaps I wasn't completely clear. I get the same plot as you posted above. However, what I would like is each section of the stacked barplot to be the colour of the hex provided. The hex colours in the dataframe should be creams and browns: for example the first is [here](https://www.color-hex.com/color/53412f). I am therefore, expecting the bars on the plot to be these colours not red,green,black and blue. I hope that makes sense and I have explained myself better. My appologies for the mis-understanding and thank you for your continued help. – QPaps Feb 25 '20 at 15:28
  • There is no problem, don't appologize that much, my english isn't very good, it may be the issue here ;) I edited again the code. actually, I think you need to give to values a vector-like argument. hex is, df$hex is a factor. Then, R just picks 4 colors by default. With hex, I got the right result. Yet, you choose very similar colors, and I first considered it wasn't working. Beware of the potential confusions. Is it working for you? – Francois51 Feb 25 '20 at 15:58