如何从R中的facet_grid图中获得真实比例?

发布于 2025-01-16 05:55:24 字数 2679 浏览 6 评论 0原文

我正在尝试添加出现在此 帖子

因此,当我使用facet_grid时,我需要绘图的真实比例(x和y轴)。我知道我可以使用layer_data,因为它保存了图中的所有内容...但是,它并不准确,因为当我尝试使用min建立限制时和 max 从该输出来看,绘图发生了变化。

这里有一个示例:

library(ggplot2)
library(dplyr)

val1 <- c(2.1490626,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)

df1 <- data.frame(value = val1)   
df2 <- data.frame(value = val2)   

data <- bind_rows(lst(df1, df2), .id = 'id')
data$Sex <- rep(c("Male", "Female"), times=84/2)

p <- data %>% 
  ggplot(aes(value)) +
  geom_density(lwd = 1.2, colour="red", show.legend = FALSE) +
  geom_histogram(aes(y=..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())
  
p

image1

plot_info <- layer_data(p)
> min(plot_info$density)
[1] 7.166349e-09
> max(plot_info$density)
[1] 0.5738021

正如您在图中看到的,y 轴从 0 开始,结束时大约为 0.7 左右。然而,最大密度为0.57。

如果我尝试使用来自layer_data的信息:

p + coord_cartesian(clip="off", ylim=c(min(plot_info$density), max(plot_info$density)), 
                  xlim = c(min(plot_info$x), max(plot_info$x)))

绘图完全改变。

输入图片这里的描述

有谁知道如何获得ggplot2facet_grid正在使用的比例?我需要密度(y_轴)的信息和x_轴的信息。

I am trying to add captions as it appears in this post.

For that reason, I need the real scale of the plot (x and y axis) when I am using facet_grid. I know that I can use layer_data, since it saves everything from the plot... However, it is not really accurate, because when I try to establish the limits using min and max from that output, the plot changes.

Here you have an example:

library(ggplot2)
library(dplyr)

val1 <- c(2.1490626,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)

df1 <- data.frame(value = val1)   
df2 <- data.frame(value = val2)   

data <- bind_rows(lst(df1, df2), .id = 'id')
data$Sex <- rep(c("Male", "Female"), times=84/2)

p <- data %>% 
  ggplot(aes(value)) +
  geom_density(lwd = 1.2, colour="red", show.legend = FALSE) +
  geom_histogram(aes(y=..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())
  
p

image1

plot_info <- layer_data(p)
> min(plot_info$density)
[1] 7.166349e-09
> max(plot_info$density)
[1] 0.5738021

As you can see in the plot, the y-axis starts at 0 and if finishes around 0.7 more less. However, the maximum density is 0.57.

If I try to use the info from layer_data:

p + coord_cartesian(clip="off", ylim=c(min(plot_info$density), max(plot_info$density)), 
                  xlim = c(min(plot_info$x), max(plot_info$x)))

The plot changes completely.

enter image description here

Does anyone know how can I get the scales that ggplot2 and facet_grid are using? I need the information of the density (y_axis) and the info from the x_axis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2025-01-23 05:55:24

是的,要直接获取比例,请使用layer_scales(p),它为您提供轴的范围,而不仅仅是数据的范围,这是您从layer_data(p)获得的)

p + coord_cartesian(clip = "off", 
                    ylim = layer_scales(p)$y$range$range, 
                    xlim = layer_scales(p)$x$range$range)

在此处输入图像描述

或者,将此问题与上一个问题结合起来,在绘图面板之外添加文本标签,结果可能类似于:

p + coord_cartesian(clip = "off", 
                    ylim = layer_scales(p)$y$range$range, 
                    xlim = layer_scales(p)$x$range$range) +
  geom_text(data = data.frame(value = c(0, 6), id = c("df2", "df2"),
                              Sex = c('Female', 'Male')),
            aes(y = -0.15, label = c('Female', 'Male')))

在此处输入图像描述

Yes, to get the scales directly, use layer_scales(p), which gives you the range of the axes rather than just the range of the data, which is what you get from layer_data(p)

p + coord_cartesian(clip = "off", 
                    ylim = layer_scales(p)$y$range$range, 
                    xlim = layer_scales(p)$x$range$range)

enter image description here

Or, to combine this question with your last, where you add the text labels outside of the plotting panels, your result might be something like:

p + coord_cartesian(clip = "off", 
                    ylim = layer_scales(p)$y$range$range, 
                    xlim = layer_scales(p)$x$range$range) +
  geom_text(data = data.frame(value = c(0, 6), id = c("df2", "df2"),
                              Sex = c('Female', 'Male')),
            aes(y = -0.15, label = c('Female', 'Male')))

enter image description here

柠栀 2025-01-23 05:55:24

这有帮助吗?

?layer_data
summary(layer_data(p, i = 2))

i 是您要返回的图层,

可以最小化 xmin 并最大化 xmax

Does this help?

?layer_data
summary(layer_data(p, i = 2))

i is the layer you want to return

Can min the xmin and max the xmax etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文