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:
数据框“ge”的示例如下所示。实际上有10个基因,每个有200个样本,所以有2000行和3列:
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:
数据帧“avg”的示例如下所示。这是所有样本中每个基因数据点的平均值。实际上这个图有10个基因,所以这个矩阵是4col X 10行:
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.
我想把avggraph叠加到linegraphs的顶部。有没有办法做到这一点?我尝试过avggraph + linegraphs,但我得到了一个错误。我认为这是因为图是由两个不同的数据帧生成的。
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.
我还应该指出这两个图的坐标轴是相同的。这两个图都有x轴上的基因和y轴上的基因表达。
Any help would be greatly appreciated!
如有任何帮助,我们将不胜感激!
1 个解决方案
#1
22
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:
一种方法是将第二个图的geom_line命令添加到第一个图中。你需要告诉ggplot这个geom是基于不同的数据集:
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.
最后的geom_line命令用于基于原始数据创建行。
#1
22
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:
一种方法是将第二个图的geom_line命令添加到第一个图中。你需要告诉ggplot这个geom是基于不同的数据集:
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.
最后的geom_line命令用于基于原始数据创建行。