有沒有辦法在不改變繪圖順序的情況下更改堆疊條形圖圖例中因子水平的順序(并且不會錯誤標記資料)?我想先將順序更改為“在場”,然后是“缺席”。
我也遇到了刻度線略微向一側移動的問題。
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")

uj5u.com熱心網友回復:
在這里找到答案!:在不改變情節順序的情況下翻轉圖例的順序
只需將此代碼添加到 ggplot 的末尾: guides(fill = guide_legend(reverse = TRUE))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482736.html
