简体   繁体   English

将具有密度的 y 轴添加到脊线图

[英]Add a y-axis with density to ridgeline plot

I struggle to add a y-axis to my ridgeline plot which depicts the density.我很难在描述密度的山脊线图中添加 y 轴。 If I got it right a ridgeline plot is a kind of density plot.如果我猜对了,山脊线图是一种密度图。 I used it in instead of a boxplot and also included quantile lines.我用它代替箱线图,还包括分位数线。 I like it in the way it is, however my supervisor doesn't see the benefits compared to a boxplot and always wanted me to include a y-axis with the density.我喜欢它的样子,但是我的主管没有看到与箱线图相比的好处,并且一直希望我包含一个 y 轴和密度。 Is it possible to add it or do I have to use density plots and do I have the possibility to arrange them as beautiful as geom_density_ridges does?是否可以添加它,或者我是否必须使用密度图,我是否有可能像geom_density_ridges那样将它们排列得漂亮? Here one of my plots:这是我的情节之一: 在此处输入图片说明 Here a reproduceable example:这是一个可重现的示例:

ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
  geom_density_ridges(scale = 0.9) +
  theme_ridges() + 
  theme(legend.position = "none")

Thanks a lot!非常感谢!

One option would be to make you plot via facet_grid or _wrap plus some additional styling like getting rid of strip texts and the spacing between panels:一种选择是让您通过facet_grid_wrap以及一些额外的样式进行绘图,例如摆脱条形文本和面板之间的间距:

library(ggplot2)
library(ggridges)

ggplot(diamonds, aes(x = price, fill = cut)) +
  geom_density() +
  scale_y_continuous(expand = expansion(mult = c(0, .2))) +
  facet_grid(cut~., switch = "y") +
  xlim(-1000, NA) +
  theme_ridges() + 
  theme(legend.position = "none", 
        strip.text.x = element_blank(),
        strip.text.y.left = element_text(angle = 0, vjust = 0.01),
        strip.background.y = element_rect(fill = NA),
        strip.placement = "outside",
        panel.grid.major.y = element_blank(),
        panel.spacing.y = unit(0, "pt"))

I'm just going to go ahead and say this is an awful and hacky solution, but I've already spend the time so why the hell not post it?我只是要继续说这是一个糟糕而笨拙的解决方案,但我已经花时间了,那么为什么不发布呢? Who knows;谁知道; such hacks might be convenient for someone else.这样的黑客可能对其他人很方便。

The plan is: we're semi-manually going to replicate what ggridges does, but in vanilla ggplot2 and on a continuous scale.计划是:我们将半手动地复制 ggridges 所做的事情,但是在 vanilla ggplot2 中并且以连续的规模进行。 To do this, we'll be abusing the after_stat() function to (1) fill a ribbon with the densities and (2) superassign ourselves the maximum density, which we'll use as an offset.为此,我们将滥用after_stat()函数来 (1) 用密度填充色带和 (2) 为我们自己超级分配最大密度,我们将其用作偏移量。

library(ggplot2)

g <- ggplot(diamonds, aes(x = price, fill = cut)) +
  stat_density(
    geom = "ribbon", position = "identity",
    aes(ymax = after_stat(density + (group - 1) * max(density)),
        ymin = after_stat((group - 1) * {yoffset <<- max(density)}))
  )

The flaw in this whole plan is that we don't know the offset in advance, so we'd need to build the plot at least once before we can actually use it.整个计划的缺陷是我们事先不知道偏移量,因此我们需要至少构建一次图才能实际使用它。

build <- ggplot_build(g)

Lastly, we'll very manually assign the right values on the scales.最后,我们将非常手动地在比例尺上分配正确的值。 We'll be using the primary for the discrete labels and the secondary for the continuous labels.我们将对离散标签使用主要标签,对连续标签使用次要标签。 You can look at the yoffset value that now should be in your global environment to get a sense of what the right breaks and labels are.您可以查看现在应该在您的全局环境中的yoffset值,以了解正确的中断和标签是什么。

g + scale_y_continuous(
    breaks = yoffset * (4:0), labels = levels(diamonds$cut),
    sec.axis = dup_axis(
      breaks = rep(yoffset * (0:4), 4) + rep(seq(0, 0.0003, by = 0.0001), each = 5),
      labels = rep(seq(0, 0.0003, by = 0.0001), each = 5)
    )
  )

Created on 2021-07-15 by the reprex package (v1.0.0)reprex 包(v1.0.0) 于 2021 年 7 月 15 日创建

A slightly more generalisable way of setting the breaks/labels:一种更通用的设置中断/标签的方法:

levels <- levels(diamonds$cut)
labels <- scales::extended_breaks(3)(c(0, yoffset * 0.8))
offsets <- yoffset * (seq(length(levels), 1) - 1)

g + scale_y_continuous(
    breaks = offsets, labels = levels,
    sec.axis = dup_axis(
      breaks = rep(offsets, length(labels)) + rep(labels, each = length(offsets)),
      labels = rep(labels, each = length(offsets))
    )
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM