简体   繁体   English

使用facet_grid时,我可以为x和y以外的美学(例如大小)设置自由比例吗?

[英]Can I set free scales for aesthetics other than x and y (e.g. size) when using facet_grid?

facet_grid and facet_wrap have the scales parameter, which as far as I know allows each plot to adjust the scales of the x and/or y axis to the data being plotted. facet_grid和facet_wrap有scale参数,据我所知,每个绘图都允许每个绘图调整x和/或y轴的刻度到绘制的数据。 Since according to the grammar of ggplot x and y are just two among many aesthetics, and there's a scale for each aesthetic, I figured it would be reasonable to have the option of letting each aesthetic be free, but so far I didn't find a way to do it. 因为根据ggplot x和y的语法只是众多美学中的两个,并且每个美学都有一个尺度,我认为让每个美学都是自由的选择是合理的,但到目前为止我没有找到一种方法。

I was trying to set it in particular for the Size, since sometimes a variables lives in a different order of magnitude depending on the group I'm using for the facet, and having the same scale for every group blocks the possibility of seeing within-group variation. 我试图特别为尺寸设置它,因为有时变量会以不同的数量级生存,具体取决于我用于构面的组,并且每个组具有相同的比例阻止了在内部看到的可能性 - 小组变异。

A reproducible example: 一个可重复的例子:

set.seed(1)
x <- runif(20,0,1)
y <- runif(20,0,1)
groups <- c(rep('small', 10), rep('big', 10))
size_small <- runif(10,0,1)
size_big   <- runif(10,0,1) * 1000

df <- data.frame(x, y, groups, sizes = c(size_small, size_big))

And an auxiliary function for plotting: 以及绘图的辅助功能:

basic_plot <- function(df) ggplot(df) + 
  geom_point(aes(x, y, size = sizes, color = groups)) + 
  scale_color_manual(values = c('big' = 'red', 'small' = 'blue')) +
  coord_cartesian(xlim=c(0,1), ylim=c(0,1))

If I we plot the data as is, we get the following: 如果我按原样绘制数据,我们得到以下结果:

basic_plot(df)

Non faceted plot 非刻面情节

The blue dots are relatively small, but there is nothing we can do. 蓝点相对较小,但我们无能为力。 If we add the facet: 如果我们添加facet:

basic_plot(df) +
  facet_grid(~groups, scales = 'free')

Faceted plot 多面的情节

The blue dots continue being small. 蓝点继续很小。 But I would like to take advantage of the fact that I'm dividing the data in two, and allow the size scale to adjust to the data of each plot. 但我想利用这样一个事实:我将数据分成两部分,并允许尺寸比例调整到每个图的数据。 I would like to have something like the following: 我希望得到以下内容:

plot_big <- basic_plot(df[df$groups == 'big',])
plot_small <- basic_plot(df[df$groups == 'small',])

grid.arrange(plot_big, plot_small, ncol = 2)

What I want 我想要的是

Can it be done without resorting to this kind of micromanaging, or a manual rescaling of the sizes like the following? 是否可以在不采用这种微观管理或手动重新缩放尺寸的情况下完成,如下所示?

df %>% 
  group_by(groups) %>% 
  mutate(maximo = max(sizes),
         sizes = scale(sizes, center = F)) %>% 
  basic_plot() +
  facet_grid(~groups)

I can manage to do those things, I'm just trying to see if I'm not missing another option, or if I'm misunderstanding the grammar of graphics. 我可以设法做那些事情,我只是想看看我是不是错过了另一种选择,或者我是否误解了图形的语法。

Thank you for your time! 感谢您的时间!

As mentioned, original plot aesthetics are maintained when calling facet_wrap . 如上所述,调用facet_wrap时保持原始的情节美学。 Since you need grouped graphs, consider base::by (the subsetting data frame function) wrapped in do.call : 由于您需要分组图,请考虑包含在do.call base::by (子集化数据框函数):

do.call(grid.arrange, 
        args=list(grobs=by(df, df$groups, basic_plot), 
                  ncol=2,
                  top="Grouped Point Plots"))

分组绘图输出


Should you need to share a legend, I always use this wrapper from @Steven Lockton's answer 如果你需要分享一个传奇,我总是使用@Steven Lockton答案中的这个包装器

do.call(grid_arrange_shared_legend, by(df, df$groups, basic_plot))

共享图例绘图输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM