我想用 x 軸時間和 y 軸日期制作一個“geom tile()”圖。我想 a.) 用 縮放繪圖scale_fill_gradient(low="lightyellow", high="red"),但出現錯誤。Discrete value supplied to continuous scaleb.) 如何在 x 軸上以 1 小時為間隔設定休息時間?

示例代碼:
library(dplyr)
library(ggplot2)
library(readr)
df<-as.data.frame(df)
ggplot(df, aes(x=time, y=date) ) geom_tile(aes(fill=temperature))
theme_bw()
labs(x="Time", y ="", fill="Temperature")
theme(plot.title = element_text(hjust = 0.5))
theme(axis.text.x = element_text(hjust = 1,family="Times", face="bold", size=14),
axis.text.y = element_text(family="Times", face="bold", size=14),
strip.text = element_text(size=12, face="bold"))
樣本資料:
df<-structure(list(time = structure(c(63285, 63885, 64485, 65085,
65685, 66285, 66885, 67485, 68085, 68685, 69285, 69885, 70485,
71085, 71685, 72285, 72885, 73485, 74085, 74685, 75285, 75885,
76485, 77085, 77685, 78285, 78885, 79485, 80085, 80685, 81285,
81885, 82485, 83085, 83685, 84285, 84885, 85485, 86085, 285,
885, 1485, 2085, 2685, 3285, 3885, 4485, 5085, 5685, 6285), class = c("hms",
"difftime"), units = "secs"), temperature = c("6.31<f8>", "14.81<f8>",
"17.13<f8>", "16.56<f8>", "16.44<f8>", "16.44<f8>", "16.56<f8>",
"16.69<f8>", "16.88<f8>", "17.00<f8>", "17.13<f8>", "17.19<f8>",
"17.31<f8>", "17.50<f8>", "17.81<f8>", "18.06<f8>", "18.19<f8>",
"18.25<f8>", "18.25<f8>", "18.19<f8>", "18.06<f8>", "17.94<f8>",
"17.81<f8>", "17.75<f8>", "17.63<f8>", "17.50<f8>", "17.44<f8>",
"17.38<f8>", "17.25<f8>", "17.19<f8>", "17.13<f8>", "17.06<f8>",
"17.00<f8>", "17.00<f8>", "16.88<f8>", "16.88<f8>", "16.88<f8>",
"16.81<f8>", "16.81<f8>", "16.81<f8>", "16.81<f8>", "16.88<f8>",
"16.88<f8>", "16.88<f8>", "16.88<f8>", "16.88<f8>", "16.81<f8>",
"16.81<f8>", "16.81<f8>", "16.75<f8>"), humidity = c(0.674, 0.643,
0.472, 0.503, 0.515, 0.523, 0.53, 0.539, 0.541, 0.546, 0.548,
0.549, 0.555, 0.561, 0.578, 0.577, 0.575, 0.579, 0.596, 0.597,
0.599, 0.604, 0.612, 0.616, 0.619, 0.623, 0.637, 0.638, 0.639,
0.639, 0.643, 0.646, 0.649, 0.653, 0.654, 0.66, 0.664, 0.668,
0.672, 0.676, 0.691, 0.693, 0.693, 0.694, 0.693, 0.694, 0.698,
0.691, 0.694, 0.697), date = c("04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022",
"04/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022",
"05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022",
"05/01/2022", "05/01/2022")), row.names = c(NA, 50L), class = "data.frame")
原圖:

uj5u.com熱心網友回復:
問題是您的temperature列是包含符號的字符<f8>(也許是度數符號?)。要使 scale_fill_gradient 作業,您必須將其轉換為數字。對于您問題的第二部分,由于您的時間列是型別,hms您可以使用(為避免混亂,我在示例代碼中使用 4 小時)將breaksof設定scale_x_time為 1 小時間隔hms::hms(hours = seq(0, 24, 1)):
library(ggplot2)
df$temperature <- as.numeric(gsub("<f8>$", "", df$temperature))
ggplot(df, aes(x=time, y=date) )
geom_tile(aes(fill=temperature))
scale_x_time(breaks = hms::hms(hours = seq(0, 24, 4)))
scale_fill_gradient(low="lightyellow", high="red")
theme_bw()
labs(x="Time", y ="", fill="", title="Temperature")
theme(plot.title = element_text(hjust = 0.5))
theme(axis.text.x = element_text(hjust = 1,family="Times", face="bold", size=14),
axis.text.y = element_text(family="Times", face="bold", size=14),
strip.text = element_text(size=12, face="bold"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407002.html
標籤:
