How to color a single histogram bar in ggplot

I found similar questions online, however I seem to be having problems because I am already using fill to color the rest of the bars in the histogram; So when I try to add a manual command to color that separate bar, it overrides it and produces an error.

Here is my ggplot code. I added a column called X$Test that has a single TRUE value at indel_size = 0 where I want to color the bar... Below is the main code, then after that is what I have tried

ggplot(data=X, aes(x=indel_size, y=Fq2, fill = indel_size))+
  geom_bar(stat="identity")+
  scale_fill_gradient2(low="dodgerblue", mid="royalblue", high = "royalblue4")+ 
  ylab("Indel Frequency")+
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())+
  scale_color_continuous(labels = c(-5, 0, 5))+
  scale_x_continuous(name = "Indel Size", limits = c(-50,10),
                     breaks = seq(from = -50, to = 10, by = 10))+
  scale_y_continuous(name = "Indel Frequency", limits = c(0,0.5))

Here is the command I tried:

+scale_fill_manual(values = 0, aesthetics = c("red", "fill"))

With the error:

Scale for 'fill' is already present. Adding another scale for 'fill', which will
replace the existing scale.
Error: Continuous value supplied to discrete scale
In addition: Warning message:
Removed 74 rows containing missing values (position_stack).

Thanks!

Hi there,

The simplest way to colour a single element in a plot is just to add a new element of a different colour over the existing one. You can try adding something like this:

ggplot(data=X, aes(x=indel_size, y=Fq2, fill = indel_size))+
  geom_bar(stat="identity")+
  scale_fill_gradient2(low="dodgerblue", mid="royalblue", high = "royalblue4")+ 
  ylab("Indel Frequency")+
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())+
  scale_color_continuous(labels = c(-5, 0, 5))+
  scale_x_continuous(name = "Indel Size", limits = c(-50,10),
                     breaks = seq(from = -50, to = 10, by = 10))+
  scale_y_continuous(name = "Indel Frequency", limits = c(0,0.5)) + 

# Here is what you need
geom_bar(aes(data = filtered_data), stat="identity")

Hope this helps.

1 Like

Yes after messing with for a little I got it. Thanks so much. This solution is so much more simpler than everything I have come across

Happy to be of help.

I had the same problem and spent two days figuring it out until someone pointed this out to me.

Just for future browsers, the line of code was something like this. First filtering the main dataframe to contain the single point, then applying your suggestion:

Y <- X[X$indel_size == 0, ]
geom_bar(data = Y,  aes(x=indel_size, y=Fq2), stat="identity", fill = "firebrick")
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.