重現:
library(tidyverse)
# use this for geom_text annotation
diamonds_annotations <- diamonds |>
group_by(cut) |>
summarise(SD_Price = paste0("sd = ", round(sd(price)), 2),
Cor_Price = paste0("cor = ", round(cor(price, carat), 2)))
diamonds |>
ggplot(aes(x = carat, y = price))
geom_point()
facet_wrap(. ~ cut)
這給了我一個情節:

I would like to annotate the facets with the standard deviation sd() of price and also with the correlation cor() of price with carat. Tried:
diamonds |>
ggplot(aes(x = carat, y = price))
geom_point()
facet_wrap(. ~ cut)
geom_text(data = diamonds_annotations, mapping = aes(x = -Inf, y = -Inf, label = SD_Price))
geom_text(data = diamonds_annotations, mapping = aes(x = -Inf, y = -Inf, label = Cor_Price))

Here it looks like the text is cut off along the bottom. Tried several variations of playing with vjust and hjust e.g:
diamonds |>
ggplot(aes(x = carat, y = price))
geom_point()
facet_wrap(. ~ cut)
geom_text(data = diamonds_annotations, mapping = aes(x = -Inf, y = -Inf, label = SD_Price), vjust = -8, hjust = -3, size = 3)
geom_text(data = diamonds_annotations, mapping = aes(x = -Inf, y = -Inf, label = Cor_Price), vjust = -6, hjust = -2, size = 3)

Here I can see the text cut off but still 'there' over on the right hand side.
This feels too fiddly and I wondered if I was doing something 'wrong'. Is there a way to consistently position the text in a corner, e.g. in this plot the bottom right hand side tends to be open space... is there a way to align right and bottom?
Same question e.g. right and top?
uj5u.com熱心網友回復:
這很棘手,但可能。首先,創建沒有注釋的圖:
p <- diamonds |>
ggplot(aes(x = carat, y = price))
geom_point()
facet_wrap(. ~ cut)
現在從繪圖物件中獲取 x 和 y 范圍:
x_range <- layer_scales(p)$x$range$range
y_range <- layer_scales(p)$y$range$range
現在我們可以使用這些資訊在每個構面內的固定位置添加文本:
p geom_text(data = diamonds_annotations,
x = x_range[2] - 0.1 * (diff(x_range)),
y = y_range[1] 0.1 * (diff(y_range)),
aes(label = paste(SD_Price, Cor_Price, sep = "\n")),
hjust = 1)

同樣,對于右上角的固定位置,我們可以這樣做:
p geom_text(data = diamonds_annotations,
x = x_range[2] - 0.1 * (diff(x_range)),
y = y_range[2] - 0.1 * (diff(y_range)),
aes(label = paste(SD_Price, Cor_Price, sep = "\n")),
hjust = 1)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/435708.html
上一篇:如何填充由geom_xspline創建的曲線下的區域
下一篇:ggplot按多列分組
