[英]which format for month plus year R requires in csv file to show it on x-axis in 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 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
轉換為日期。 像這樣:
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.