ggplot2& facet_wrap - 消除刻面之间的垂直距离 [英] ggplot2 & facet_wrap - eliminate vertical distance between facets

查看:391
本文介绍了ggplot2& facet_wrap - 消除刻面之间的垂直距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些我想要显示为nxn地块的数据。 编辑:要更清楚一点,我的数据中有21个类别。我想要按类别分面,并将这21个地块放在一个5 x 5的正方形网格中(孤儿本身就在第五排)。因此facet_wrap而不是facet_grid。



我已经准备了下面的代码来完成它(使用我的可重现示例中良好的旧的iris数据集):

  library(ggplot2)
library(grid)

cust_theme< - theme_bw()+ theme (legend.position =none,
axis.title = element_blank(),axis.ticks = element_blank(),
axis.text = element_blank(),strip.text = element_blank(),
strip.background = element_blank(),panel.margin = unit(0,lines),
panel.border = element_rect(size = 0.25,color =black),
panel .grid = element_blank())

iris.plot< - ggplot(data = iris,aes(x = Sepal.Length,y = Sepal.Width,color = Species))+
geom_point()+ cust_theme + facet_wrap(〜Species,ncol = 2)+
labs(title =Irises by species)

这让我几乎想要什么,但没有t完全相同:



我还是在第一排地块和第二排地块之间留下了一小块空间。我想完全摆脱这一点,但panel.margin显然没有这样做。有没有办法做到这一点?

解决方案

更改 panel.margin 参数为 panel.margin = unit(c(-0.5,0-0.5,0),lines)。出于某种原因,顶部和底部边距需要为负才能完美排列。结果如下:




I'm working with some data that I want to display as a nxn grid of plots. Edit: To be more clear, there's 21 categories in my data. I want to facet by category, and have those 21 plots in a 5 x 5 square grid (where the orphan is by itself on the fifth row). Thus facet_wrap instead of facet_grid.

I've got the following code written up for doing it (using the good old iris data set for my reproducible example):

library(ggplot2)
library(grid)

cust_theme <- theme_bw() + theme(legend.position="none", 
              axis.title = element_blank(), axis.ticks = element_blank(), 
              axis.text = element_blank(), strip.text = element_blank(), 
              strip.background = element_blank(), panel.margin = unit(0, "lines"), 
              panel.border = element_rect(size = 0.25, color = "black"), 
              panel.grid = element_blank())

iris.plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
             geom_point() + cust_theme + facet_wrap( ~ Species, ncol = 2) + 
             labs(title = "Irises by species")

This gives me ALMOST what I want, but not quite:

I've still got a tiny strip of space between the top row of plots and the bottom row. I'd like to get rid of that entirely, but panel.margin is obviously not doing it. Is there a way to do this?

解决方案

Change the panel.margin argument to panel.margin = unit(c(-0.5,0-0.5,0), "lines"). For some reason the top and bottom margins need to be negative to line up perfectly. Here is the result:

这篇关于ggplot2&amp; facet_wrap - 消除刻面之间的垂直距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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