当改变我的数据集,使ExpDayHour列是正确的格式(posixct)R告诉我,时间_trans工作与类POSIXct的对象,只有当试图作出ggplot的n在函数的RefillRate,ExpDayHour,FlowerNo和偏移。
我希望生成一个ggplot,其中日期和时间在x轴上,refillrate是曲线的颜色,n在y轴上。这是我的脚本:
library(ggplot2)
library(dplyr)
library(ggthemes)
library(scales)
preds = read.csv("preds_2.csv")
# Convert ExpDayHour to POSIXct format with timezone set to "GMT"
preds$ExpDayHour <- as.POSIXct(preds$ExpDayHour, format = "%Y-%m-%d %H:%M:%S", tz = "GMT")
# Create a scatter plot with facets and color gradient
ggplot(data = preds, aes(x = ExpDayHour, y = n, color = RefillRate)) +
geom_point(size = 3) +
scale_x_datetime(date_labels = "%d %H:%M", breaks = date_breaks("2 hours"), timezone = "GMT") +
facet_grid(. ~ FlowerNo + Offset) +
scale_color_gradient(low = "blue", high = "red") +
labs(x = "Date and Time", y = "Number of visits", color = "RefillRate") +
ggtitle("Relationship between the number of flower visits and the time, refill rate and flower number") +
theme_economist_white() +
theme(plot.title = element_text(hjust = 0.5, size = 18),
axis.text = element_text(size = 14),
axis.title = element_text(size = 16),
legend.position = "bottom",
legend.text = element_text(size = 14),
legend.title = element_text(size = 16)) +
annotate("text", x = 0, y = 0, label = "Source: My Data")
这是我的数据集:
https://drive.google.com/file/d/19rprT4TB1TiFkhXIUzzi-WhBKb-z1fVg/view?usp=sharing
和数据样本:
structure(list(FlowerNo = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), Row = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Column = c(1, 1,
1, 1, 1, 1, 1, 1, 1, 1), Colony = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), ColonySize = c(77L, 77L, 77L, 77L, 77L, 77L, 77L,
77L, 77L, 77L), RefillRate = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L), ExpDayHour = structure(c(-2208989361, -2208985761, -2208982161,
-2208978561, -2208974961, -2208971361, -2208967761, -2208964161,
-2208960561, -2208956961), tzone = "", class = c("POSIXct", "POSIXt"
)), n = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 3L, 3L), Offset = c(2.19722457733622,
2.19722457733622, 2.19722457733622, 2.19722457733622, 2.19722457733622,
2.19722457733622, 2.19722457733622, 2.19722457733622, 2.19722457733622,
2.19722457733622), ExpDay = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), ExpHour = 0:9, obs = 1:10, model = c("model3", "model3",
"model3", "model3", "model3", "model3", "model3", "model3", "model3",
"model3"), fit = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lower.CI = c(0,
0, 0, 0, 0, 0, 0, -1.01202890791274, -3.03611841847294, -3.03615011445332
), upper.CI = c(0, 0, 0, 0, 0, 0, 0, 1.01202890791274, 3.03611841847294,
3.03615011445332), lower.PI = c(`1` = 0, `2` = 0, `3` = 0, `4` = 0,
`5` = 0, `6` = 0, `7` = 0, `8` = 0, `9` = 0, `10` = 0), upper.PI = c(`1` = 0,
`2` = 0, `3` = 0, `4` = 0, `5` = 0, `6` = 0, `7` = 0, `8` = 81.2233791197293,
`9` = 244.247330806024, `10` = 244.824524252859)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
n = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 3L, 3L), ExpDayHour = structure(c(-2208989361,
-2208985761, -2208982161, -2208978561, -2208974961, -2208971361,
-2208967761, -2208964161, -2208960561, -2208956961), tzone = "", class = c("POSIXct",
"POSIXt")), Offset = c(2.19722457733622, 2.19722457733622,
2.19722457733622, 2.19722457733622, 2.19722457733622, 2.19722457733622,
2.19722457733622, 2.19722457733622, 2.19722457733622, 2.19722457733622
), obs = 1:10, FlowerNo = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), RefillRate = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))
1条答案
按热度按时间l0oc07j21#
正如@MrFlick所指出的,您提供的日期时间格式需要与您的数据相匹配。从示例数据中,我猜测它是月/日/年,因此请注意下面代码中的
%m/%d/%Y
。如果您尝试给予x = 0
作为位置,则注解将抛出错误,因为您的x轴现在是时间序列。下面应用的一种替代方法是添加标题文本,labs(caption = 'text')
并在theme
中左对齐。创建于2023-04-03带有reprex v2.0.2