簡體   English   中英

做ggplot facet時是否有R function來獲得最大y軸?

[英]Is there an R function to get the max y-axis when doing ggplot facet?

我有一個free = scales的構面圖,因此每個 plot 都有不同的 y 軸。 我想知道是否有一個 function 可以返回最大 y 軸,以便文本始終出現在每個 plot 的頂部,如下所示:

geom_text(data,aes(x=25,y=max_y,label="test text"))

我無法找到與此問題類似的任何資源,並且正在努力查看是否可以使用 ggplot_build 或 g_table 提取布局信息。 如果我錯過了資源,請告訴我。 先感謝您。

ggplot(mtcars, aes(wt,mpg)) +
  geom_point() +
  annotate("text", x = -Inf, y = Inf, label= "Test", 
           hjust = -0.2, vjust = 1.2) +
  facet_wrap(~gear, scales = "free")

在此處輸入圖像描述

或者使用 facet-varying 數據:

ggplot(mtcars, aes(wt,mpg)) +
  geom_point() +
  geom_text(data = data.frame(wt = -Inf, mpg = Inf, gear = 3:5, 
                              label = c("Test", "More", "And more")),
            aes(label = label),
            hjust = -0.2, vjust = 1.2) +
  facet_wrap(~gear, scales = "free")

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM