简体   繁体   English

如何在ggplot2中的汇总数据上创建小提琴图?

[英]How to create a violin plot in ggplot2 on aggregated data?

I'm trying to create a violin plot in ggplot2 on aggregated data which consists of pre-calculated counts (the raw data is too big and takes really long to plot). 我正在尝试在ggplot2上的聚合数据上创建一个小提琴图,该数据由预先计算的计数组成(原始数据太大,需要花费很长时间进行绘制)。 Example dataset below. 下面的示例数据集。

data <- data.frame(category = rep(LETTERS[1:3],3),
               value = c(1,1,1,2,2,2,3,3,3),
               count = c(3,2,1,1,2,3,2,1,3))

The solution found here Violin Plot (geom_violin) with aggregated values doesn't seem to work properly, giving inaccurate densities. 在此处找到带有汇总值的小提琴图(geom_violin)的解决方案似乎无法正常工作,从而导致密度不准确。 When I plot using that method and compare against plotting with the entire dataset, the plots looks very different. 当我使用该方法进行绘制并与整个数据集进行比较时,这些绘制看起来非常不同。

Anyone know how to plot on aggregated data? 有人知道如何绘制汇总数据吗?

Any difference you're seeing might be caused simply by the difference in how the density is being calculated. 您看到的任何差异可能仅是由于密度计算方式上的差异引起的。 Here is the example you gave: 这是您提供的示例:

data <- data.table(category = rep(LETTERS[1:3],3),
                   value = c(1,1,1,2,2,2,3,3,3),
                   count = c(3,2,1,1,2,3,2,1,3))
data[, count2 := count/sum(count), by = category]
ggplot(data, aes(x = category, y = value, weight = count2)) + geom_violin()

在此处输入图片说明

Now here is the same data with the values repeated, rather than counts: 现在,这里是重复的值而不是计数的相同数据:

val2 <- unlist(sapply(1:length(data$value), 
                      function(x) rep(data$value[x], 
                                      data$count[x])))
cat2 <- unlist(sapply(1:length(data$value), 
                      function(x) rep(data$category[x], 
                                      data$count[x])))

dat2 <- data.table(cat2, val2)
ggplot(dat2, aes(x = cat2, y = val2)) + geom_violin()

This generates the plot: 生成图: 在此处输入图片说明

Not a lot of difference, and likely caused by how the density is being calculated. 差异不大,很可能是由密度的计算方式引起的。

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

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