代码之家  ›  专栏  ›  技术社区  ›  roblanf

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

  •  1
  • roblanf  · 技术社区  · 8 年前

    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轴上,而不是读取次数。一、 e.对于直方图中的每个箱子,我想要Y轴上该箱子中所有读取长度的总和。

    2 回复  |  直到 8 年前
        1
  •  2
  •   JustCurious    8 年前

    试试这个。

    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")
    
        2
  •  0
  •   roblanf    8 年前

    幸亏 this answer

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