请问在gggplot 2中如何去除轴与面积之间的空间?

在gggplot 2中如何去除轴与面积之间的空间?

我有以下数据:

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, 199
7L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 198
7L, 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,

当我用:

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"

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


蛊毒传说
浏览 514回答 3
3回答

一只萌萌小番薯

这个问题通过增加expand=c(0,0)到scale_x_continuous和scale_y_continuous..这也消除了添加panel.margin参数。守则:ggplot(data&nbsp;=&nbsp;uniq)&nbsp;+&nbsp; &nbsp;&nbsp;geom_area(aes(x&nbsp;=&nbsp;year,&nbsp;y&nbsp;=&nbsp;uniq.p,&nbsp;fill&nbsp;=&nbsp;uniq.loc),&nbsp;stat&nbsp;=&nbsp;"identity",&nbsp;position&nbsp;=&nbsp;"stack")&nbsp;+ &nbsp;&nbsp;scale_x_continuous(limits&nbsp;=&nbsp;c(1986,2014),&nbsp;expand&nbsp;=&nbsp;c(0,&nbsp;0))&nbsp;+ &nbsp;&nbsp;scale_y_continuous(limits&nbsp;=&nbsp;c(0,101),&nbsp;expand&nbsp;=&nbsp;c(0,&nbsp;0))&nbsp;+ &nbsp;&nbsp;theme_bw()&nbsp;+&nbsp;theme(panel.grid&nbsp;=&nbsp;element_blank(),&nbsp;panel.border&nbsp;=&nbsp;element_blank())

MMMHUHU

截至ggplot2 version 3,有一个expand_scale()函数,您可以将该函数传递给expand=参数,该参数允许为缩放的每一侧指定不同的展开值。它还允许您选择是否希望展开成为绝对大小(使用add=参数)或绘图大小的百分比(使用mult=参数):ggplot(data&nbsp;=&nbsp;uniq)&nbsp;+&nbsp; &nbsp;&nbsp;geom_area(aes(x&nbsp;=&nbsp;year,&nbsp;y&nbsp;=&nbsp;uniq.p,&nbsp;fill&nbsp;=&nbsp;uniq.loc),&nbsp;stat&nbsp;=&nbsp;"identity",&nbsp;position&nbsp;=&nbsp;"stack")&nbsp;+ &nbsp;&nbsp;scale_x_continuous(limits&nbsp;=&nbsp;c(1986,2014),&nbsp;expand&nbsp;=&nbsp;c(0,&nbsp;0))&nbsp;+ &nbsp;&nbsp;scale_y_continuous(limits&nbsp;=&nbsp;c(0,101),&nbsp;expand&nbsp;=&nbsp;expand_scale(mult&nbsp;=&nbsp;c(0,&nbsp;.1)))&nbsp;+ &nbsp;&nbsp;theme_bw()

倚天杖

另一个产生相同结果的选项是使用coord_cartesian而不是连续的位置标度(x&y):ggplot(data&nbsp;=&nbsp;uniq)&nbsp;+&nbsp;&nbsp; &nbsp;&nbsp;geom_area(aes(x&nbsp;=&nbsp;year,&nbsp;y&nbsp;=&nbsp;uniq.p,&nbsp;fill&nbsp;=&nbsp;uniq.loc),&nbsp;stat&nbsp;=&nbsp;"identity",&nbsp;position&nbsp;=&nbsp;"stack")&nbsp;+&nbsp;&nbsp; &nbsp;&nbsp;coord_cartesian(xlim&nbsp;=&nbsp;c(1986,2014),&nbsp;ylim&nbsp;=&nbsp;c(0,101))+ &nbsp;&nbsp;theme_bw()&nbsp;+&nbsp;theme(panel.grid=element_blank(),&nbsp;panel.border=element_blank())
打开App,查看更多内容
随时随地看视频慕课网APP