我想在我的 x 軸上繪制以下特定日期:“2022-04-21”、“2022-05-20”、“2022-05-23”、“2022-05-27”、“2022-05- 30”、“2022-06-03”、“2022-06-07”、“2022-06-10”、“2022-06-13”。這些日期需要固定在軸上,這意味著如果我的繪圖中有資料點而不對應于這些日期之一(空值或 Null 值),它們仍然需要出現在軸上。我嘗試了以下代碼但沒有成功:
ggplot(Afstand_ind, aes(Datum, distance_m))
geom_point()
scale_x_date(date_labels="%b %d", breaks = as.Date("2022-04-21", "2022-06-13"), minor_breaks = as.Date(c("2022-04-21", "2022-05-20", "2022-05-23", "2022-05-27", "2022-05-30", "2022-06-03", "2022-06-07", "2022-06-10", "2022-06-13")))
scale_y_continuous(breaks = seq(0,150, by = 5))
theme_classic()
df = Afstand_ind
structure(list(Datum_Tijd = structure(c(1653637570, 1653285342,
1654242563, 1654578739, 1654837567, 1653899310, 1655108033), tzone = "", class = c("POSIXct",
"POSIXt")), Datum = structure(c(19139, 19135, 19146, 19150, 19153,
19142, 19156), class = "Date"), Tijd = c("09:46:10", "07:55:42",
"09:49:23", "07:12:19", "07:06:07", "10:28:30", "10:13:53"),
Reader_ID = c("A0", "A0", "A0", "A0", "A0", "A0", "A0"),
HEX_Tag_ID = c("3D6.15341BBB4F", "3D6.15341BBB4F", "3D6.15341BBB4F",
"3D6.15341BBB4F", "3D6.15341BBB4F", "3D6.15341BBB4F", "3D6.15341BBB4F"
), Longitude = c(4.70917, 4.70918, 4.70918, 4.70914, 4.70914,
4.70927, 4.70921), Latitude = c(51.934189, 51.9342, 51.934269,
51.93428, 51.934292, 51.934341, 51.934441), x = c(108370.273,
108370.972, 108371.043, 108368.304, 108368.317, 108377.308,
108373.285), y = c(438631.208, 438632.425, 438640.102, 438641.351,
438642.686, 438648.054, 438659.218), `Lengte_(cm)` = c(9.7,
9.7, 9.7, 9.7, 9.7, 9.7, 9.7), Geslacht = c("vrouw", "vrouw",
"vrouw", "vrouw", "vrouw", "vrouw", "vrouw"), Sloot = c("22",
"22", "22", "22", "22", "22", "22"), Lengte_8e_lichting = c(NA,
NA, NA, NA, NA, NA, 10.3), Lengteklasse = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L), .Label = c("6", "7", "8", "9", "10",
"11", "12", "13"), class = "factor"), distance_m = c(77.8879654012696,
79.1440538153195, 86.8156131287763, 87.953110772786, 89.2887843834125,
95.2906913528804, 106.044905287657)), row.names = c(NA, -7L
), class = c("tbl_df", "tbl", "data.frame"))
uj5u.com熱心網友回復:
如果您希望中斷出現在 x 軸上,則需要將它們設定為breaks. minor_breaks僅適用于網格線,繪圖沒有,因為您設定了“theme_classic limits ”. You will also need to set 以確保軸從沒有資料的地方開始:
ggplot(Afstand_ind, aes(Datum, distance_m))
geom_point()
scale_x_date(date_labels="%b %d",
limits = as.Date(c('2022-04-21', '2022-06-13')),
breaks = as.Date(c("2022-04-21", "2022-05-20",
"2022-05-23", "2022-05-27",
"2022-05-30", "2022-06-03",
"2022-06-07", "2022-06-10",
"2022-06-13")))
scale_y_continuous(breaks = seq(0,150, by = 5))
theme_classic()

為了使日期等距,我們需要 x 軸是離散的,這需要創建一個包含適當日期標簽的因子列:
Afstand_ind$plot_date <- strftime(Afstand_ind$Datum, '%b %d')
Afstand_ind$plot_date <- factor(Afstand_ind$plot_date,
strftime(as.Date(c("2022-04-21", "2022-05-20",
"2022-05-23", "2022-05-27",
"2022-05-30", "2022-06-03",
"2022-06-07", "2022-06-10",
"2022-06-13")), '%b %d'))
ggplot(Afstand_ind, aes(plot_date, distance_m))
geom_point()
scale_x_discrete(drop = FALSE)
scale_y_continuous(breaks = seq(0,150, by = 5))
theme_classic()

uj5u.com熱心網友回復:
您可以使用coord_cartesian():
library(ggplot2)
ggplot(Afstand_ind, aes(Datum, distance_m))
geom_point()
scale_x_date(date_labels="%b %d", breaks = as.Date(c("2022-04-21", "2022-05-20", "2022-05-23", "2022-05-27", "2022-05-30", "2022-06-03", "2022-06-07", "2022-06-10", "2022-06-13")))
coord_cartesian(xlim = as.Date(c("2022-04-21", "2022-06-13")))
theme_classic()
theme(axis.text.x = element_text(angle = 45, hjust=1))

由reprex 包于 2022-06-23 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/495180.html
上一篇:重疊面積圖ggplot2R
