0
votes

In a ggplot (geom_bar), I'm looking to plot the zero-values in a different color.

Code for the bar-graph itself:

ggplot(Rodeococha, aes(x=Age ,y=Quantity)) + 
geom_bar(color="dark red", stat = "identity")

And using the instructions for colouring specific values found on a different page I tried cutting my values into intervals and constructed:

 ggplot(data= Rodeococha, aes(x= Age ,y= Quantity)) + 
 geom_bar(aes(colour = cut(qsec, c(-Inf,0,Inf))), stat = "identity") +
  scale_colour_manual(name = "qsec", values = c("(-Inf,0]" = "black",
                                                 "(0,Inf]" = "red"))

Atm it gives the error

Error in cut(qsec, c(-Inf, 0, Inf)) : object 'qsec' not found.

Before this error, it also gave a few other errors so instead of taking even more time tackling this one error I thought why not ask advice, maybe there is someone else with a better idea.

Edit: the answer from @Tjebo worked.

For clarification to others: the plot is actually a stacked plot with 7 x-axes each containing multiple bars. This code was just the first x-axis. Showing the zeros in a different color was to make interpretation more easy.

1

1 Answers

0
votes

Your code is not reproducible, I am therefore using another data set. First, bar graphs may not be appropriate here. It's difficult to show 'zeros' with bar graphs. I am increasing the line size in order to show the effect, and you will see that this has a quite undesired side effect.

For your question, just use a conditional statement as aesthetic. See below

If this is not what you want, provide better sample data and a desired output.

library(ggplot2)
ggplot(mtcars, aes(x= cyl,y= vs)) + 
  geom_bar(stat = "identity", size = 3, aes(color = vs == 0)) +
  scale_colour_manual(name = "vs", values = c(`TRUE` = 'black',`FALSE` = "red"))

Created on 2020-03-30 by the reprex package (v0.3.0)