在R中使用ggplot2重叠两个图 [英] Overlaying two graphs using ggplot2 in R

查看:3813
本文介绍了在R中使用ggplot2重叠两个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个图表,我试图覆盖另一个:

数据框ge的例子看起来像这样。实际上有10个基因,每个基因有200个样本,因此有2000行和3列:

  Exp基因样本
903.0 1 1
1060.0 1 2
786.0 1 3
736.0 1 4
649.0 2 1
657.0 2 2
733.5 2 3
774.0 2 4

数据框avg的示例如下所示。这是所有样本中每个基因数据点的平均值。实际上这个图有10个基因,所以矩阵是4col X 10行:

  mean Gene sd se 
684.2034 1 102.7142 7.191435
723.2892 2 100.6102 7.044122

第一个图形表示平均表达式的一行对于每个基因以及每个数据点的标准偏差。

  avggraph<  -  ggplot(avg,aes(x = Gene ,y = mean))+ geom_point()+ geom_line()+ geom_errorbar(aes(ymin = mean-sd,ymax = mean + sd),width = .1)
pre>

第二个图形表示所有基因中每个样本的基因表达形式。<​​b>

<$ (ge,aes(x = Gene,y = Expression,group = Samples,color =#000099))+ geom_line()+ scale_x_discrete(limits = flevels .tge)

我想将 avggraph 叠加在 linegraphs 即可。有没有办法做到这一点?我试过avggraph + linegraphs,但是我收到一个错误。我认为这是因为这些图是由两个不同的数据框产生的。



我还应该指出,两个图的坐标轴都是相同的。这两个图表都有X轴上的基因和Y轴上的基因表达。



任何帮助都将不胜感激!


一种方法是将第二个图的 geom_line 命令添加到第一个图中。您需要告诉 ggplot 该数据集基于不同的数据集:

  ggplot(avg,aes(x = Gene,y = mean))+ 
geom_point()+
geom_line()+
geom_errorbar(aes(ymin = mean-sd,ymax = data_ge,aes(x = Gene,y = Exp,group = Sample,color =#000099),
show_guide = FALSE)

最后一个 geom_line 命令用于根据原始数据创建线条。


I have two graphs and I am trying to overlay one on top of the other:

An example of the data frame "ge" looks like this. In actuality there are 10 Genes with 200 samples each, so there are 2000 rows and 3 columns:

Exp    Gene    Sample
903.0   1       1
1060.0  1       2
786.0   1       3
736.0   1       4
649.0   2       1
657.0   2       2
733.5   2       3
774.0   2       4

An example of the data frame "avg" looks like this. This is an average of the data points for each gene across all samples. In actuality this graph has 10 genes, so the matrix is 4col X 10 rows:

mean       Gene   sd         se
684.2034    1   102.7142    7.191435
723.2892    2   100.6102    7.044122

The first graph graphs a line of the average expression for each gene along with the standard deviation for each data point.

avggraph <- ggplot(avg, aes(x=Gene, y=mean)) + geom_point() +geom_line() + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.1)

The second graph graphs the gene expression in the form a line for each sample across all the genes.

linegraphs <- ggplot(ge, aes(x=Gene, y=Expression, group=Samples, colour="#000099")) + geom_line() + scale_x_discrete(limits=flevels.tge)

I would like to superimpose avggraph on top of linegraphs. Is there a way to do this? I've tried avggraph + linegraphs but I'm getting an error. I think this is because the graphs are generated by two different data frames.

I should also point out that the axes of both graphs are the same. Both graphs have the genes on the X-axis and the gene expression on the Y-axis.

Any help would be greatly appreciated!

解决方案

One way is to add the geom_line command for the second plot to the first plot. You need to tell ggplot that this geom is based on a different data set:

ggplot(avg, aes(x=Gene, y=mean)) + 
  geom_point() + 
  geom_line() + 
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.1) +
  geom_line(data = ge, aes(x=Gene, y=Exp, group=Sample, colour="#000099"),
            show_guide = FALSE)

The last geom_line command is for creating the lines based on the raw data.

这篇关于在R中使用ggplot2重叠两个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆