有沒有辦法制作帶有連續中斷和標簽的堆疊條形圖?將我的資料框從“更寬版本”轉換為“更長版本”允許我制作堆疊條形圖,但現在我的標簽未對齊(CYR 2009 應直接高于 WYR 2010 等),我已修復,但由于這一變化,空的條形圖出現在 2022 年的最右上方。
我試圖只顯示年份 <= 2021 并簡單地將第二個 x 軸向左移動以排列兩個標簽,但這似乎對“空”條形圖空間沒有任何作用。
我的原始資料表:
>dput(prop)
structure(list(WYR = c(2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022), CYR = c(2009, 2010, 2011,
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021),
prop_zero = c(0.745762711864407, 0.888888888888889, 0.739130434782609,
0.842857142857143, 0.958333333333333, 0.915492957746479,
0.929577464788732, 0.75, 0.76056338028169, 0.720588235294118,
0.785714285714286, 0.6875, 0.833333333333333), prop_nonzero = c(0.254237288135593,
0.111111111111111, 0.260869565217391, 0.157142857142857,
0.0416666666666667, 0.0845070422535211, 0.0704225352112676,
0.25, 0.23943661971831, 0.279411764705882, 0.214285714285714,
0.3125, 0.166666666666667)), class = "data.frame", row.names = c(NA,
-13L))
“更長”版本:
>dput(prop)
structure(list(WYR = c(2010, 2010, 2011, 2011, 2012, 2012, 2013,
2013, 2014, 2014, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018,
2019, 2019, 2020, 2020, 2021, 2021, 2022, 2022), CYR = c(2009,
2009, 2010, 2010, 2011, 2011, 2012, 2012, 2013, 2013, 2014, 2014,
2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018, 2019, 2019, 2020,
2020, 2021, 2021), class = c("prop_zero", "prop_nonzero", "prop_zero",
"prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero",
"prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero",
"prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero",
"prop_zero", "prop_nonzero", "prop_zero", "prop_nonzero", "prop_zero",
"prop_nonzero", "prop_zero", "prop_nonzero"), proportions = c(0.745762711864407,
0.254237288135593, 0.888888888888889, 0.111111111111111, 0.739130434782609,
0.260869565217391, 0.842857142857143, 0.157142857142857, 0.958333333333333,
0.0416666666666667, 0.915492957746479, 0.0845070422535211, 0.929577464788732,
0.0704225352112676, 0.75, 0.25, 0.76056338028169, 0.23943661971831,
0.720588235294118, 0.279411764705882, 0.785714285714286, 0.214285714285714,
0.6875, 0.3125, 0.833333333333333, 0.166666666666667)), row.names = c(NA,
-26L), class = c("tbl_df", "tbl", "data.frame"))
中斷和標簽:
years <- seq(2009,2021,1)
labs <- seq(2009,2021,by=1)
陰謀:
ggplot(prop[which(prop$CYR<=2021),], aes(x=CYR, y=proportions, fill=class))
geom_bar(position="fill", stat="identity")
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 = labs,
size = 8.8 / .pt,
vjust = 2.5)
# WYR labels
annotate(geom = "text",
x = prop$WYR,
y = -Inf,
label = prop$WYR,
size = 8.8 / .pt,
vjust = 4,
hjust = 3.4,
color = "grey")
# CYR title
annotate(geom = "text",
x = -Inf,
y = -Inf,
label = c("CYR"),
vjust = 2.5, hjust = 1,
size = 8.8 / .pt)
# WYR title
annotate(geom = "text",
x = -Inf,
y = -Inf,
label = c("WYR"),
vjust = 4, hjust = 1,
size = 8.8 / .pt,
color = "grey")
coord_cartesian(clip = "off")
theme(axis.title.x = element_blank())
# Make extra space between axis ticks and axis title
theme(axis.text.x.bottom = element_text(margin = margin(t = 8.8, b = 8.8)),
axis.text.x = element_text(size = 10, color = "grey"),
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),
panel.border = element_rect(fill = NA, color = "black", size = 1),
legend.position = "none")
labs(y = "Proportion")
uj5u.com熱心網友回復:
問題是您將x = prop$WYR其用作 WYR 標簽的 x 坐標,這會為 2022 年添加一個空條。而是使用x = prop$CYR并洗掉 - 恕我直言 - 不需要hjust對齊兩行標簽。
library(ggplot2)
years <- seq(2009, 2021)
ggplot(prop[which(prop$CYR <= 2021), ], aes(x = CYR, y = proportions, fill = class))
geom_bar(position = "fill", stat = "identity")
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 = 8.8 / .pt,
vjust = 2.5
)
# WYR labels
annotate(
geom = "text",
x = prop$CYR,
y = -Inf,
label = prop$WYR,
size = 8.8 / .pt,
vjust = 4,
color = "grey"
)
# CYR title
annotate(
geom = "text",
x = -Inf,
y = -Inf,
label = c("CYR"),
vjust = 2.5, hjust = 1,
size = 8.8 / .pt
)
# WYR title
annotate(
geom = "text",
x = -Inf,
y = -Inf,
label = c("WYR"),
vjust = 4, hjust = 1,
size = 8.8 / .pt,
color = "grey"
)
coord_cartesian(clip = "off")
theme(axis.title.x = element_blank())
# Make extra space between axis ticks and axis title
theme(
axis.text.x.bottom = element_text(margin = margin(t = 8.8, b = 8.8)),
axis.text.x = element_text(size = 10, color = "grey"),
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),
panel.border = element_rect(fill = NA, color = "black", size = 1),
legend.position = "none"
)
labs(y = "Proportion")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/479260.html
