我的資料集是一個資料框,其中包含過去 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 僅適用于 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 Date Time 格式?我將不勝感激您的任何建議
uj5u.com熱心網友回復:
正如我在評論中所說的那樣,問題在于您將DateTime列轉換為字符,strftime(DateTime,format="%m/%d")這就是您的比例非常擁擠的原因,也是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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/377675.html
