我有以下兩個資料框
df <- data.frame(Groesse=c("A", "B", "C", "D"),
x=c(0, 1, 30, 25),
y=c(0, 90, 85, 20))
df_text <- data.frame(Groesse=c("X", "Y"),
x=c(1, 30),
y=c(105, 105))
并試圖通過以下方式實作我想要的:
plot <- ggplot(df)
geom_point(aes(x, y))
geom_text(data = df_text, aes(x, y, label = "Groesse"))
scale_y_continuous(limits = c(0,100), expand = c(0,0))
coord_cartesian(clip = "off")
theme(plot.margin = unit(c(10, 1, 1, 1), "lines"))
plot
我想讓注釋(使用geom_text())位于散點圖上方,但在繪圖區域之外。但是,我的情節缺少geom_text().

所需的輸出(只是一個草圖):

知道如何在繪圖區域之外添加文本而不產生兩個不同的繪圖然后將它們組合在一個網格中嗎?
uj5u.com熱心網友回復:
除了在坐標函式中關閉剪裁之外,您還需要在其中設定越界oob引數,scale_y_continuous()以便不審查資料。
ggplot(df)
geom_point(aes(x, y))
geom_text(data = df_text, aes(x, y, label = "Groesse"))
scale_y_continuous(limits = c(0,100), expand = c(0,0), oob = ~ .x)
coord_cartesian(clip = "off")
theme(plot.margin = unit(c(10, 1, 1, 1), "lines"))

uj5u.com熱心網友回復:
這只是為了指出使用geom_text()for 注釋的替代方法。對于連續軸,您可以使用輔助軸添加一些注釋。
library(ggplot2)
df <- data.frame(Groesse=c("A", "B", "C", "D"),
x=c(0, 1, 30, 25),
y=c(0, 90, 85, 20))
df_text <- data.frame(Groesse=c("X", "Y"),
x=c(1, 30),
y=c(105, 105))
ggplot(df)
geom_point(aes(x, y))
scale_y_continuous(
limits = c(0,100), expand = c(0,0)
)
scale_x_continuous(
sec.axis = sec_axis(
~.x, name = NULL, breaks = c(1, 30),
labels = rep("Groesse", 2)
)
)
theme(
axis.ticks.length.x.top = unit(0, "pt"),
axis.text.x.top = element_text(margin = margin(b = 10))
)

由
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523464.html
標籤:rggplot2
