重新排列堆叠的条形图图例标签而不更改 R 中的图(并修复刻度线)
Rearrange stacked barplot legend labels without changing plot (and fix tick marks) in R
有没有办法在不改变图例顺序的情况下改变堆叠条形图图例中因子水平的顺序(并且不会错误标记数据)?我想把顺序改为先“在场”,再“不在场”。
我也遇到了刻度线稍微向一侧移动的问题。
dput(prop)
structure(list(WYR = 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), 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), 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), .Label = c("prop_zero",
"prop_nonzero"), class = "factor"), proportions = c(0.170212765957447,
0.829787234042553, 0.170212765957447, 0.829787234042553, 0.361702127659574,
0.638297872340426, 0.234042553191489, 0.765957446808511, 0.234042553191489,
0.765957446808511, 0.434782608695652, 0.565217391304348, 0.58695652173913,
0.41304347826087, 0.574468085106383, 0.425531914893617, 0.51063829787234,
0.48936170212766, 0.595744680851064, 0.404255319148936, 0.608695652173913,
0.391304347826087, 0.51063829787234, 0.48936170212766, 0.404255319148936,
0.595744680851064, 0.319148936170213, 0.680851063829787, 0.468085106382979,
0.531914893617021, 0.608695652173913, 0.391304347826087)), row.names = c(NA,
-32L), 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")
在这里找到答案!:Flip ordering of legend without altering ordering in plot
只需将此代码添加到 ggplot 的末尾:+ guides(fill = guide_legend(reverse = TRUE))
有没有办法在不改变图例顺序的情况下改变堆叠条形图图例中因子水平的顺序(并且不会错误标记数据)?我想把顺序改为先“在场”,再“不在场”。
我也遇到了刻度线稍微向一侧移动的问题。
dput(prop)
structure(list(WYR = 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), 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), 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), .Label = c("prop_zero",
"prop_nonzero"), class = "factor"), proportions = c(0.170212765957447,
0.829787234042553, 0.170212765957447, 0.829787234042553, 0.361702127659574,
0.638297872340426, 0.234042553191489, 0.765957446808511, 0.234042553191489,
0.765957446808511, 0.434782608695652, 0.565217391304348, 0.58695652173913,
0.41304347826087, 0.574468085106383, 0.425531914893617, 0.51063829787234,
0.48936170212766, 0.595744680851064, 0.404255319148936, 0.608695652173913,
0.391304347826087, 0.51063829787234, 0.48936170212766, 0.404255319148936,
0.595744680851064, 0.319148936170213, 0.680851063829787, 0.468085106382979,
0.531914893617021, 0.608695652173913, 0.391304347826087)), row.names = c(NA,
-32L), 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")
在这里找到答案!:Flip ordering of legend without altering ordering in plot
只需将此代码添加到 ggplot 的末尾:+ guides(fill = guide_legend(reverse = TRUE))