我正在尝试创建一个热图,显示 2 年内每月的数据,X 轴上的月份和 y 轴上的地区,其中数字为“填充”。
我的数据是
Month District Number
Jan-17 Lahore 10
Feb-17 Lahore 15
Mar-17 Lahore 2
Apr-17 Lahore 7
May-17 Lahore 8
Jun-17 Lahore 9
Jul-17 Lahore 20
Aug-17 Lahore 13
Sep-17 Lahore 22
Oct-17 Lahore 14
Nov-17 Lahore 5
Dec-17 Lahore 5
Jan-18 Lahore 19
Feb-18 Lahore 21
Mar-18 Lahore 2
Apr-18 Lahore 17
May-18 Lahore 18
Jun-18 Lahore 12
Jul-18 Lahore 9
Aug-18 Lahore 1
Sep-18 Lahore 1
Oct-18 Lahore 1
数据仅显示一个区/市。在我的数据中,我有 6 个区。我使用的命令是:
ggplot(HEAT_MAP2, aes(x = Month, y = District, fill = Number)) +
geom_tile() +
scale_x_date(date_labels = "%b%Y") +
scale_fill_gradient(low = "white", high = "darkgreen", name = "Your Legend")
但它给出了错误
错误:提供给连续刻度的离散值
如何消除此错误以实现我正在寻找的图形。
您的帮助将不胜感激
这是一个常见问题:问题是 Month + Year 不是日期。日期需要日、月和年。
最简单的解决方案是添加每个月的第一天,以便Month
可以转换为日期。像这样的东西:
library(dplyr)
library(ggplot2)
HEAT_MAP2 %>%
mutate(Date = as.Date(paste0("01-", Month), "%d-%b-%y")) %>%
ggplot(aes(x = Date, y = District, fill = Number)) +
geom_tile() +
scale_x_date(date_labels = "%b%Y") +
scale_fill_gradient(low = "white", high = "darkgreen", name = "Your Legend")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句