I want to plot the following specific dates on my x axis: "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". These dates need to be fixed on the axis, meaning that if I have data points in my plot without corresponding to one of these dates (empty or Null values), they still need to appear on the axis. I tried the following code without succes:
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"))
CodePudding user response:
If you want the breaks to appear on the x axis, you need to set them in breaks
. The minor_breaks
are only for grid lines, which plot doesn't have because you have set 'theme_classic. You will also need to set
limits` to ensure that the axis starts where there is no data:
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()
To have the dates equally spaced, we need the x axis to be discrete, which requires creating a factor column of the appropriate date labels:
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()
CodePudding user response:
You can use 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))
Created on 2022-06-23 by the reprex package (v2.0.1)