如何使用ggplot2让x轴在非零位置拦截y?

Chr*_*now 3 r ggplot2

我有一个温度图:

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

但是,我想要做的是让x轴在50摄氏度截取y轴,以便向下绘制低于50度的值.理想情况下,使用渐变填充比例,以便高值为红色,低值为蓝色.

我怎么能用ggplot做到这一点?

在此输入图像描述

arv*_*000 7

你只需要破解比例标签.

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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

# 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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑:交换颜色,低=蓝色和高=红色(!)