有一個小標題和一個簡單的散點圖:
p <- tibble(
x = rnorm(50, 1),
y = rnorm(50, 10)
)
ggplot(p, aes(x, y)) geom_point()
我得到這樣的東西:

我想對齊(中心,左,右,視情況而定)x 軸的標題 - 在這里相當平淡的x - 與軸上的特定值對齊,在這種情況下說偏心0。有沒有一種方法可以宣告性地做到這一點,而不必求助于愚蠢的(如“無背景關系”)試錯法element_text(hjust=??)。在??這里相當合適,因為每個值都是實驗的結果(我在 RStudio 中的螢屏和 PDF 匯出從未在相當多的情節元素上達成一致)。資料或渲染尺寸的任何更改都可能(或可能不會)使hjust值無效,我正在尋找一種優雅地重新定位自身的解決方案,就像軸一樣。
按照@tjebo 評論中的建議,我對坐標空間進行了更深入的挖掘。hjust = 0.0并hjust = 1.0清楚地將標簽與笛卡爾坐標系范圍對齊(但分別神奇地左對齊和右對齊),因此當我設定特定限制時,精確值的計算hjust很簡單(針對0和hjust = (0 - -1.5) / (3.5 - -1.5) = 0.3):
ggplot(p, aes(x, y))
geom_point()
coord_cartesian(ylim = c(8, 12.5), xlim = c(-1.5, 3.5), expand=FALSE)
theme(axis.title.x = element_text(hjust = 0.3))
對于像x這樣的標簽,這給出了可接受的結果,但對于較長的標簽,對齊再次關閉:
ggplot(p %>% mutate(`Longer X label` = x), aes(x = `Longer X label`, y = y))
geom_point()
coord_cartesian(ylim = c(8, 12.5), xlim = c(-1.5, 3.5), expand=FALSE)
theme(axis.title.x = element_text(hjust = 0.3))
任何進一步的建議非常感謝。
uj5u.com熱心網友回復:
另一個選項(有希望證明第二個答案是不同的)如前所述,將注釋創建為單獨的圖。這消除了范圍問題。我喜歡{patchwork}。
library(tidyverse)
library(patchwork)
p <- tibble( x = rnorm(50, 1), y = rnorm(50, 10))
p1 <- tibble( x = rnorm(50, 1), y = 100*rnorm(50, 10))
## I like to define constants outside my ggplot call
mylab <- "longer_label"
x_demo <- c(-1, 2)
demo_fct <- function(p){
p1 <- ggplot(p, aes(x, y))
geom_point()
labs(x = NULL)
theme(plot.margin = margin())
p2 <- ggplot(p, aes(x, y))
## you need that for your correct alignment with the first plot
geom_blank()
annotate(geom = "text", x = x_demo, y = 1,
label = mylab, hjust = 0)
theme_void()
# you need that for those annoying margin reasons
coord_cartesian(clip = "off")
p1 / p2 plot_layout(heights = c(1, .05))
}
demo_fct(p) plot_annotation(title = "demo1 with x at -1 and 2")

demo_fct(p1) plot_annotation(title = "demo2 with larger data range")

由reprex 包(v2.0.1)于 2021 年 12 月 4 日創建
uj5u.com熱心網友回復:
我仍然認為使用自定義注釋會更好更容易。通常有兩種方法可以做到這一點。要么使用文本層直??接標記(對于我更喜歡??的單個標簽annotate(geom = "text"),要么創建一個單獨的圖并將兩者縫合在一起,例如拼湊而成。
最大的挑戰是在 y 維度上的定位。為此,我通常采用半自動方法,我只需要定義一個常量,并設定相對于資料范圍的坐標,因此理論上范圍的變化應該無關緊要。(他們仍然做了一點,因為面板尺寸也會改變)。下面顯示了兩個不同資料范圍的精確標簽定位示例(對兩者使用相同的常量)
library(tidyverse)
# I only need patchwork for demo purpose, it is not required for the answer
library(patchwork)
p <- tibble( x = rnorm(50, 1), y = rnorm(50, 10))
p1 <- tibble( x = rnorm(50, 1), y = 100*rnorm(50, 10))
## I like to define constants outside my ggplot call
y_fac <- .1
mylab <- "longer_label"
x_demo <- c(-1, 2)
demo_fct <- function(df, x) {map(x_demo,~{
## I like to define constants outside my ggplot call
ylims <- range(df$y)
ggplot(df, aes(x, y))
geom_point()
## set hjust = 0 for full positioning control
annotate(geom = "text", x = ., y = min(ylims) - y_fac*mean(ylims),
label = mylab, hjust = 0)
coord_cartesian(ylim = ylims, clip = "off")
theme(plot.margin = margin(b = .5, unit = "in"))
labs(x = NULL)
})
}
demo_fct(p, x_demo) %>% wrap_plots() plot_annotation(title = "demo 1, label at x = -1 and x = 2")

demo_fct(p1, x_demo) %>% wrap_plots() plot_annotation(title = "demo 2 - different data range")

由reprex 包(v2.0.1)于 2021 年 12 月 4 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/377680.html
