在直方图的 y 轴上绘制变量总和

Plot variable sums on y axis of a histogram

我有这样的数据(来自 DNA 测序):

read.lengths = c(100, 100, 200, 250, 300, 400, 400, 500, 500)

每个长度只是读取中的 DNA 碱基数。给定任意数量的箱,绘制读取长度与计数的直方图很简单。

ggplot(data.frame(read.lengths)) + geom_histogram(aes(x = read.lengths))

但我想做的是将碱基总数放在 y 轴上,而不是读取计数。 IE。对于直方图中的每个 bin,我想要 Y 轴上该 bin 中所有读取长度的总和。

试试这个。

library(ggplot2)
library(dplyr)
read.lengths <- c(100, 100, 200, 250, 300, 400, 400, 500, 500)
read.lengths.cat <- as.factor(read.lengths)

read.lengths.data <- data.frame(read.lengths.cat, read.lengths)

read.lengths.data <- aggregate(read.lengths ~ read.lengths.cat, 
            data = read.lengths.data, sum)

ggplot(aes(x = read.lengths.cat, y = read.lengths), 
       data = read.lengths.data) + 
  geom_bar(stat = "identity")

感谢 让我以正确的方式看待它,我尝试了一些 ggplot 魔法,它似乎工作得很好。

ggplot(data.frame(read.lengths)) + geom_histogram(aes(x = read.lengths, y = (..count..*x))