I can't figure out how to customize the x-axis in the graph below. At minimum, I would like each month to be labeled on the x-axis. All help is appreciated.
dput(head(cohort1,10))
structure(list(DeerId = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("A82118", "A82120", "A82121", "A82122",
"A82126", "A82127", "A82132", "A82135", "A82136", "A82139", "A82140",
"A82141", "A82142", "A82145", "A82146"), class = "factor"), DateTime = structure(c(1557401400,
1557403200, 1557405000, 1557406800, 1557408600, 1557410400, 1557412200,
1557414000, 1557415800, 1557417600), tzone = "CST6CDT", class = c("POSIXct",
"POSIXt")), Hour = structure(c(7L, 8L, 8L, 9L, 9L, 10L, 10L,
11L, 11L, 12L), .Label = c("0", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23"), class = "factor"), Mean_dist_m = c(3002.86596551194,
3072.72756617098, 3095.13565305238, 3091.97138086376, 3021.62530500961,
2991.56358399469, 3075.78252594177, 3037.51460737638, 3023.45492190491,
3016.40127683411)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -10L), groups = structure(list(
DeerId = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("A82118", "A82120", "A82121", "A82122", "A82126",
"A82127", "A82132", "A82135", "A82136", "A82139", "A82140",
"A82141", "A82142", "A82145", "A82146"), class = "factor"),
DateTime = structure(c(1557401400, 1557403200, 1557405000,
1557406800, 1557408600, 1557410400, 1557412200, 1557414000,
1557415800, 1557417600), tzone = "CST6CDT", class = c("POSIXct",
"POSIXt")), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))
Dist_Date_plot<-ggplot(cohort1, aes(DateTime, Mean_dist_m))
geom_line()
labs(title = "Deer-Cattle Proximity Across Months", x='Time Period', y='Mean Distance (m)')
theme(axis.text=element_text(size=20),axis.title=element_text(size=25,face="bold"),
plot.title = element_text(size=30,hjust = 0.5))
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
Dist_Date_plot
CodePudding user response:
You can use scale_x_datetime
. Note that your sample data only has a few hours on a single day, so it almost just looks like a vertical line here:
ggplot(cohort1, aes(DateTime, Mean_dist_m))
geom_line()
scale_x_datetime(limits = as.POSIXct(c("2019-02-01", "2020-02-01")),
labels = function(x) format(x, "%b"),
breaks = seq(as.POSIXct("2019-01-01"),
as.POSIXct("2020-01-01"),
by = "month"))
labs(title = "Deer-Cattle Proximity Across Months",
x = 'Time Period',
y = 'Mean Distance (m)')
theme(axis.text = element_text(size = 20),
axis.title = element_text(size = 25, face = "bold"),
plot.title = element_text(size = 30, hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))