【问题标题】:Remove space between plotted data and the axes删除绘制数据和轴之间的空间
【发布时间】:2014-04-08 18:44:39
【问题描述】:

我有以下数据框:

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"

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

【问题讨论】:

  • 我认为你需要为unit显式加载网格包
  • @TylerRinker 谢谢!我不再收到错误消息。我已经用另一种方式解决了这个问题,但是当我想设置边距时,这是一个很好的提醒。
  • 这有时被称为紧凑布局。在这里提到这一点,以便搜索引擎可以找到它。

标签: r plot ggplot2


【解决方案1】:

更新:请参阅@divibisan's answer,了解最新版本的 的更多可能性。


来自?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())

结果:

【讨论】:

    【解决方案2】:

    ggplot2 version 3 开始,您可以将expand_scale() 函数传递给expand= 参数,让您可以为比例尺的每一侧指定不同的展开值。

    ggplot2 version 3.3.0 开始,expand_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()
    


    由于这是我投票最多的答案,我想我会扩展它以更好地说明add=mult= 之间的区别。这两个选项都将绘图区域扩展到数据之外的特定量。使用add,将区域扩展一个绝对量(以该轴使用的单位),而mult 将该区域扩展该轴总大小的指定比例。

    在下面的示例中,我使用add=10 扩展了底部,它将绘图区域向下扩展了 10 个单位,直到 -10。我使用mult=.15 扩展顶部,它延伸到绘图区域的顶部,占 y 轴上数据总大小的 15%。由于数据从 0 到 100,即 0.15 * 100 = 15 个单位 - 所以它扩展到 115。

    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),
                           breaks = seq(-10, 115, by=15),
                           expand = expansion(mult = c(0, .15),
                                              add = c(10, 0))) +
        theme_bw()
    

    【讨论】:

    • 有什么办法可以去掉主题中的扩展?我希望能够让它默认不存在(也许它需要是一个自定义的scale_* 函数)
    • @MokeEire 我不知道。我认为您的想法是最好的:您可以定义一个自定义函数,该函数是具有此默认设置的比例。 sxc &lt;- function(...) scale_x_continuous(expand = expansion(add=0), ...)。然后你可以像使用scale_x_continuous 一样使用sxc,甚至传入其他参数,但使用默认设置。
    【解决方案3】:

    另一个产生相同结果的选项是使用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())
    

    【讨论】:

    • 不错的选择,+1。但是,如果您想指定中断等,您仍然需要 scales_x/y_continuous 部分。
    • 或者,更简单的是,按照@Marcus comment in this question 中的建议在coord_cartesian 中设置expand = FALSE
    • @Tjebo 输出略有不同,但实际上它可能更接近 OP 的预期结果。我稍后会将其添加到我的答案中。谢谢。
    猜你喜欢
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 2018-03-04
    • 1970-01-01
    • 2023-03-19
    • 2014-02-04
    • 1970-01-01
    相关资源
    最近更新 更多