【问题标题】:Line at the top of a ridgeline density plot is cut off山脊线密度图顶部的线被截断
【发布时间】:2018-04-27 05:24:48
【问题描述】:

为什么情节的顶部被切断了,我该如何解决这个问题?我增加了边距,它没有任何区别。

查看 1854 年的曲线,位于左侧驼峰的最顶部。看起来驼峰顶部的线条更细。对我来说,将大小更改为 0.8 并没有帮助。

这是生成此示例所需的代码:

library(tidyverse)
library(ggridges)

t2 <-   structure(list(Date = c("1853-01", "1853-02", "1853-03", "1853-04", 
                                "1853-05", "1853-06", "1853-07", "1853-08", "1853-09", "1853-10", 
                                "1853-11", "1853-12", "1854-01", "1854-02", "1854-03", "1854-04", 
                                "1854-05", "1854-06", "1854-07", "1854-08", "1854-09", "1854-10", 
                                "1854-11", "1854-12"), t = c(-5.6, -5.3, -1.5, 4.9, 9.8, 17.9, 
                                                             18.5, 19.9, 14.8, 6.2, 3.1, -4.3, -5.9, -7, -1.3, 4.1, 10, 16.8, 
                                                             22, 20, 16.1, 10.1, 1.8, -5.6), year = c("1853", "1853", "1853", 
                                                                                                      "1853", "1853", "1853", "1853", "1853", "1853", "1853", "1853", 
                                                                                                      "1853", "1854", "1854", "1854", "1854", "1854", "1854", "1854", 
                                                                                                      "1854", "1854", "1854", "1854", "1854")), row.names = c(NA, -24L
                                                                                                      ), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Date", 
                                                                                                                                                              "t", "year"))

# Density plot -----------------------------------------------
jj <- ggplot(t2, aes(x = t, y = year)) +
  stat_density_ridges(
    geom = "density_ridges_gradient",
    quantile_lines = TRUE,
    size = 1,
    quantiles = 2) +
  theme_ridges() +
  theme(
    plot.margin = margin(t = 1, r = 1, b = 0.5, l = 0.5, "cm") 
  )


# Build ggplot and extract data
d <- ggplot_build(jj)$data[[1]]

# Add geom_ribbon for shaded area
jj +
  geom_ribbon(
    data = transform(subset(d, x >= 20), year = group),
    aes(x, ymin = ymin, ymax = ymax, group = group),
    fill = "red",
    alpha = 0.5) 

【问题讨论】:

  • 很难判断密度的顶部是否被切断,但是一旦尺寸减小到小于1的值,整个图就会显示出来(对我来说)
  • 无法重现,一切正常。
  • 更改大小对我没有帮助。我在原始问题中添加了一张图片。
  • 我想如果我可以在最后一个可能有效的 ggridges 图(1854)上扩展 y 轴。有人能解释一下怎么做吗?
  • 这是用于离散比例的 ggplot2 比例扩展代码的属性。它将比例精确地扩展到密度曲线的最大值点,但不会进一步扩大。这会在最顶部切断一半的线。

标签: r ggplot2 ggridges ridgeline-plot


【解决方案1】:

一些评论者说他们无法重现这个问题,但它确实存在。如果我们增加线条大小,更容易看出:

library(ggridges)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Species)) + 
  geom_density_ridges(size = 2)

这是 ggplot 如何扩展离散尺度的属性。密度线超出了 ggplot 使用的正常加性扩展值(其大小是从“setosa”基线到 x 轴的距离)。在这种情况下,ggplot 会进一步扩展轴,但只能精确到最大数据点。因此,线的一半在该最大点处超出了绘图区域,而那一半被截断。

即将推出的 ggplot2 2.3.0(目前可通过 github 获得)将有两种新方法来处理这个问题。首先,可以在坐标系中设置clip = "off",让线条延伸到绘图范围之外:

ggplot(iris, aes(x = Sepal.Length, y = Species)) + 
  geom_density_ridges(size = 2) +
  coord_cartesian(clip = "off")

其次,您可以分别扩展刻度的底部和顶部。对于离散尺度,我更喜欢加法扩展,我认为在这种情况下,我们想让下限值小于默认值,但上限值要大一些:

ggplot(iris, aes(x = Sepal.Length, y = Species)) + 
  geom_density_ridges(size = 2) +
  scale_y_discrete(expand = expand_scale(add = c(0.2, 1.5)))

【讨论】:

    【解决方案2】:

    添加

    scale_y_discrete(expand = c(0.01, 0))

    成功了。

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 1970-01-01
      • 2020-06-01
      • 2015-12-09
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 2017-02-11
      • 2017-12-20
      相关资源
      最近更新 更多