7

我希望只有一半的小提琴图(类似于 ggridges 的 stat_density_ridges 创建的图)。一个 MWE

library(ggplot2)

dframe = data.frame(val = c(), group = c())
for(i in 1:5){
  offset = i - 3
  dframe = rbind(dframe, 
                 data.frame(val = rnorm(n = 50, mean = 0 - offset), group = i)
                 )
}
dframe$group = as.factor(dframe$group)

ggplot(data = dframe, aes(x = group, y = val)) + 
  geom_violin()      

产生这样的情节

在此处输入图像描述

我虽然希望有一个看起来像这样:

在此处输入图像描述

理想情况下,绘图也将被缩放到宽度的 1.5 到 2 倍。

4

2 回答 2

10

@David Robinson有一个巧妙的解决方案(原始代码来自他 的要点,我只做了一些修改)。

GeomFlatViolin他根据小提琴图的宽度变化创建了新层 ( ):

data <- transform(data, 
                  xmaxv = x,
                  xminv = x + violinwidth * (xmin - x))

这一层也有width论据。


例子:

# Using OPs data
# Get wanted width with: geom_flat_violin(width = 1.5)
ggplot(dframe, aes(group, val)) +
    geom_flat_violin()

在此处输入图像描述

代码:

library(ggplot2)
library(dplyr)


"%||%" <- function(a, b) {
  if (!is.null(a)) a else b
}

geom_flat_violin <- function(mapping = NULL, data = NULL, stat = "ydensity",
                        position = "dodge", trim = TRUE, scale = "area",
                        show.legend = NA, inherit.aes = TRUE, ...) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomFlatViolin,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      trim = trim,
      scale = scale,
      ...
    )
  )
}

GeomFlatViolin <-
  ggproto("GeomFlatViolin", Geom,
          setup_data = function(data, params) {
            data$width <- data$width %||%
              params$width %||% (resolution(data$x, FALSE) * 0.9)

            # ymin, ymax, xmin, and xmax define the bounding rectangle for each group
            data %>%
              group_by(group) %>%
              mutate(ymin = min(y),
                     ymax = max(y),
                     xmin = x - width / 2,
                     xmax = x)
          },

          draw_group = function(data, panel_scales, coord) {
            # Find the points for the line to go all the way around
            data <- transform(data, 
                              xmaxv = x,
                              xminv = x + violinwidth * (xmin - x))

            # Make sure it's sorted properly to draw the outline
            newdata <- rbind(plyr::arrange(transform(data, x = xminv), y),
                             plyr::arrange(transform(data, x = xmaxv), -y))

            # Close the polygon: set first and last point the same
            # Needed for coord_polar and such
            newdata <- rbind(newdata, newdata[1,])

            ggplot2:::ggname("geom_flat_violin", GeomPolygon$draw_panel(newdata, panel_scales, coord))
          },

          draw_key = draw_key_polygon,

          default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,
                            alpha = NA, linetype = "solid"),

          required_aes = c("x", "y")
)
于 2018-08-27T08:14:44.987 回答
2

see还有一个功能geom_violinhalf似乎完全符合您的要求(请参见下面的右图)。它的行为主要类似于geom_violin(),除了没有geom_violin()具有的所有参数(例如缺少draw_quantiles

library(ggplot2)
library(see)


p <- ggplot(mtcars, aes(factor(cyl), mpg))
p1 <- p + geom_violin()+ ggtitle("geom_violin")
p2 <- p + see::geom_violinhalf()+ ggtitle("see::geom_violinhalf")

## show them next to each other
library(patchwork)
p1+p2

reprex 包(v0.3.0)于 2020-04-30 创建

于 2020-04-30T18:59:23.617 回答