ggplot:为什么将geom_contour添加到日历热图中这么难?

时间:2018-02-10 20:21:22

标签: r ggplot2

我正在尝试使用ggplot制作日历热图。我找到了thisthat,但我不想引入那么多依赖项,因为我的最终闪亮服务器是有限的。这是我的预期结果:

enter image description here

我几乎完成了使用几行tidyverse代码,但是我在最后一点上挣扎,即突出显示日历热图中的月份。这是如何使用我的数据重现我的问题:

library(tidyverse)
library(lubridate)

# Load data
logdata <- read_csv("https://georoen.github.io/heuteZensiert/Logfile.csv") %>% 
  select(date:prozent) %>% 
  transmute(date = as.Date(date),
        sender = factor(ifelse(grepl("h", sendung), "ZDF", "ARD")),
        sendung = factor(sendung, labels = c("Heute 19Uhr", "Heute Journal",
                                             "Tageschau", "Tagesthemen")),
        prozent = as.numeric(gsub("%", "", prozent))) %>% 
  mutate(Day = lubridate::wday(date, label = T, abbr = F, week_start = 1),
     Day = forcats::fct_rev(Day),
     Week = lubridate::week(date),
     Month = lubridate::month(date),
     Year = lubridate::year(date)) %>% 
  arrange(desc(date))

# Plot
ggplot(logdata, aes(Week, Day)) +
  geom_tile(aes(fill = prozent)) +
  geom_contour(aes(z = Month), binwidth = 1) +
  scale_fill_continuous(low = "lightgreen", high = "red") +
  facet_grid(sendung ~ Year, scales = "free_x") +
  labs(x = "Kalenderwoche", y = "") 

这给了我一些由geom_contour(aes(z = Month), binwidth = 1)引起的错误,结果如下:

enter image description here

所以现在我觉得为什么geom_contour不能用于概述几个月?如果我放z = Month,它是否与此ggplot示例中的相同*? (*通过添加geom_tile)进行略微修改

?geom_contour()
ggplot(faithfuld, aes(waiting, eruptions, z = density)) + geom_tile(aes(fill = density)) + geom_contour()

enter image description here

任何人都可以解释stat_contour正在做什么或为什么不能让z = Month

非常感谢你!

0 个答案:

没有答案