2015-06-12 60 views
3

我有溫度的曲線圖:如何讓x軸在ggplot2的非零位截取y?

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

不過,我想做的是有截距在50攝氏度y軸的x軸,使得50度以下的值被向下拉伸。理想情況下使用漸變填充比例,以使高值爲紅色,低值爲藍色。

我該如何做到這一點與ggplot?

enter image description here

回答

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') 

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

編輯:換色,低=藍色和高=紅

(!)