R ggplot中的热图,每组的颜色不同
我试图在ggplot中生成一个热图。我希望每组有不同的颜色梯度,但不知道如何做到这一点。我当前的代码如下所示:R ggplot中的热图,每组的颜色不同,r,ggplot2,colors,heatmap,R,Ggplot2,Colors,Heatmap,我试图在ggplot中生成一个热图。我希望每组有不同的颜色梯度,但不知道如何做到这一点。我当前的代码如下所示: ## dummy data ----------------------------------------------------------------------------- data <- data.frame( group = sample(c("Direct Patient Care", "Indirect Patient Care", "Education",
## dummy data -----------------------------------------------------------------------------
data <- data.frame(
group = sample(c("Direct Patient Care", "Indirect Patient Care", "Education", "Rounds", "Handoff", "Misce"), 30, replace = T),
pct = rnorm(30, mean = 50, sd = 8)
)
## generate group id
data <- data %>%
group_by(group) %>%
mutate(id = row_number())
data$grpid <- with(data, ifelse(group == "Direct Patient Care", 1, ifelse(group == "Indirect Patient Care", 2,
ifelse(group == "Education", 3,
ifelse(group == "Rounds", 4,
ifelse(group == "Handoff", 5,6 ))))))
## draw graph ------------------------------------------------------------------------------
library(ggplot2)
p <- ggplot(data, aes(x=id, y=group, fill = pct)) +
theme(panel.background = element_rect(fill = "white", colour = "grey50"), aspect.ratio = 0.4) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)+
# guides(fill = guide_legend("Time, %")) +
geom_tile() +
scale_x_continuous (name = " ", breaks = seq(1, 8, by = 1)) +
scale_y_discrete(name = " ") +
theme(axis.text.x = element_text(angle = 0,hjust = 1,vjust = 1), plot.title = element_text(hjust = 0.5) ) +
ggtitle("Heatmap of time spent doing activities across 194 shifts")
p + scale_fill_gradient2(low = "white", high = "red", limits = c(0, 80), breaks = c(0, 10, 20, 30, 40, 50, 60, 70), guide = guide_legend("Time, %")) ## change the color theme ##
##虚拟数据-----------------------------------------------------------------------------
数据%
变异(id=行号()
data$grpid您可以通过在数据中创建自己的重缩放值,然后稍微“黑客”一下alpha
美学,再结合填充
美学:
library(tidyverse)
data %>%
group_by(group) %>%
mutate(rescale = scales::rescale(pct)) %>%
ggplot(., aes(x = factor(id), y = group)) +
geom_tile(aes(alpha = rescale, fill = group), color = "white") +
scale_alpha(range = c(0.1, 1))
首先,我们创建一个名为rescale
的新列,它将pct
从0
重新缩放到1
,然后强制执行scale\u alpha(range=c(0,1))
[注意,在本例中,我使用了c(0.1,1)
,以便您仍然可以“看到”零点
最后,您可能要删除辅助线:
data %>%
group_by(group) %>%
mutate(rescale = scales::rescale(pct)) %>%
ggplot(., aes(x = factor(id), y = group)) +
geom_tile(aes(alpha = rescale, fill = group), color = "white") +
scale_alpha(range = c(0.1, 1)) +
theme(legend.position = "none")
注意:通过使用aes(x=因子(id)…
您可以手动设置x轴
,因为在这种情况下,您似乎希望将其视为因子而不是数字刻度
最后,如果您真的想变得有趣,您可以将axis.text.y
颜色双重编码为factor
(即数据$group
)变量的级别:
data %>%
group_by(group) %>%
mutate(rescale = scales::rescale(pct)) %>%
ggplot(., aes(x = factor(id), y = group)) +
geom_tile(aes(alpha = rescale, fill = group), color = "white") +
scale_alpha(range = c(0.1, 1)) +
theme(legend.position = "none",
axis.text.y = element_text(color = scales::hue_pal()(length(levels(data$group)))),
axis.ticks = element_blank()) +
labs(x = "", y = "")
谢谢,杰森,这非常有帮助!!有没有办法让每组的颜色栏保持与我在图中显示的相同的截止值?@mandy您可以通过将mutate(…)
行更改为mutate(重新缩放=地板(pct/10)*10)来接近
--我很确定,ggplot2
在某种程度上不鼓励这种类型的情节和美学映射,这就是为什么你感觉像是在跳圈。可以说,你的原始情节仅用一种颜色映射就更容易理解。换言之,“深青色”比“深橙色”更强烈还是更不强烈?虽然我绝对不是人机交互/可视化方面的专家,但这些微妙之处可能会误导你的情节的最终用户/消费者。