简体   繁体   English

加热 map 与 R 中的附加值

[英]Heat map with additional values in R

I created a heat map for sample data.我为示例数据创建了一个热量 map。 I would like to add some information about the average for each hour and the sum for each day of the week.我想添加一些关于每小时平均值和一周中每一天总和的信息。 It is best to have an additional column on the right and an additional row at the top of the chart, without filling.最好在图表的右侧增加一列,在顶部增加一行,无需填充。

在此处输入图像描述

My code looks like this:我的代码如下所示:

TD=data.frame(wday=rep(c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
                         "Saturday"),24), hour=rep(0:23, each=7), N=sample(100:300, 168))

ggplot(TD, aes(hour, wday,  fill=N)) + 
  geom_tile(colour = "white", na.rm = TRUE) +
  theme_bw() + theme_minimal() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_fill_viridis() +
  coord_fixed(xlim = c(0, 23)) +
  geom_text(aes(label=paste(N)), size=4) + 
  scale_x_continuous(breaks=seq(0, 23, 1)) 

The most obvious solution for me is to include these means in your dataframe and then plot your heatmap afterwards.对我来说最明显的解决方案是将这些方法包含在您的 dataframe 中,然后在 plot 中包含您的热图。

library("ggplot2")
library("dplyr")
library("tidyr")
library("viridis")

TD=data.frame(wday=rep(c("Sunday", "Monday", "Tuesday", 
              "Wednesday", "Thursday", "Friday", "Saturday"),24),
              hour=rep(0:23, each=7), 
              N=sample(100:300, 168))

df <- TD %>% group_by(wday) %>% summarise(N=round(mean(N)), hour="avg") %>% rbind(TD)
df <- TD %>% group_by(hour) %>% summarise(N=round(mean(N)), wday="avg") %>% rbind(df)

df$wday <- factor(df$wday, levels=c("Monday", "Tuesday", "Wednesday", "Thursday",
                  "Friday", "Saturday", "Sunday", "avg"))
df$hour <- factor(df$hour, levels=c(as.character(0:23), "avg"))

ggplot(df, aes(hour, wday, fill=N)) + 
  geom_tile(colour="white", na.rm=TRUE) +
  theme_bw() + 
  theme_minimal() + 
  theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank()) +
  scale_fill_viridis() +
  coord_fixed(xlim = c(0, 23)) +
  geom_text(aes(label=paste(N)), size=4) + 
  coord_fixed(xlim=c(0, 25), ratio=1)

在此处输入图像描述

EDIT: without filling in for the new elements.编辑:不填写新元素。

df <- TD %>% group_by(hour) %>% summarise(N=round(mean(N)), wday="avg") %>% rbind(TD)
df <- df %>% group_by(wday) %>% summarise(N=round(sum(N)), hour="sum") %>% rbind(df)

df$wday <- factor(df$wday, levels=c("Monday", "Tuesday", "Wednesday", "Thursday",
                                    "Friday", "Saturday", "Sunday", "avg"))
df$hour <- factor(df$hour, levels=c(as.character(0:23), "sum"))

ggplot() + 
  geom_tile(colour="white", data=subset(df, hour!="sum" & wday!="avg"), 
            aes(hour, wday, fill=N)) +
  geom_text(aes(hour, wday, label=N), data=df, inherit.aes=FALSE) +
  scale_x_discrete(limits=levels(df$hour)) +
  scale_y_discrete(limits=levels(df$wday)) +
  theme_bw() +  
  theme_minimal() + 
  theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
        axis.title=element_blank()) +
  scale_fill_viridis() +
  coord_fixed(xlim=c(0, 25), ratio=1)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM