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)。所以,如果你只想拥有那些年,那么你可以将 limits
添加到 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))
输出
或者如果你想包括所有年份,那么你需要将缺失的年份添加到 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))
我无法弄清楚 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)。所以,如果你只想拥有那些年,那么你可以将 limits
添加到 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))
输出
或者如果你想包括所有年份,那么你需要将缺失的年份添加到 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))