[英]Plotting rectangles in ggplot2 - Invalid input: time_trans works with objects of class POSIXct only
[英]ggplot Time Series Figure Error: Invalid input: time_trans works with objects of class POSIXct only BUT data is in POSIXct
我的數據集是 dataframe,過去 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
我的目標是制作一個每年都有不同線條的折線圖。 到目前為止,在互聯網的大力幫助下,我每年都制作了一條線,但規模非常緊湊。
我想重新調整我的時間序列中的 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.