如何删除ggplot2中轴与面积图之间的空间?


113

我有以下数据框:

uniq <- structure(list(year = c(1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L), uniq.loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("u.1", "u.2", "u.3"), class = "factor"), uniq.n = c(1, 1, 1, 2, 5, 4, 2, 16, 16, 10, 15, 14, 8, 12, 20, 11, 17, 30, 17, 21, 22, 19, 34, 44, 56, 11, 0, 0, 3, 3, 7, 17, 12, 21, 18, 10, 12, 9, 7, 11, 25, 14, 11, 17, 12, 24, 59, 17, 36, 50, 59, 12, 0, 0, 0, 1, 4, 6, 3, 3, 9, 3, 4, 2, 5, 2, 12, 6, 8, 8, 3, 2, 9, 5, 20, 7, 10, 8), uniq.p = c(100, 100, 25, 33.3, 31.2, 14.8, 11.8, 40, 37.2, 43.5, 48.4, 56, 40, 48, 35.1, 35.5, 47.2, 54.5, 53.1, 44.7, 24.4, 46.3, 37.8, 43.6, 44.8, 35.5, 0, 0, 75, 50, 43.8, 63, 70.6, 52.5, 41.9, 43.5, 38.7, 36, 35, 44, 43.9, 45.2, 30.6, 30.9, 37.5, 51.1, 65.6, 41.5, 40, 49.5, 47.2, 38.7, 0, 0, 0, 16.7, 25, 22.2, 17.6, 7.5, 20.9, 13, 12.9, 8, 25, 8, 21.1, 19.4, 22.2, 14.5, 9.4, 4.3, 10, 12.2, 22.2, 6.9, 8, 25.8)), .Names = c("year", "uniq.loc", "uniq.n", "uniq.p"), class = "data.frame", row.names = c(NA, -78L))

当我用以下方法绘制区域图时:

ggplot(data = uniq) + 
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
  scale_x_continuous(limits=c(1986,2014)) +
  scale_y_continuous(limits=c(0,101)) +
  theme_bw()

我得到这个结果:

在此处输入图片说明

但是,我想删除轴与实际图之间的空间。当我添加时theme(panel.grid = element_blank(), panel.margin = unit(-0.8, "lines")),出现以下错误消息:

Error in theme(panel.grid = element_blank(), panel.margin = unit(-0.8,  : 
  could not find function "unit"

关于如何解决这个问题有什么建议吗?


3
我认为您需要为unit
Tyler Rinker

@TylerRinker Thanx!我不再收到错误消息。我已经用另一种方法解决了这个问题,但是当我要设置边距时,这是对未来的一个很好的提醒。
Jaap 2014年

3
有时将其称为紧凑布局。在此提及此内容,以便搜索引擎可以将其提取。
匿名

Answers:


191

更新:请参阅@divibisan的答案,以获取最新版本的进一步的可能性


?scale_x_continuous有关expand-argument:

范围扩展常数矢量,用于在数据周围添加一些填充,以确保将其放置在距轴一定距离的位置。默认值是将连续变量的比例分别扩大5%,将离散变量的比例分别扩大0.6单位。

问题是这样通过增加解决expand = c(0,0)scale_x_continuousscale_y_continuous。这也消除了添加panel.margin参数的需要。

代码:

ggplot(data = uniq) + 
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
  scale_x_continuous(limits = c(1986,2014), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0,101), expand = c(0, 0)) +
  theme_bw() +
  theme(panel.grid = element_blank(),
        panel.border = element_blank())

结果: 绘制面积图,无间隙


41

从开始ggplot2 version 3,有一个expand_scale()函数可以传递给expand=参数,使您可以为刻度的每一侧指定不同的扩展值。

从开始ggplot2 version 3.3.0expand_scale()不推荐使用,expansion从其他方面起相同的作用。

它还使您可以选择是要扩展为绝对大小(使用add=参数)还是绘制大小的百分比(使用mult=参数):

ggplot(data = uniq) + 
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
  scale_x_continuous(limits = c(1986,2014), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0,101), expand = expansion(mult = c(0, .1))) +
  theme_bw()

在此处输入图片说明


快速说明:expand_scale()已更改为,expansion()但在这种情况下的用法相同。
Molx

1
@Molx谢谢,我已经更新了答案!
divibisan

14

产生相同结果的另一种选择是使用coord_cartesian而不是连续位置刻度(x&y):

ggplot(data = uniq) +  
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +  
  coord_cartesian(xlim = c(1986,2014), ylim = c(0,101))+
  theme_bw() + theme(panel.grid=element_blank(), panel.border=element_blank())

2
不错的选择,+ 1。但是,scales_x/y_continuous如果要指定中断等,仍将需要零件。
Jaap 2015年

或者,更简单,设置expand = FALSEcoord_cartesian所建议的@Marcus 在这个问题上意见
Tjebo

@Tjebo输出略有不同,但实际上可能更接近OP的预期结果。稍后我将其添加到我的答案中。谢谢。
mpalanco
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.