2017-09-03 237 views
1

我試圖將我的圖的圖例比例設置在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]第一:

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

+0

謝謝。這真的解決了我的問題。 – Joe