简短回答:功能rainbow()
发疯了当你经过时100
正如你所要求的100
不同的颜色。
你应该做什么:通过n
to rainbow()
您想要多少种颜色。如果你想从蓝色变成红色,那么你还必须用函数包装它rev()
.
library(egg)
library(ggplot2)
library(reshape2)
# Heatmap number of rows/columns
Nvalue <- 1e2
# n for colors passed to function rainbow
nColor <- c(1:10, seq(20, 100, 20))
# dummy data
df <- melt(matrix(rnorm(N^2), N))
plotList <- list()
for(i in seq_along(nColor)) {
plotList[[i]] <- ggplot(df, aes(Var1, Var2, fill = value)) +
geom_raster() +
scale_fill_gradientn(colours = rev(rainbow(nColor[i]))) +
labs(title = paste0("rainbow(", nColor[i], ")"),
x = NULL,
y = NULL,
fill = NULL) +
theme_void()
}
ggarrange(plots = plotList)
Edit:
在 OP 指定他想要的颜色之后,传递十六进制向量应该可以工作:
hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
ggplot(df, aes(Var1, Var2, fill = value)) +
geom_raster() +
scale_fill_gradientn(colours = rev(hex)) +
labs(x = NULL,
y = NULL,
fill = NULL) +
theme_void()