在csv文件中,月份加年份R需要哪种格式才能在ggplot2的x轴上显示

在csv文件中,月份加年份R需要哪种格式才能在ggplot2的x轴上显示,r,ggplot2,R,Ggplot2,我试图创建一个热图,显示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

我试图创建一个热图,显示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")
但这是一个错误

错误:离散值提供给连续刻度

如何消除此错误以实现我正在寻找的图形。
非常感谢您的帮助

这是一个常见问题:问题是月+年不是日期。日期需要日、月和年

最简单的解决方案是添加每个月的第一天,以便将月份转换为日期。大概是这样的:

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")

不客气;如果答案解决了您的问题,请投赞成票并接受它。@neilfws…我尝试了您的上述命令,现在它给出了错误:错误:离散值提供给连续刻度。另外:警告消息:1:In-minx:no non-missing arguments to-min;返回Inf 2:In-maxx:max没有未丢失的参数;返回-Inf 3:In-mindiffsortx:min没有未丢失的参数;返回INF如何删除此错误。我做错了什么?在excel中,我选择了日期格式date month year,例如,2001年3月14日代码与您问题中的示例数据一起工作。我不知道为什么现在会涉及Excel,我不想这样: