设置图例的颜色和比例

时间:2017-09-03 12:59:32

标签: r plot ggplot2

我试图将我的情节的图例比例设置在0和1之间。当我将limits= c(0,1)添加到scale_fill_continuous时,我得到了淡化颜色图。但是,当删除此部件时,绘图的颜色可以,但不能设置比例。

这是一个可以生成的代码:

# Create some sample data
CNP <- rnorm(48,0.4/2,0.4/6)       
Nrings <-2 # Number of rings
Nsectors<-24 # Number of sectors
day=seq(0,24*Nrings-1,by=1)
d5 <- data.frame(day,CNP)
# remove period from time coordinate
d5$x <- d5$day %% (Nsectors) +1 
# compute number of periods elapsed
d5$y <-rep(0.76,2*24) 
# to match the size of macular shots and the grid
d5$y[25:48] <-2.1

require(ggplot2)
### The plot with `limits=c(0,1)`
ggplot(d5) + 
     geom_tile(aes(x=x, y=y, fill=CNP)) +
     scale_fill_continuous(low="white", high="darkgreen",space = "rgb",na.value="grey90", limits=c(0,1)) +
     theme(axis.ticks.y=element_blank(),axis.text.y=element_blank())    

有任何解决此问题的建议吗?

1 个答案:

答案 0 :(得分:1)

您可以先将填充变量重新缩放到[0,1]范围:

library(dplyr); library(scales)

ggplot(d5 %>% mutate(CNP = rescale(CNP)))+
  geom_tile(aes(x=x, y=y, fill=CNP)) +
  scale_fill_continuous(low="white", high="darkgreen",
                        na.value="grey90")+
  theme(axis.ticks.y=element_blank(),axis.text.y=element_blank())

enter image description here