Force ggplot gradient to include low colors that are < 0

Viewed 164

Hi I have a plot as this.

data <- data.frame(
  name=seq ( 1, 15),  
  value=c( 4.5,  21.1,  32.8 ,  8.1, -44.1 ,-27.7 , -1.5 ,148.0, -30.6 ,143.5, 486.0,  58.5, 226.0 , 4.6  ,43.5 )
)


ggplot(data, aes(x=name, y=value, fill = value )) + 
  geom_bar(stat = "identity") + 
  scale_fill_gradient2(mid="orange", low = "red", high = "green", midpoint = -1 )

I would like all the lowest values and also < 0 to be red however in this plot here the lowest color its showing looks like orange. Is there anyway to force ggplot to include red for the lowest value, bonus would be great that its condition only if its < 0. In other words if the range was only from 0-100 then it should just be orange to green. Is this possible? thanks.

enter image description here

1 Answers

You can achieve this by setting breaks argument: (Play around)

ggplot(data, aes(x=name, y=value, fill = value )) + 
  geom_bar(stat = "identity") + 
  scale_fill_gradient(low = "red", high = "green", name="Sum", 
                      labels = c("0", "500"),
                      breaks = c(0, 400))

enter image description here

Related