我正在做一个循环来绘制几个geom_tile图。以下代码是循环的内部部分。对于某些列(我使用它绘制geom_tile的每列),值可能不会落入我设置的所有间隔中。换句话说,某些间隔有一些0计数。我使用了scale_fill_manual(values = rev(rbPal(break1)),labels = rev(cut),drop = FALSE),其中drop = FALSE应该解决问题,但事实并非如此。有人可以帮忙吗?真的很感激!感谢
library(data.table)
library(ggplot2)
color1<-c()
y1<-data[(1:(lifespan[i]*numbperday)),i]
max1<-max(y1)
n<-(lifespan[i]*numbperday)
dt <- data.table(t=0:(n-1))
dt[, y :=y1]
dt[,t_in_day := t%%numbperday]
dt[,day := (floor(t/numbperday)+1)]
dt2 <- copy(dt)
dt2[,day := day-1]
dt2[,t_in_day := t_in_day + numbperday]
dd <- rbind(dt, dt2)
dd <- dd[day>0]
dd[,day_str := sprintf("day\n%03d",day)]
dd[, day_str := as.ordered(day)]
dd[, day_str := ordered(day_str,levels=rev(levels(day_str)))]
rbPal <- colorRampPalette(c("white","blue"))
col1 <- rbPal(break1)[as.numeric(cut(dd[,y],breaks = breakvect))]
cuts<-levels(cut(dd[,y],breaks = breakvect))
cuts<-gsub(","," - ",cuts)
cuts<-gsub("\\(","[",cuts)
dd1<-cbind(dd,col1)
ggplot(dd1,aes(t_in_day,day_str, fill = col1)) +
geom_tile() +
scale_x_continuous(name="time (h)",breaks = 0:(numbperday*2)) +
scale_y_discrete(name="starting day") + # scale_fill_gradient(low = "white", high = "limegreen")
scale_fill_manual(values=rev(rbPal(break1)),labels=rev(cuts),drop=FALSE)