我有一个温度图:
qplot( TS, TEMPERATURE, data=dataInput(), geom="bar", stat="identity", fill=TEMPERATURE) +
labs(x="Measurement date", y="Temperature (degrees C)") +
ggtitle("temperature")
但是,我想要做的是让x轴在50摄氏度截取y轴,以便向下绘制低于50度的值。理想情况下,使用渐变填充比例,以便高值为红色,低值为蓝色。
如何使用ggplot执行此操作?
答案 0 :(得分: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')
# 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))
编辑:交换颜色,低=蓝色和高=红色(!)