首页 > 解决方案 > 在ggplot2的每个方面覆盖相同的线图

问题描述

我需要在所有方面覆盖相同的“target_gene”图。如果我在 1 个面板中绘制所有内容,我可以轻松地覆盖它。facet_wrap但是,当将此操作与 请参阅下面的脚本。' select_genes_plot '有效(参见相同的面板图),但'select_genes_plot_facet'不起作用(参见图方面的图)。

请不要说我不能使用+annotate(geom='point',x=target_gene$gene_exp.Time,y=target_gene$gene_exp.Value),因为我还想绘制 sd 误差线。

转录组重塑和目标基因数据如下所示,请参阅转录组数据示例目标基因数据

library(ggplot2)
library(dplyr)
library(reshape2)

##1. Read transcriptome dataset
setwd('./')
transcriptome_data <- read.csv ('./Kaladp_expression_FPKM_average_sd.csv', header = TRUE)
reshape_data <- melt(transcriptome_data)  #the function melt reshapes it from wide to long
average_geneexpression <- reshape_data[1:(743136/2),]
sd_geneexpression <- reshape_data[((743136/2)+1):743136,]
transcriptome_reshape <- cbind(average_geneexpression, sd_geneexpression)

colnames(transcriptome_reshape)[2]<- 'gene_exp.Time'
colnames(transcriptome_reshape)[3]<- 'gene_exp.Value'
colnames(transcriptome_reshape)[4]<- 'transcript_sd_data'
colnames(transcriptome_reshape)[5]<- 'sd.Time'
colnames(transcriptome_reshape)[6]<- 'sd.Value'

##2. Select ONE key target gene 
target_gene <- transcriptome_reshape %>%
  filter (Transcript %in% c('Kaladp0977s0008.1')) 

##3.Plot options

##plot whole genome
ggplot(data = transcriptome_reshape, aes(x = gene_exp.Time, y = gene_exp.Value, group = Transcript)) +
  geom_line()

##plot target gene alone
ggplot(target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group = 1)) +
  geom_line()+
  geom_point()

##plot all genes on same panel
select_genes_plot <- transcriptome_reshape %>%
  filter (Transcript %in% c('Kaladp0498s0001.1','Kaladp0011s0858.1','Kaladp0071s0450.1')) %>%
  ggplot(mapping = aes(x = gene_exp.Time, y = gene_exp.Value, color = Transcript)) +
  geom_line(aes(group=Transcript))+
  geom_point()+
  geom_errorbar(aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value), width = 0.2, position = position_dodge(0.0))+
  geom_point(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value), color = 'black', shape = 17)+ #this line plots target gene
  geom_line(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group=Transcript), color = 'black')+ #this line plots target gene
  geom_errorbar(data = target_gene, aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value),color = 'black', width = 0.2, position = position_dodge(0.0))#this line plots target gene
  
select_genes_plot

##plot in facets
select_genes_plot_facets <- transcriptome_reshape %>%
  filter (Transcript %in% c('Kaladp0498s0001.1','Kaladp0011s0858.1','Kaladp0071s0450.1')) %>%
  ggplot(mapping = aes(x = gene_exp.Time, y = gene_exp.Value, color = Transcript)) +
  geom_line(aes(group=Transcript))+
  geom_point()+
  geom_errorbar(aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value), width = 0.2, position = position_dodge(0.0))+
  facet_wrap(facets = vars(Transcript), ncol = 3)+
  geom_point(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value), color = 'black', shape = 17)+ #this line plots target gene
  geom_line(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group = 1), color = 'black')+ #this line plots target gene
  
select_genes_plot_facets

标签: rggplot2overlayfacet-wrap

解决方案


推荐阅读