Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot中的热图,每组的颜色不同_R_Ggplot2_Colors_Heatmap - Fatal编程技术网

R 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",

我试图在ggplot中生成一个热图。我希望每组有不同的颜色梯度,但不知道如何做到这一点。我当前的代码如下所示:

## 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
在某种程度上不鼓励这种类型的情节和美学映射,这就是为什么你感觉像是在跳圈。可以说,你的原始情节仅用一种颜色映射就更容易理解。换言之,“深青色”比“深橙色”更强烈还是更不强烈?虽然我绝对不是人机交互/可视化方面的专家,但这些微妙之处可能会误导你的情节的最终用户/消费者。