Reputation: 13792
Ggplot adds ticks marks centrally using this code:
tibble(year = 2010:2020, count = sample(1:100, 11)) %>%
mutate(year = paste0("01/01/", year)) %>%
mutate(year = dmy(year)) %>%
ggplot +
geom_bar(aes(year, count), stat = "identity", width = 240, position = position_dodge(width = 0.1)) +
scale_x_date(date_labels = "%y", date_breaks = "1 year") +
scale_y_continuous(limits = c(0,100), expand = c(0, 0)) +
theme_classic()
But I need tick marks either side of the year. I have photoshopped in the tick marks plot to show where I need them. What code can I use to add ticks marks either side of year?
Upvotes: 2
Views: 193
Reputation: 37913
You can draw the tick marks yourself with geom_segment()
, turn off the clipping and hide the default ones.
library(ggplot2)
library(dplyr)
library(lubridate)
tbl <- tibble(year = 2010:2020, count = sample(1:100, 11)) %>%
mutate(year = paste0("01/01/", year)) %>%
mutate(year = dmy(year))
ggplot(tbl) +
geom_bar(aes(year, count), stat = "identity", width = 240, position = position_dodge(width = 0.1)) +
geom_segment(aes(x = year, xend = year), y = 0, yend = -1,
colour ="black", position = position_nudge(x = 240/2)) +
geom_segment(aes(x = year, xend = year), y = 0, yend = -1,
colour ="black", position = position_nudge(x = -240/2)) +
scale_x_date(date_labels = "%y", date_breaks = "1 year") +
scale_y_continuous(limits = c(0,100), expand = c(0, 0),
oob = scales::oob_keep) +
coord_cartesian(clip = "off") +
theme_classic() +
theme(axis.ticks = element_blank())
Created on 2021-04-10 by the reprex package (v1.0.0)
Upvotes: 1