在R中使用ggplot2自定义颜色缩放

时间:2014-11-29 22:25:55

标签: r ggplot2

我有这段代码:

require(reshape2)
library(ggplot2)
library(RColorBrewer)

df <- read.csv("https://dl.dropboxusercontent.com/u/73950/moduVSmnc.csv")
breaks1 <- seq(1.85,2.5,by=0.05)

gg <- aggregate(mnc~cut(apl,breaks=breaks1,
labels=format(breaks1[-1],nsmall=2))+modu,df,mean)

colnames(gg)<- c("apl","modu","mnc")
gg$modu <- as.factor(gg$modu)
library(ggplot2)
library(RColorBrewer)
ggplot(gg) + 
  geom_tile(aes(x=modu,y=apl,fill=mnc))+
  scale_fill_gradientn(colours=rev(brewer.pal(10,"Spectral"))) +
  coord_fixed()

产生:

enter image description here

现在,我希望这个情节能够将40以下的所有值显示为深蓝色(如果值为0),然后开始平稳地移动到绿色,黄色,橙色,红色和深红色(酿酒师&#39) ; s光谱色标)直到最大值。

如何使用ggplot2实现这一目标?

1 个答案:

答案 0 :(得分:3)

我不确定我是否正确理解了你的问题。为什么不重新映射所有mnc&lt; 40到40并重新着色?

gg$mnc2<-gg$mnc
gg$mnc2[gg$mnc2<40]<-40


ggplot(gg) + 
  geom_tile(aes(x=modu,y=apl,fill=mnc2))+
  scale_fill_gradientn(colours=rev(brewer.pal(11,"Spectral"))) +
  coord_fixed()

enter image description here