如何删除ggplot2中绘图区域和面板之间的边距? [英] How to remove the margin between plot region and panel in ggplot2?

查看:1453
本文介绍了如何删除ggplot2中绘图区域和面板之间的边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是重现我的问题的最简单的例子



  library(ggplot2)
library(grid)
df< - expand.grid(list(x = seq( 1),y = seq(1,10),z = seq(1,2)))

p < - ggplot(df)+ geom_tile(aes(x,y))+ facet_wrap(〜z)

p < - p + theme_minimal()+ xlab('')+ ylab('')
p < - p + theme(axis.text = element_blank ),
panel.grid = element_blank(),
axis.ticks = element_blank(),
panel.border = element_rect(color ='black',fill ='transparent'),
panel.margin =单位(0,'mm'))
p + ylim(2,6)+ xlim(2,6)

这是我的代码的结果。




如何去掉上图中的所有白色区域?感谢您的任何建议。

解决方案

(好的,这里是我的评论作为答案..)

只需将以下内容添加到图中:

  + scale_y_continuous(expand = c(0,0) )+ scale_x_continuous(expand = c(0,0))


I am creating some maps and want to remove all margins between the plot region and panel border.

This is the minimal example to reproduce my question

library(ggplot2)
library(grid)
df <- expand.grid(list(x = seq(1, 10), y = seq(1, 10), z = seq(1, 2)))

p <- ggplot(df) + geom_tile(aes(x, y)) + facet_wrap(~z)

p <- p + theme_minimal() + xlab('') + ylab('')
p <- p + theme(axis.text = element_blank(),
    panel.grid = element_blank(),
    axis.ticks = element_blank(),
    panel.border = element_rect(colour = 'black', fill = 'transparent'),
    panel.margin = unit(0, 'mm'))
p + ylim(2, 6) + xlim(2, 6)

This is the result of my codes.

How could I remove all white areas in the figure above? Thanks for any suggestions.

解决方案

(Alright, here's my comment as an answer..)

Just add the following to the plot:

+ scale_y_continuous(expand = c(0,0)) + scale_x_continuous(expand = c(0,0))

这篇关于如何删除ggplot2中绘图区域和面板之间的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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