簡體   English   中英

ggplot x軸刻度線丟失

[英]ggplot x-axis tick marks missing

我無法弄清楚 x 軸上的刻度線發生了什么。 好像有一些不見了,我不明白為什么......

數據:

> 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"))

陰謀:

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))

問題是您在years中排除了某些年份(即 2006-2008)。 因此,如果您只想擁有那些年,那么您可以在scale_x_continuous中添加limits

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))

輸出

在此處輸入圖像描述

或者,如果您想包括所有年份,則需要將缺少的年份添加到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))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM