ggplot Time Series Figure Error: Invalid input: time_trans works with objects of class POSIXct only BUT data is in POSIXct
ggplot Time Series Figure Error: Invalid input: time_trans works with objects of class POSIXct only BUT data is in POSIXct
我的数据集是一个包含过去 4 个夏天每日最高水温的数据框。
X site DateTime value Month Year Day
<int> <fct> <dttm> <dbl> <int> <fct> <chr>
1 6775 RAYNER_UP 2018-07-09 19:00:00 19.8 7 2018 07/10
2 6776 RAYNER_UP 2018-07-10 19:00:00 21.2 7 2018 07/11
3 6777 RAYNER_UP 2018-07-11 19:00:00 20.4 7 2018 07/12
4 6778 RAYNER_UP 2018-07-12 19:00:00 20.1 7 2018 07/13
5 6779 RAYNER_UP 2018-07-13 19:00:00 17.3 7 2018 07/14
6 6780 RAYNER_UP 2018-07-14 19:00:00 19.5 7 2018 07/15
7 6781 RAYNER_UP 2018-07-15 19:00:00 21.2 7 2018 07/16
8 6782 RAYNER_UP 2018-07-16 19:00:00 21.0 7 2018 07/17
9 6783 RAYNER_UP 2018-07-17 19:00:00 19.8 7 2018 07/18
10 6784 RAYNER_UP 2018-07-18 19:00:00 16.8 7 2018 07/19
我的目标是制作一个折线图,每年都有不同的线。到目前为止,在互联网的大量帮助下,我每年都做了一个线,但是规模非常拥挤example。
我想重新调整我的时间序列中的 x 轴以显示一个月中的几天。
到目前为止在 ggplot 中使用 scale_x_datetime 和 scale_x_date 但没有成功。
我不断收到错误消息:无效输入:time_trans 仅适用于 class POSIXct 的对象,即使日期列在 POSIXct 中也是如此。
给出问题的代码示例:
test1 = ggplot() +
geom_line(data = Rayner_up_summer, aes(x=strftime(DateTime,format="%m/%d"),
y=value,
group = Year,
color=strftime(DateTime,format="%Y")))+
# size=.1))+
scale_color_discrete(name="Year")+
labs(x="date")
有谁知道为什么我无法使用 scale_x_datetime 即使我的 x 轴 (DateTime) 是 POSIXct 日期时间格式?如果您有任何建议,我将不胜感激
正如我在评论中所说,问题是您通过 strftime(DateTime,format="%m/%d")
将 DateTime
列转换为一个字符,这就是为什么您的秤非常拥挤的原因,也是为什么scale_x_date
或 _datetime
将不起作用。
相反,实现您想要的结果的一种选择是
- 在您使用同一年份的数据中添加辅助日期列。
- 将此帮助日期列映射到
x
注意:我稍微修改了你的数据集以说明第二年。
Rayner_up_summer$DateTime <- as.Date(paste0("2018/", Rayner_up_summer$Day))
library(ggplot2)
ggplot() +
geom_line(data = Rayner_up_summer, aes(
x = DateTime,
y = value,
group = Year,
color = factor(Year)
)) +
scale_x_date(date_labels = "%m/%d") +
scale_color_discrete(name = "Year") +
labs(x = "date")
数据
Rayner_up_summer <- structure(list(X = c(
6775L, 6776L, 6777L, 6778L, 6779L, 6780L,
6781L, 6782L, 6783L, 6784L, 6775L, 6776L, 6777L, 6778L, 6779L,
6780L, 6781L, 6782L, 6783L, 6784L
), site = c(
"RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP"
), DateTime = structure(c(
1531173600,
1531260000, 1531346400, 1531432800, 1531519200, 1531605600, 1531692000,
1531778400, 1531864800, 1531951200, 1531173600, 1531260000, 1531346400,
1531432800, 1531519200, 1531605600, 1531692000, 1531778400, 1531864800,
1531951200
), class = c("POSIXct", "POSIXt"), tzone = ""), value = c(
19.8,
21.2, 20.4, 20.1, 17.3, 19.5, 21.2, 21, 19.8, 16.8, 21.5740302174818,
21.6853770664893, 18.4306976739317, 21.1522381303366, 20.208727594465,
19.5954797456507, 20.6829415732063, 17.6733329861891, 20.2849614520092,
20.5253239201847
), Month = c(
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), Year = c(
2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019
), Day = c(
"07/10",
"07/11", "07/12", "07/13", "07/14", "07/15", "07/16", "07/17",
"07/18", "07/19", "07/10", "07/11", "07/12", "07/13", "07/14",
"07/15", "07/16", "07/17", "07/18", "07/19"
)), row.names = c(
"1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "21", "31",
"41", "51", "61", "71", "81", "91", "101"
), class = "data.frame")
我的数据集是一个包含过去 4 个夏天每日最高水温的数据框。
X site DateTime value Month Year Day
<int> <fct> <dttm> <dbl> <int> <fct> <chr>
1 6775 RAYNER_UP 2018-07-09 19:00:00 19.8 7 2018 07/10
2 6776 RAYNER_UP 2018-07-10 19:00:00 21.2 7 2018 07/11
3 6777 RAYNER_UP 2018-07-11 19:00:00 20.4 7 2018 07/12
4 6778 RAYNER_UP 2018-07-12 19:00:00 20.1 7 2018 07/13
5 6779 RAYNER_UP 2018-07-13 19:00:00 17.3 7 2018 07/14
6 6780 RAYNER_UP 2018-07-14 19:00:00 19.5 7 2018 07/15
7 6781 RAYNER_UP 2018-07-15 19:00:00 21.2 7 2018 07/16
8 6782 RAYNER_UP 2018-07-16 19:00:00 21.0 7 2018 07/17
9 6783 RAYNER_UP 2018-07-17 19:00:00 19.8 7 2018 07/18
10 6784 RAYNER_UP 2018-07-18 19:00:00 16.8 7 2018 07/19
我的目标是制作一个折线图,每年都有不同的线。到目前为止,在互联网的大量帮助下,我每年都做了一个线,但是规模非常拥挤example。
我想重新调整我的时间序列中的 x 轴以显示一个月中的几天。
到目前为止在 ggplot 中使用 scale_x_datetime 和 scale_x_date 但没有成功。 我不断收到错误消息:无效输入:time_trans 仅适用于 class POSIXct 的对象,即使日期列在 POSIXct 中也是如此。
给出问题的代码示例:
test1 = ggplot() +
geom_line(data = Rayner_up_summer, aes(x=strftime(DateTime,format="%m/%d"),
y=value,
group = Year,
color=strftime(DateTime,format="%Y")))+
# size=.1))+
scale_color_discrete(name="Year")+
labs(x="date")
有谁知道为什么我无法使用 scale_x_datetime 即使我的 x 轴 (DateTime) 是 POSIXct 日期时间格式?如果您有任何建议,我将不胜感激
正如我在评论中所说,问题是您通过 strftime(DateTime,format="%m/%d")
将 DateTime
列转换为一个字符,这就是为什么您的秤非常拥挤的原因,也是为什么scale_x_date
或 _datetime
将不起作用。
相反,实现您想要的结果的一种选择是
- 在您使用同一年份的数据中添加辅助日期列。
- 将此帮助日期列映射到
x
注意:我稍微修改了你的数据集以说明第二年。
Rayner_up_summer$DateTime <- as.Date(paste0("2018/", Rayner_up_summer$Day))
library(ggplot2)
ggplot() +
geom_line(data = Rayner_up_summer, aes(
x = DateTime,
y = value,
group = Year,
color = factor(Year)
)) +
scale_x_date(date_labels = "%m/%d") +
scale_color_discrete(name = "Year") +
labs(x = "date")
数据
Rayner_up_summer <- structure(list(X = c(
6775L, 6776L, 6777L, 6778L, 6779L, 6780L,
6781L, 6782L, 6783L, 6784L, 6775L, 6776L, 6777L, 6778L, 6779L,
6780L, 6781L, 6782L, 6783L, 6784L
), site = c(
"RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP"
), DateTime = structure(c(
1531173600,
1531260000, 1531346400, 1531432800, 1531519200, 1531605600, 1531692000,
1531778400, 1531864800, 1531951200, 1531173600, 1531260000, 1531346400,
1531432800, 1531519200, 1531605600, 1531692000, 1531778400, 1531864800,
1531951200
), class = c("POSIXct", "POSIXt"), tzone = ""), value = c(
19.8,
21.2, 20.4, 20.1, 17.3, 19.5, 21.2, 21, 19.8, 16.8, 21.5740302174818,
21.6853770664893, 18.4306976739317, 21.1522381303366, 20.208727594465,
19.5954797456507, 20.6829415732063, 17.6733329861891, 20.2849614520092,
20.5253239201847
), Month = c(
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), Year = c(
2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019
), Day = c(
"07/10",
"07/11", "07/12", "07/13", "07/14", "07/15", "07/16", "07/17",
"07/18", "07/19", "07/10", "07/11", "07/12", "07/13", "07/14",
"07/15", "07/16", "07/17", "07/18", "07/19"
)), row.names = c(
"1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "21", "31",
"41", "51", "61", "71", "81", "91", "101"
), class = "data.frame")