简体   繁体   English

做ggplot facet时是否有R function来获得最大y轴?

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

I have a facet graph where it's free = scales , so each plot has a different y-axis.我有一个free = scales的构面图,因此每个 plot 都有不同的 y 轴。 I want to know if there is a function that can return the max y-axis so that the text always appears on the top of each plot, like this:我想知道是否有一个 function 可以返回最大 y 轴,以便文本始终出现在每个 plot 的顶部,如下所示:

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

I haven't been able to find any resources similar to this issue and was struggling to see if I could extract the layout information with ggplot_build or g_table.我无法找到与此问题类似的任何资源,并且正在努力查看是否可以使用 ggplot_build 或 g_table 提取布局信息。 If I missed a resource, please let me know.如果我错过了资源,请告诉我。 Thank you in advance.先感谢您。

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")

在此处输入图像描述

Or with facet-varying data:或者使用 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.

相关问题 在单个 ggplot facet 中反转 y 轴 - Reversing y-axis in an individual ggplot facet R ggplot facet_wrap具有不同的y轴标签,一个值,一个百分比 - R ggplot facet_wrap with different y-axis labels, one values, one percentages R ggplot2双y轴多面环绕,一条直方图和另一条线 - R ggplot2 Dual y-axis facet wrap, one histogram and other line 在 R 中使用 ggplot() 时,y 轴刻度限制不起作用 - y-axis scale limits not working when using ggplot() in R 在 ggplot/ggplotly 中使用 facet_wrap 时,如何防止 y 轴压扁标签? - How to prevent the y-axis from squishing the labels when using facet_wrap in ggplot/ggplotly? 当 facet_wrap() 中的比例设置为“自由”时,如何增加 ggplot 中的默认 y 轴限制 - How to increase the default y-axis limit in ggplot when scales in facet_wrap() are set to "free" 在 ggplot2 中使用 facet_grid() 时如何定义常见的 y 轴限制 - How to define common y-axis limits when using facet_grid() in ggplot2 使用 facet_wrap 时在 ggplot2 中使用具有不同比例因子的辅助 y 轴 - Using secondary y-axis in ggplot2 with different scale factor when using facet_wrap 带有facet_grid的ggplot2图的两个Y轴 - two Y-axis for ggplot2 graph with a facet_grid 按 y 轴值在 ggplot 的每个方面对因子进行排序 - Ordering factors in each facet of ggplot by y-axis value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM