ggplot 热图的反转颜色 brewer 颜色
Reverse color brewer colors of a ggplot heatmap
library(tidyverse)
library(RColorBrewer)
x <- LETTERS[1:20]
y <- paste0("var", seq(1, 20))
data <- expand.grid(X = x, Y = y)
data$Z <- runif(400, 0, 5)
如果我构建上面的数据框,然后用下面的代码绘制热图,我得到这个和那个:
ggplot(data, aes(X, Y, fill = Z)) +
geom_tile() +
scale_fill_distiller(palette = "Reds") + # line 3
# scale_fill_distiller(palette = "Reds", direction = -1) + # line 4
labs(x = NULL, y = NULL) +
theme_minimal()
根据 ?scale_fill_distiller()
,我认为 运行 line #4
代替 line #3
(上图)应该会反转我的图例的颜色:
direction
argument: Sets the order of colours in the scale. If 1, the default, colours are as output by RColorBrewer::brewer.pal(). If -1, the order of colours is reversed.
本质上,热图的白色区域会变成暗红色,而热图的暗红色区域会变成白色。为什么这没有发生? 运行 line #3
或 line #4
并且输出相同。
根据我上面的评论,让我们将两个图并排放置
gg1 <- ggplot(data, aes(X, Y, fill = Z)) +
geom_tile() +
scale_fill_distiller(palette = "Reds", direction = +1) +
labs(x = NULL, y = NULL) +
theme_minimal()
gg2 <- ggplot(data, aes(X, Y, fill = Z)) +
geom_tile() +
scale_fill_distiller(palette = "Reds", direction = -1) +
labs(x = NULL, y = NULL) +
theme_minimal()
library(gridExtra)
g <- grid.arrange(gg1, gg2)
您在上面看到的行为的原因是 scale_fill_distiller
默认假设 direction = -1
:
scale_fill_distiller
#function (..., type = "seq", palette = 1, direction = -1, values = NULL,
# space = "Lab", na.value = "grey50", guide = "colourbar",
# aesthetics = "fill")
#{
# type <- match.arg(type, c("seq", "div", "qual"))
# if (type == "qual") {
# warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead",
# call. = FALSE)
# }
# continuous_scale(aesthetics, "distiller", gradient_n_pal(brewer_pal(type,
# palette, direction)(7), values, space), na.value = na.value,
# guide = guide, ...)
#}
#<bytecode: 0x7fed742ed1d8>
#<environment: namespace:ggplot2>
library(tidyverse)
library(RColorBrewer)
x <- LETTERS[1:20]
y <- paste0("var", seq(1, 20))
data <- expand.grid(X = x, Y = y)
data$Z <- runif(400, 0, 5)
如果我构建上面的数据框,然后用下面的代码绘制热图,我得到这个和那个:
ggplot(data, aes(X, Y, fill = Z)) +
geom_tile() +
scale_fill_distiller(palette = "Reds") + # line 3
# scale_fill_distiller(palette = "Reds", direction = -1) + # line 4
labs(x = NULL, y = NULL) +
theme_minimal()
根据 ?scale_fill_distiller()
,我认为 运行 line #4
代替 line #3
(上图)应该会反转我的图例的颜色:
direction
argument: Sets the order of colours in the scale. If 1, the default, colours are as output by RColorBrewer::brewer.pal(). If -1, the order of colours is reversed.
本质上,热图的白色区域会变成暗红色,而热图的暗红色区域会变成白色。为什么这没有发生? 运行 line #3
或 line #4
并且输出相同。
根据我上面的评论,让我们将两个图并排放置
gg1 <- ggplot(data, aes(X, Y, fill = Z)) +
geom_tile() +
scale_fill_distiller(palette = "Reds", direction = +1) +
labs(x = NULL, y = NULL) +
theme_minimal()
gg2 <- ggplot(data, aes(X, Y, fill = Z)) +
geom_tile() +
scale_fill_distiller(palette = "Reds", direction = -1) +
labs(x = NULL, y = NULL) +
theme_minimal()
library(gridExtra)
g <- grid.arrange(gg1, gg2)
您在上面看到的行为的原因是 scale_fill_distiller
默认假设 direction = -1
:
scale_fill_distiller
#function (..., type = "seq", palette = 1, direction = -1, values = NULL,
# space = "Lab", na.value = "grey50", guide = "colourbar",
# aesthetics = "fill")
#{
# type <- match.arg(type, c("seq", "div", "qual"))
# if (type == "qual") {
# warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead",
# call. = FALSE)
# }
# continuous_scale(aesthetics, "distiller", gradient_n_pal(brewer_pal(type,
# palette, direction)(7), values, space), na.value = na.value,
# guide = guide, ...)
#}
#<bytecode: 0x7fed742ed1d8>
#<environment: namespace:ggplot2>