[英]which format for month plus year R requires in csv file to show it on x-axis in ggplot2
I am trying to create a heat map showing data per month for 2 years, with Months on X axis and districts on y axis where as number as "fill". 我正在尝试创建一个热图,以显示2年每月的数据,X轴为月,y轴为区,数字为“填充”。
My data is 我的数据是
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
The data is just showing one district/City. 数据仅显示一个地区/城市。 In my data i have 6 districts.The command i used is: 在我的数据中,我有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")
But it's giving error 但这给了错误
Error: Discrete value supplied to continuous scale 错误:离散值提供给连续刻度
How can I remove this error to achieve the graph i am looking for. 如何删除此错误以实现所需的图形。
Your help will be highly appreciated 非常感谢您的帮助
This is a common problem: the issue is that Month + Year is not a date. 这是一个常见问题:问题是“月份+年”不是日期。 A date requires day, month and year. 日期需要日,月和年。
The simplest solution is to add the first day of each month, so as Month
can be converted to a date. 最简单的解决方案是添加每个月的第一天,以便将Month
转换为日期。 Something like this: 像这样:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.