我正在嘗試使用這篇
字幕后:

這個想法是我想避免每次更改資料時都必須設定 y 引數。想象一下,y 軸是不同的(它是這樣的:0.0000、0.0005、0.0010、0.0015)。在這種情況下,適當的 y 將是 -0.0005,因為“跳躍”是 0.0005,所以我只需將其設為負數。
出于這個原因,我想知道是否可以從 y 軸獲取 COMPLETE 值向量。例如,如果我們想從之前的影像中獲取 y 軸的所有值/中斷,則為:c(0.0, 0.2, 0.4, 0.6).
有誰知道我是否可以從繪圖的 y 軸獲取所有值?
提前致謝
uj5u.com熱心網友回復:
p您可以像這樣從物件中獲取 y 軸中斷:
as.numeric(na.omit(layer_scales(p)$y$break_positions()))
#> [1] 0.0 0.2 0.4 0.6
但是,如果您希望標簽在面板下方的固定距離而不管 y 軸比例如何,最好使用整個面板范圍的固定部分而不是中斷:
yrange <- layer_scales(p)$y$range$range
ypos <- min(yrange) - 0.2 * diff(yrange)
p coord_cartesian(clip = "off",
ylim = layer_scales(p)$y$range$range,
xlim = layer_scales(p)$x$range$range)
geom_text(data = caption_df,
aes(y = ypos, label = c(levels(data$Sex))))

For example, suppose you had a y scale that was twice the size:
p <- data %>%
ggplot(aes(value))
geom_density(lwd = 1.2, colour="red", show.legend = FALSE)
geom_histogram(aes(y= 2 * ..density.., fill = id), bins=10, col="black", alpha=0.2)
facet_grid(id ~ Sex )
xlab("type_data")
ylab("Density")
ggtitle("title")
guides(fill=guide_legend(title="legend_title"))
theme(strip.text.y = element_blank())
Then the exact same code would give you the exact same label placement, without any reference to breaks:
yrange <- layer_scales(p)$y$range$range
ypos <- min(yrange) - 0.2 * diff(yrange)
p coord_cartesian(clip = "off",
ylim = layer_scales(p)$y$range$range,
xlim = layer_scales(p)$x$range$range)
geom_text(data = caption_df,
aes(y = ypos, label = c(levels(data$Sex))))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450635.html
上一篇:使用ggplot2Rstudio進行時間序列可視化(國家級)
下一篇:將坐標添加到現有的gpplot
