热图绘制时间与日期 ggplot
Heatmap plotting time against date ggplot
我想用 ggplot 制作热图。
结果应该是这样的(虽然 y 轴需要反转):
示例数据的子集如下。对于实际应用,数据框有 1000+ 个用户,而不是只有 3 个。渐变填充应基于用户的值。
Date <- seq(
from = as.POSIXct("2016-01-01 00:00"),
to = as.POSIXct("2016-12-31 23:00"),
by = "hour"
)
user1 <- runif(length(Date), min = 0, max = 10)
user2 <- runif(length(Date), min = 0, max = 10)
user3 <- runif(length(Date), min = 0, max = 10)
example <- data.frame(Date, user1, user2, user3)
example$hour <- format(example$Date, format = "%H:%M")
example$total <- rowSums(example[,c(2:4)])
我尝试了几种方法,将 (fill = total) 参数与 geom_tile、geom_raster 和 stat_density2d 结合使用(如此处类似帖子中的建议)。下面是一个例子:
ggplot(plotHuishoudens, aes(Date, hour, fill = Total)) +
geom_tile() +
scale_fill_gradient(low = "blue", high = "red")
虽然变量是连续的,但只显示单个点而不像连续变量那样显示y轴(scale_y_continuous也没有帮助)?
如何创建像上面提供的示例那样的热图?
我怎样才能在 y 轴上做一个很好的截止(例如每 3 小时而不是每小时)?
您的数据定义方式,您不会得到所需的输出,因为 example$Date
是一个 POSIXct 对象,即 和 一个小时的日期。
因此,您必须仅将图表映射到当天:
ggplot(data = example) +
geom_raster(aes(x=as.Date(Date, format='%d%b%y'), y=hour, fill=total)) +
scale_fill_gradient(low = "blue", high = "red")
对于你的第二个问题,你可以这样分组时间:
example <- example %>%
group_by(grp = rep(row_number(), length.out = n(), each = 4)) %>%
summarise(Date = as.Date(sample(Date, 1), format='%d%b%y'),
total = sum(total),
time_slot = paste(min(hour), max(hour), sep = "-"))
ggplot(data = example) +
geom_raster(aes(x = Date, y = time_slot, fill = total)) +
scale_fill_gradientn(colours = (cet_pal(6, name = "inferno"))) # I like gradients from "cetcolor" package
我想用 ggplot 制作热图。 结果应该是这样的(虽然 y 轴需要反转):
示例数据的子集如下。对于实际应用,数据框有 1000+ 个用户,而不是只有 3 个。渐变填充应基于用户的值。
Date <- seq(
from = as.POSIXct("2016-01-01 00:00"),
to = as.POSIXct("2016-12-31 23:00"),
by = "hour"
)
user1 <- runif(length(Date), min = 0, max = 10)
user2 <- runif(length(Date), min = 0, max = 10)
user3 <- runif(length(Date), min = 0, max = 10)
example <- data.frame(Date, user1, user2, user3)
example$hour <- format(example$Date, format = "%H:%M")
example$total <- rowSums(example[,c(2:4)])
我尝试了几种方法,将 (fill = total) 参数与 geom_tile、geom_raster 和 stat_density2d 结合使用(如此处类似帖子中的建议)。下面是一个例子:
ggplot(plotHuishoudens, aes(Date, hour, fill = Total)) +
geom_tile() +
scale_fill_gradient(low = "blue", high = "red")
虽然变量是连续的,但只显示单个点而不像连续变量那样显示y轴(scale_y_continuous也没有帮助)?
如何创建像上面提供的示例那样的热图? 我怎样才能在 y 轴上做一个很好的截止(例如每 3 小时而不是每小时)?
您的数据定义方式,您不会得到所需的输出,因为 example$Date
是一个 POSIXct 对象,即 和 一个小时的日期。
因此,您必须仅将图表映射到当天:
ggplot(data = example) +
geom_raster(aes(x=as.Date(Date, format='%d%b%y'), y=hour, fill=total)) +
scale_fill_gradient(low = "blue", high = "red")
对于你的第二个问题,你可以这样分组时间:
example <- example %>%
group_by(grp = rep(row_number(), length.out = n(), each = 4)) %>%
summarise(Date = as.Date(sample(Date, 1), format='%d%b%y'),
total = sum(total),
time_slot = paste(min(hour), max(hour), sep = "-"))
ggplot(data = example) +
geom_raster(aes(x = Date, y = time_slot, fill = total)) +
scale_fill_gradientn(colours = (cet_pal(6, name = "inferno"))) # I like gradients from "cetcolor" package