我想在我的折線圖上添加 2 行 x 軸標簽,但不確定如何合并第一個 x 軸的連續標簽和中斷(列“CYR” - 日歷年的縮寫)。我想讓第二個軸(WYR)在第一個標簽和第二個標簽之間開始(WYR = 2010 在 CYR = 2009 -> 2010 之間開始)。我也不確定如何添加 2 個 x 軸標題,也許在每個 x 軸行的開頭?
我的資料:
> dput(wet_pivot)
structure(list(WYR = c("WR_2010", "WR_2011", "WR_2012", "WR_2013",
"WR_2014", "WR_2015", "WR_2016", "WR_2017", "WR_2018", "WR_2019",
"WR_2020", "WR_2021", "WR_2022"), CYR = c(2009, 2010, 2011, 2012,
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021), Season = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("DRY",
"WET"), class = "factor"), N = c(59L, 63L, 69L, 70L, 72L, 71L,
71L, 72L, 71L, 68L, 70L, 48L, 72L), n_mean = c(0.00696806934430411,
0.000649730847004026, 0.00288256551918419, 0.01141088388474,
0.000536174103147671, 0.00349584646220785, 0.000482925207291882,
0.00245359625194744, 0.00292096956686587, 0.00252817293686805,
0.00196286772014134, 0.00501799463867351, 0.00132244297252478
), n_median = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), sd = c(0.030946706350869,
0.00248965525641742, 0.0100973832581282, 0.051577934580242, 0.00331468784320076,
0.0266064084754242, 0.00212505905295283, 0.00675243933898364,
0.0119729983336735, 0.00639785127193391, 0.00930625647382774,
0.0136275258272549, 0.00543420856675111), se = c(0.00402891799826298,
0.000313667078988821, 0.00121558209746373, 0.0061647423020683,
0.000390639708573979, 0.00315759975690469, 0.000252198110662322,
0.000795782607691024, 0.00142093348159893, 0.000775853428563995,
0.00111231039833223, 0.00196696392618855, 0.000640427621321956
)), row.names = c(NA, -13L), class = "data.frame")
我的嘗試:
years <- seq(2009,2021,1)
labs <- seq(2009,2021,by=1)
myplot <- ggplot(wet_pivot, aes(x = CYR, y = n_mean))
geom_errorbar(aes(ymin=n_mean-se, ymax=n_mean se), width=.2, color = "black")
geom_point(color = "black", shape = 1, size = 2)
geom_line(color = "black")
scale_y_continuous(limits = c(0, 0.04), expand = expansion(mult = c(0, 0.05)))
scale_x_continuous(breaks= years, labels = labs)
myplot
annotate(geom = "text",
x = 1:nrow(wet_pivot),
y = min(wet_pivot$n_mean),
label = labs,
vjust = 3.5)
annotate(geom = "text",
x = 1:nrow(wet_pivot),
y = min(wet_pivot$n_mean),
label = wet_pivot$WYR,
vjust = 5)
uj5u.com熱心網友回復:
您確實可以使用文本注釋來代替 x 軸標簽。一些建議:
- 設定
y = -Inf為自動將文本放置在底部,與繪圖上的任何資料無關。vjust確實可以用來把它放得更遠。 - 您需要
coord_cartesian(clip = "off")實際顯示文本。 - 您可以放置??帶有額外注釋層的“標題”,
x = -Inf將其放置在左側。
我在下面的示例中使用了上述內容。也許文本仍然很大,因此您可以將其設定8.8 / .pt為更小。(在使用的/ .ptmm 之間轉換geom_text()為在主題中使用的點)
library(ggplot2)
# wet_pivot <- structure(...) # omitted for previty
ggplot(wet_pivot, aes(x = CYR, y = n_mean))
geom_errorbar(aes(ymin=n_mean-se, ymax=n_mean se), width=.2, color = "black")
geom_point(color = "black", shape = 1, size = 2)
geom_line(color = "black")
scale_y_continuous(limits = c(0, 0.04), expand = expansion(mult = c(0, 0.05)))
scale_x_continuous(breaks= years, labels = ~ rep("", length(.x)))
annotate(geom = "text",
x = wet_pivot$CYR,
y = -Inf,
label = labs,
size = 8.8 / .pt,
vjust = 2.5)
annotate(geom = "text",
x = wet_pivot$CYR,
y = -Inf,
label = wet_pivot$WYR,
size = 8.8 / .pt,
vjust = 4)
# Titles
annotate(geom = "text",
x = -Inf,
y = -Inf,
label = c("CYR", "WYR"),
vjust = c(2.5, 4), hjust = 1,
size = 8.8 / .pt
)
coord_cartesian(clip = "off")
theme(
# Make extra space between axis ticks and axis title
axis.text.x.bottom = element_text(margin = margin(t = 8.8, b = 8.8))
)

由reprex 包(v2.0.1)于 2022-05-19 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/478380.html
上一篇:Twitter資料情感分析
下一篇:堆疊式扁平小提琴ggplot
