在ggplot2中绘制多行+带状图

时间:2016-10-31 14:04:54

标签: r ggplot2

我的数据集如下所示

package com.rahulweb.spring.springtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestModelApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestModelApplication.class, args);
    }
}

我想为sample_data <- structure(list(chrom = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("chr1", "chr2", "chr3", "chr4"), class = "factor"), sample = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("sample_1", "sample_2"), class = "factor"), average = c(358.017, 34.452, 409.7959, 117.0805, 345.6717, 34.3544, 362.3519, 110.7264), std_dev = c(1484.33280699, 97.332895241, 1460.24099656, 519.299214731, 1439.86318396, 114.04659662, 1340.67100158, 499.901605662)), .Names = c("chrom", "sample", "average", "std_dev"), row.names = c(1L, 2L, 3L, 4L, 25L, 26L, 27L, 28L), class = "data.frame") > sample_data chrom sample average std_dev 1 chr1 sample_1 358.0170 1484.3328 2 chr2 sample_1 34.4520 97.3329 3 chr3 sample_1 409.7959 1460.2410 4 chr4 sample_1 117.0805 519.2992 25 chr1 sample_2 345.6717 1439.8632 26 chr2 sample_2 34.3544 114.0466 27 chr3 sample_2 362.3519 1340.6710 28 chr4 sample_2 110.7264 499.9016 sample(y)每average(x)制作一个线图,其中一个标签显示标准差(chrom)范围。所以我提出了这个代码:

std_dev

但是,我一直收到这个错误:

sample_ribbon <- ggplot(sample_data, x=chrom, y=average, group=sample)
sample_ribbon <- sample_ribbon + geom_line()
sample_ribbon <- sample_ribbon + geom_ribbon(aes(x=chrom, ymin=average - std_dev, ymax=average + std_dev), alpha = 0.3)
print(sample_ribbon)

知道这有什么问题吗?我已经尝试了多种方法来绘制这个,他们都回来了这个错误。

1 个答案:

答案 0 :(得分:1)

您需要更改ggplot的第一行以包含aes,它将起作用:

sample_ribbon <- ggplot(sample_data, aes(x=chrom, y=average, group=sample))

其余的都很好。

 sample_ribbon <- sample_ribbon + geom_line()
 sample_ribbon <- sample_ribbon + geom_ribbon(aes(x=chrom, ymin=average - std_dev, ymax=average + std_dev), alpha = 0.3)
 print(sample_ribbon)

enter image description here