3
votes

I have a plot of temperatures:

qplot( TS, TEMPERATURE, data=dataInput(), geom="bar", stat="identity", fill=TEMPERATURE) + 
    labs(x="Measurement date", y="Temperature (degrees C)") +
    ggtitle("temperature")

However, what I would like to do is have the x axis intercept the y axis at 50 degrees C so that values below 50 degrees are drawn downwards. Ideally with a gradient fill scale so that high values are red and low values are blue.

How can I do this with ggplot?

enter image description here

1

1 Answers

8
votes

You just have to hack the scale labels.

library(ggplot2)

# fake data
my_dat <- data.frame(x=1:(24*3), temp=sample(0:100, 24*3))

# the initial plot 
ggplot(my_dat, aes(x=x, y=temp, fill=temp)) +
  geom_bar(stat='identity')

enter image description here

# make a copy
my_dat2 <- my_dat

# pretend that 50 is zero
my_dat2$temp  <- my_dat2$temp-50

ggplot(my_dat2, aes(x=x, y=temp, fill=temp)) +
  geom_bar(stat='identity') +
  scale_fill_gradient2(low = 'blue', mid = 'white', high='red',
                       labels=seq(0,100,25)) +
  scale_y_continuous(breaks=seq(-50,50,25), labels=seq(0,100,25))

enter image description here

edit: swapping colors, low=blue and high=red (!)