我正在使用 facet_wrap 和 ggplot 来创建一个平面图。我的问题是我的刻面总是看起来像很短很宽的矩形,但我希望它们是方形的,这样它们更容易理解。理想情况下,我想指定我想要的列数,并让 ggplot 弄清楚绘图的高度应该是多少,以便所有方面都是方形的。这可能吗?
问问题
4484 次
4 回答
10
也许有aspect.ratio
帮助:
df <- data.frame(x = runif(100, 0,10), y = runif(100, 0, 50), z=gl(4,25))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~ z, ncol = 4) +
theme(aspect.ratio = 1) # try with and without
于 2014-01-22T22:33:07.350 回答
0
g = ggplotGrob(p)
g$respect = TRUE
require(grid)
grid.draw(g)
于 2014-01-22T23:03:35.537 回答
0
我对这个问题的理解与 lukeA 略有不同。我以为你希望地块以 nxm 方式排列,尺寸尽可能接近“正方形”。
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~ z, ncol = sqrt(length(levels(df$z))) )
于 2014-01-22T22:45:21.390 回答
0
使用 coord_fixed(1) 就可以了。似乎我使用的是旧版本的 ggplot 所以 theme(aspect.ratio=1) 对我不起作用。
df <- data.frame(x = runif(100, 0,10), y = runif(100, 0, 50), z=gl(4,25))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~ z, ncol = 4) +
coord_fixed(1)
于 2014-01-23T16:54:58.350 回答