ggplot - some axis tick marks missing (or shifted to the right)

I'm having trouble figuring out what's going on with the tick marks on the x-axis. It looks like they got shifted to the right and I can't understand why...

ggplot(prop, aes(x = CYR, y = proportions, fill = class)) +
  geom_bar(position = "fill", stat = "identity") +
  scale_fill_manual(values = c("grey70", "grey20"), labels = c("Absence", "Presence")) + 
  scale_y_continuous(limits = c(0, 1.0), expand = expansion(mult = c(0, 0.05))) +
  scale_x_continuous(breaks = years, labels = ~ rep("", length(.x))) +
  # CYR labels
  annotate(
    geom = "text",
    x = prop$CYR,
    y = -Inf,
    label = prop$CYR,
    size = 6.5 / .pt,
    vjust = 2.5
  ) +
  # WYR labels
  annotate(
    geom = "text",
    x = prop$CYR,
    y = -Inf,
    label = prop$WYR,
    size = 6.5 / .pt,
    vjust = 4,
    color = "grey"
  ) +
  # CYR title
  annotate(
    geom = "text",
    x = -Inf,
    y = -Inf,
    label = c("CYR"),
    vjust = 2.5, hjust = 1,
    size = 6.5 / .pt
  ) +
  # WYR title
  annotate(
    geom = "text",
    x = -Inf,
    y = -Inf,
    label = c("WYR"),
    vjust = 4, hjust = 1,
    size = 6.5 / .pt,
    color = "grey") +
  coord_cartesian(clip = "off") +
  theme(
    axis.text.x.bottom = element_text(margin = margin(t = 8.8, b = 8.8)),
    axis.title.x = element_blank(),
    axis.text.y = element_text(size = 10),
    axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0), size = 14),
    axis.ticks = element_line(colour = "black", size = 1), 
    legend.title=element_blank(),
    panel.border = element_rect(fill = NA, color = "black", size = 1), 
    plot.title = element_text(hjust = 0.5)) +
  labs(y = "% presence/absence") +
  ggtitle("DRY SEASONS")

Can you post the data so the ggplot code can be run without having to invent data? You can do that by posting the output of

dput(prop)

Sorry for the delayed response! The site kept my question hidden for review.

>dput(prop)
structure(list(WYR = c(2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 
2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 
2013L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L, 2017L, 2017L, 
2018L, 2018L, 2019L, 2019L, 2020L, 2020L, 2021L, 2021L, 2022L, 
2022L), CYR = c(2005L, 2005L, 2006L, 2006L, 2007L, 2007L, 2008L, 
2008L, 2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 
2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L, 2017L, 
2017L, 2018L, 2018L, 2019L, 2019L, 2020L, 2020L, 2021L, 2021L
), class = c("prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", 
"prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", 
"prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", 
"prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", 
"prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", 
"prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", 
"prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero"
), proportions = c(0.695652173913043, 0.304347826086957, 0.466666666666667, 
0.533333333333333, 0.659574468085106, 0.340425531914894, 0.48936170212766, 
0.51063829787234, 0.739130434782609, 0.260869565217391, 0.739130434782609, 
0.260869565217391, 0.466666666666667, 0.533333333333333, 0.543478260869565, 
0.456521739130435, 0.829787234042553, 0.170212765957447, 0.808510638297872, 
0.191489361702128, 0.425531914893617, 0.574468085106383, 0.446808510638298, 
0.553191489361702, 0.638297872340426, 0.361702127659574, 0.425531914893617, 
0.574468085106383, 0.553191489361702, 0.446808510638298, 0.595744680851064, 
0.404255319148936, 0.685393258426966, 0.314606741573034)), row.names = c(NA, 
-34L), class = c("tbl_df", "tbl", "data.frame"))

Your ggplot code refers to an object named years. I invented one with the range 2005 - 2021 and I get with tick marks at the appropriate places. I suggest you check what values your years variable has.

library(ggplot2)

years <- 2005:2021
prop <- structure(list(WYR = c(2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 
                               2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 
                               2013L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L, 2017L, 2017L, 
                               2018L, 2018L, 2019L, 2019L, 2020L, 2020L, 2021L, 2021L, 2022L, 
                               2022L), 
                       CYR = c(2005L, 2005L, 2006L, 2006L, 2007L, 2007L, 2008L, 
                                               2008L, 2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 
                                               2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L, 2017L, 
                                               2017L, 2018L, 2018L, 2019L, 2019L, 2020L, 2020L, 2021L, 2021L
                               ), 
                       class = c("prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", 
                                            "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", 
                                            "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", 
                                            "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", 
                                            "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", 
                                            "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", 
                                            "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero"
                               ), 
                       proportions = c(0.695652173913043, 0.304347826086957, 0.466666666666667, 
                                                  0.533333333333333, 0.659574468085106, 0.340425531914894, 0.48936170212766, 
                                                  0.51063829787234, 0.739130434782609, 0.260869565217391, 0.739130434782609, 
                                                  0.260869565217391, 0.466666666666667, 0.533333333333333, 0.543478260869565, 
                                                  0.456521739130435, 0.829787234042553, 0.170212765957447, 0.808510638297872, 
                                                  0.191489361702128, 0.425531914893617, 0.574468085106383, 0.446808510638298, 
                                                  0.553191489361702, 0.638297872340426, 0.361702127659574, 0.425531914893617, 
                                                  0.574468085106383, 0.553191489361702, 0.446808510638298, 0.595744680851064, 
                                                  0.404255319148936, 0.685393258426966, 0.314606741573034)), row.names = c(NA, -34L), 
                  class = c("tbl_df", "tbl", "data.frame"))

ggplot(prop, aes(x = CYR, y = proportions, fill = class)) +
  geom_bar(position = "fill", stat = "identity") +
  scale_fill_manual(values = c("grey70", "grey20"), labels = c("Absence", "Presence")) + 
  scale_y_continuous(limits = c(0, 1.0), expand = expansion(mult = c(0, 0.05))) +
  scale_x_continuous(breaks = years,labels = ~ rep("", length(.x))) +
  # CYR labels 
  annotate(
    geom = "text",
    x = prop$CYR,
    y = -Inf,
    label = prop$CYR,
    size = 6.5 / .pt,
    vjust = 2.5
  ) +
  # WYR labels
  annotate(
    geom = "text",
    x = prop$CYR,
    y = -Inf,
    label = prop$WYR,
    size = 6.5 / .pt,
    vjust = 4,
    color = "grey"
  ) +
  # CYR title
  annotate(
    geom = "text",
    x = -Inf,
    y = -Inf,
    label = c("CYR"),
    vjust = 2.5, hjust = 1,
    size = 6.5 / .pt
  ) +
  # WYR title
  annotate(
    geom = "text",
    x = -Inf,
    y = -Inf,
    label = c("WYR"),
    vjust = 4, hjust = 1,
    size = 6.5 / .pt,
    color = "grey") +
  coord_cartesian(clip = "off") +
  theme(
    axis.text.x.bottom = element_text(margin = margin(t = 8.8, b = 8.8)),
    axis.title.x = element_blank(),
    axis.text.y = element_text(size = 10),
    axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0), size = 14),
    axis.ticks = element_line(colour = "black", size = 1), 
    legend.title=element_blank(),
    panel.border = element_rect(fill = NA, color = "black", size = 1), 
    plot.title = element_text(hjust = 0.5)) +
  labs(y = "% presence/absence") +
  ggtitle("DRY SEASONS")

Created on 2022-05-29 by the reprex package (v2.0.1)

I just realized the mistake. The "years" object was from 2009 to 2021. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.