12

I am generating a histogram and I would like to color certain groups with specific colors. Here is my histogram:

enter image description here

I have 14 groups and I would like to color the first 7 red, the next 4 blue, and the final 3 orange. How can I do this in ggplot? Thanks.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
drbunsen
  • 10,139
  • 21
  • 66
  • 94
  • I'm assuming you mean a bar plot, not a histogram? There a (big) difference. – joran Apr 04 '12 at 17:36
  • The data is a plot of frequency from discontinuous data. I plotted it using `geom_histogram`. I'm not sure if this constitutes a "bar chart" or a "discrete histogram". – drbunsen Apr 04 '12 at 17:44
  • 2
    Ok. I'd probably just use geom_bar in that case. And then you just need a grouping variable in your data frame that defines the color grouping you want, and then map that to `fill`. There are some examples in `?geom_bar`. – joran Apr 04 '12 at 17:57
  • Thanks, I will use `geom_bar`. I thought there was probably a way to use `geom_histogram` without the need to define color groupings. – drbunsen Apr 04 '12 at 18:05
  • 1
    If you post a small reproducible example, we can give more specific advice. – Eric Fail May 10 '12 at 01:24

1 Answers1

19

UPDATED VERSION

No need to specify grouping column, ggplot command is much more compact.

library(ggplot2)
set.seed(1234)

# Data generating block
df <- data.frame(x=sample(1:14, 1000, replace=T))
# Colors
colors <- c(rep("red",7), rep("blue",4), rep("orange",3))

ggplot(df, aes(x=x)) +
  geom_histogram(fill=colors) +
  scale_x_discrete(limits=1:14)

enter image description here

OLD VERSION

library(ggplot2)

# 
# Data generating block
#
df <- data.frame(x=sample(c(1:14), 1000, replace=TRUE))
df$group <- ifelse(df$x<=7, 1, ifelse(df$x<=11, 2, 3))

#
# Plotting
#
ggplot(df, aes(x=x)) +
  geom_histogram(data=subset(df,group==1), fill="red") +
  geom_histogram(data=subset(df,group==2), fill="blue") +
  geom_histogram(data=subset(df,group==3), fill="orange") +
  scale_x_discrete(breaks=df$x, labels=df$x)

enter image description here

redmode
  • 4,821
  • 1
  • 25
  • 30
  • I found the approach ``geom_histogram(fill=colors)`` to be the only way to handle a cumulative histogram ``aes(y=cumsum(..count../sum(..count..)))``, because setting a fill inside the ``aes`` resulted in groups being stacked. The simpler ``stat_ecdf`` approach didn't work for me because it doesn't take ``breaks`` option. In the end, only this approach worked. – PatrickT Dec 18 '17 at 21:36
  • I can't reproduce this with ggplot2 3.3.2? I get `Error: StatBin requires a continuous x variable: the x variable is discrete.` Thanks! – Matifou Aug 10 '20 at 19:33