填写geom_violin图中的特定区域

时间:2016-03-24 14:55:31

标签: r ggplot2

如何根据固定截止值以不同颜色填充 ggplot2 中的geom_violin图?

例如,给定设置:

library(ggplot2)

set.seed(123)
dat <- data.frame(x = rep(1:3,each = 100),
                  y = c(rnorm(100,-1),rnorm(100,0),rnorm(100,1)))
dat$f <- with(dat,ifelse(y >= 0,'Above','Below'))

我想采取这个基本情节:

ggplot() + 
    geom_violin(data = dat,aes(x = factor(x),y = y))

并且简单地让每个小提琴在零上下都有不同的颜色。尝试的天真的东西,映射fill美学,分裂和闪避小提琴的情节:

ggplot() + 
    geom_violin(data = dat,aes(x = factor(x),y = y, fill = f))

这不是我想要的。我想在每个x值的单个小提琴情节,但内部充满了零上下的不同颜色。

1 个答案:

答案 0 :(得分:7)

这是实现此目的的一种方法。

library(ggplot2)
library(plyr)

#Data setup
set.seed(123)
dat <- data.frame(x = rep(1:3,each = 100),
                  y = c(rnorm(100,-1),rnorm(100,0),rnorm(100,1)))

首先我们将使用ggplot::ggplot_build来捕捉绘制小提琴情节的所有计算变量:

p <- ggplot() + 
    geom_violin(data = dat,aes(x = factor(x),y = y))
p_build <- ggplot2::ggplot_build(p)$data[[1]]

接下来,如果我们看一下geom_violin geom_polygon,我们会看到它在将此计算数据帧传递给#This comes directly from the source of geom_violin p_build <- transform(p_build, xminv = x - violinwidth * (x - xmin), xmaxv = x + violinwidth * (xmax - x)) p_build <- rbind(plyr::arrange(transform(p_build, x = xminv), y), plyr::arrange(transform(p_build, x = xmaxv), -y)) 以绘制实际数据之前会对其进行一些特定的转换。小提琴区的轮廓。

因此我们将模仿该过程并简单地手动绘制填充的多边形:

#Add our fill variable
p_build$fill_group <- ifelse(p_build$y >= 0,'Above','Below')
#This is necessary to ensure that instead of trying to draw
# 3 polygons, we're telling ggplot to draw six polygons
p_build$group1 <- with(p_build,interaction(factor(group),factor(fill_group)))

我在源代码中省略了一个关于复制第一行的小细节,以确保关闭多边形。

现在我们做两个最后的修改:

#Note the use of the group aesthetic here with our computed version,
# group1
p_fill <- ggplot() + 
    geom_polygon(data = p_build,
                 aes(x = x,y = y,group = group1,fill = fill_group))
p_fill

最后的情节:

<allow-intent>

source code

请注意,一般情况下,这将破坏对任何分类x轴标签的良好处理。因此,您经常需要使用连续的x轴进行绘图,然后如果需要分类标签,请手动添加。

相关问题