我想缩放我的组以显示小时、分钟和秒。我不知道如何设置scale_x_time函数。另外,我想包含分隔8am和6pm的行。可能这个时间没有记录在我的数据集中。您建议怎么做?
R代码:
library(dplyr)
library(ggplot2)
library(ggalluvial)
library(readr)
library(lubridate)
library(viridis)
library(gganimate)
df <- read_csv("Date_Time_outdoor_temp_25_01_2023.csv")
df <- na.omit(df)
df$Time <- as.POSIXct(df$Time)
#df$temperature <- as.numeric(gsub("<f8>$", "", df$temperature)) ####remove celsius sign
df$WeekDay <- factor(df$WeekDay, levels = c("Sunday","Saturday","Friday","Thursday","Wednesday","Tuesday","Monday"))
p <- ggplot(df, aes(x=Time, y=WeekDay) ) +
geom_tile(aes(fill=Temperature)) +
geom_tile(aes(fill = Temperature)) +
#scale_x_time(breaks = hms::hms(hours = seq(0, 24, 2))) + ###here I need help
scale_y_discrete(expand=c(0, 0)) +
scale_fill_viridis()+
# geom_vline(xintercept = lubridate::hms(c("08:00:00", "18:00:00")),col = "blue", lwd = 2, lty=2) + ###as well here
theme_bw() +
labs(x="", y ="", fill="Temperature", title="") +
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")) +
theme(legend.title = element_text(family="Times", color = "black", size = 16,face="bold"),
legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
legend.position="right",
legend.box = "horizontal",
plot.title = element_text(hjust = 0.5)) +
theme(
legend.key.width = unit(1, "cm"),
legend.key.height = unit(4, "cm")
)
p
样本数据:
df<-structure(list(Time = c("01:28:53", "01:33:53", "01:38:53", "01:43:53",
"01:48:53", "01:53:53", "01:58:53", "02:03:53", "02:08:53", "02:13:53",
"02:18:53", "02:23:53", "02:28:53", "02:33:53", "02:38:53", "02:43:53",
"02:48:53", "02:53:53", "02:58:53", "03:03:53", "03:08:53", "03:13:53",
"03:18:53", "03:23:53", "03:28:53", "03:33:53", "03:38:53", "03:43:53",
"03:48:53", "03:53:53", "03:58:53", "04:03:53", "04:08:53", "04:13:53",
"04:18:53", "04:23:53", "04:28:53", "04:33:53", "04:38:53", "04:43:53",
"04:48:53", "04:53:53", "04:58:53", "05:03:53", "05:08:53", "05:13:53",
"05:18:53", "05:23:53", "05:28:53", "05:33:53", "05:38:53", "05:43:53",
"05:48:53", "05:53:53", "05:58:53", "06:03:53", "06:08:53", "06:13:53",
"06:18:53", "06:23:53", "06:28:53", "06:33:53", "06:38:53", "06:43:53",
"06:48:53", "06:53:53", "06:58:53", "07:03:53", "07:08:53", "07:13:53",
"07:18:53", "07:23:53", "07:28:53", "07:33:53", "07:38:53", "07:43:53",
"07:48:53", "07:53:53", "07:58:53", "08:03:53", "08:08:53", "08:13:53",
"08:18:53", "08:23:53", "08:28:53", "08:33:53", "08:38:53", "08:43:53",
"08:48:53", "08:53:53", "08:58:53", "09:03:53", "09:08:53", "09:13:53",
"09:18:53", "09:23:53", "09:28:53", "09:33:53", "09:38:53", "09:43:53"
), Temperature = c(22.973, 14.699, 14.029, 14.005, 14.053, 14.388,
14.364, 14.316, 13.837, 13.885, 14.364, 13.741, 14.196, 14.412,
14.532, 14.436, 14.364, 14.005, 14.101, 14.196, 14.101, 14.172,
14.268, 14.819, 14.22, 14.723, 15.034, 14.556, 15.202, 14.484,
14.148, 14.34, 14.771, 15.249, 14.963, 14.292, 13.933, 14.292,
14.484, 14.412, 14.532, 14.556, 14.508, 14.436, 14.556, 14.532,
14.412, 14.34, 14.196, 14.22, 14.196, 14.077, 14.101, 13.981,
13.885, 13.837, 13.837, 13.5, 13.356, 13.356, 13.091, 12.609,
12.754, 12.995, 12.56, 11.543, 11.276, 10.959, 10.886, 11.008,
10.788, 10.739, 10.593, 10.446, 10.495, 10.25, 10.176, 10.274,
10.078, 10.054, 9.857, 9.833, 9.759, 9.66, 9.488, 9.512, 9.685,
9.71, 9.759, 9.833, 9.734, 9.636, 9.537, 9.34, 9.216, 9.167,
8.994, 8.721, 8.646, 8.721), RH = c(54.969, 64.515, 68.844, 69.702,
70.629, 69.314, 70.229, 70.077, 70.158, 70.482, 70.43, 70.569,
70.713, 69.951, 69.801, 69.237, 69.338, 69.444, 70.208, 70.713,
70.752, 71.338, 70.24, 69.913, 70.03, 69.665, 68.018, 67.147,
67.118, 66.756, 67.942, 67.717, 67.65, 66.602, 66.375, 68.633,
67.903, 68.287, 65.21, 65.811, 64.486, 65.456, 65.944, 66.515,
64.167, 62.779, 63.083, 63.365, 64.105, 65.076, 63.164, 63.702,
63.383, 64.331, 64.929, 65.767, 66.32, 67.766, 68.258, 68.489,
69.301, 71.178, 70.637, 69.197, 70.458, 74.265, 75.658, 76.838,
76.988, 76.903, 77.655, 78.304, 78.819, 79.442, 79.316, 79.997,
80.307, 80.437, 80.421, 80.469, 80.615, 80.718, 80.593, 80.733,
80.587, 79.998, 80.09, 79.824, 79.672, 79.58, 79.069, 78.912,
78.237, 78.085, 75.894, 75.112, 74.441, 72.61, 70.807, 69.95),
WeekDay = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("Sunday", "Saturday", "Friday", "Thursday",
"Wednesday", "Tuesday", "Monday"), class = "factor")), row.names = c(NA,
100L), class = "data.frame")
问题结局:
1条答案
按热度按时间yacmzcpb1#
不要忽略错误,您对
as.POSIXct
的调用失败了。在轴中使用hms::hms
是一个好的开始,尽管您还需要将相同的逻辑应用到Time
列。从df
的structure
版本开始:从这里开始,您的绘图(注解掉的部分未注解)工作正常。