Home > Net >  ggplot x-axis tick marks missing
ggplot x-axis tick marks missing

Time:05-30

I'm having trouble figuring out what's going on with the tick marks on the x-axis. It looks like some are missing and I can't understand why...

Data:

> 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 = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("prop_zero", "prop_nonzero"
), class = "factor"), 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"
))

# Set breaks
years <- seq(2009, 2021)

# Change which factor gets plotted first
prop$class <- factor(prop$class, levels = c("prop_zero", "prop_nonzero"))

Plot:

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)))  
  guides(fill = guide_legend(reverse = TRUE))

CodePudding user response:

The issue is that you are excluding some of the years (i.e., 2006-2008) in years. So, if you only want to have those years, then you can add in limits to scale_x_continuous:

library(tidyverse)

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)), limits = c(2008, 2022))  
  guides(fill = guide_legend(reverse = TRUE))

Output

enter image description here

Or if you want to include all years, then you need to add those missing to years:

years <- seq(2005, 2022)

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)))  
  guides(fill = guide_legend(reverse = TRUE))

enter image description here

  • Related