ggplot 2 r 上与 x 轴成比例的不同大小的刻面 [英] different size facets proportional of x axis on ggplot 2 r

查看:25
本文介绍了ggplot 2 r 上与 x 轴成比例的不同大小的刻面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一种情况:

group1 <- seq(1, 10, 2)
group2 <-  seq(1, 20, 3)
x = c(group1, group2)
mydf <- data.frame (X =x , Y = rnorm (length (x),5,1), 
 groups = c(rep(1, length (group1)), rep(2, length(group2))))

ggplot(mydf, aes(X, Y, group= groups)) + geom_point()+ facet_grid (.~ group)

不同方面在下图中按 x 限制进行缩放:

Different facets are scaled by x limits in the following plot:

 ggplot(mydf, aes(X, Y, group= groups)) + geom_point()+ 
   facet_grid (.~ group, scales = "free_x")

由于 x 的总宽度有意义,我想生成不同宽度的刻面,而不仅仅是不同的比例.因此预期的 facet 1 的宽度应该是 2 大小的一半.

As total width of x has meaning, I want to produce facets of different width not only different scale. Thus the expected facet 1's wideth should be half the size of 2.

推荐答案

如果我理解正确的话,space = "free_x"facet_grid 中做了你想要的.据我所知,facet_wrap 从未支持空格参数,但许多 facet_wrap 命令可以转换为 facet_grid 命令.

If I understand you correctly, space = "free_x" does what you want in facet_grid. As far as I know, facet_wrap has never supported a space argument, but many facet_wrap commands can be cast as facet_grid commands.

library(ggplot2)

ggplot(mydf, aes(X, Y)) + geom_point()+ 
facet_grid (.~ groups, scales = "free_x", space = "free_x")

如果您想在 x 轴上使用相同的标签样式:

And if you want the same style of labelling on the x axes:

ggplot(mydf, aes(X, Y)) + geom_point()+ 
 scale_x_continuous(breaks = seq(0,20,2)) +
 facet_grid (.~ groups, scales = "free_x", space = "free_x")

这篇关于ggplot 2 r 上与 x 轴成比例的不同大小的刻面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆