根据其中一个组对线图进行排序

时间:2015-08-04 15:14:49

标签: r ggplot2

我有基因,样本的数据框(tcell_pdx_log_wgene_melt)和某些基因的表达值。 我的数据框架如下:

gene    sample                               log_fpkm
ITGB1   Sample_7630_T1_PDX_mousereads        4.4667698
ADIPOR1 Sample_7630_T1_PDX_mousereads        3.7562811
ADIPOR2 Sample_7630_T1_PDX_mousereads        2.4823200
RYK     Sample_7630_T1_PDX_mousereads        2.4521252
JAG1    Sample_7630_T1_PDX_mousereads        1.7713867
ITGB1   Sample_NYA_MT.05_primary_mousereads  1.9555776
ADIPOR1 Sample_NYA_MT.05_primary_mousereads  1.7365991
ADIPOR2 Sample_NYA_MT.05_primary_mousereads  2.1131181
RYK     Sample_NYA_MT.05_primary_mousereads  1.1464496
JAG1    Sample_NYA_MT.05_primary_mousereads  0.6931472
ITGB1   Sample_7630_T1_PDX_humanreads        4.5363987
ADIPOR1 Sample_7630_T1_PDX_humanreads        3.5718399
ADIPOR2 Sample_7630_T1_PDX_humanreads        2.4756977
RYK     Sample_7630_T1_PDX_humanreads        1.8449842
JAG1    Sample_7630_T1_PDX_humanreads        1.7451918

下面的图表按字母顺序排列这些基因,但我希望按照变量类型“Sample_7630_T1_PDX_humanreads”之一对图表进行排序

 tcell_pdx_log_wgene_melt$sample <- as.character(tcell_pdx_log_wgene_melt$sample)
 tcell_pdx_log_wgene_melt$sample <- factor(tcell_pdx_log_wgene_melt$sample, levels=unique(tcell_pdx_log_wgene_melt$sample))
 p <- ggplot(tcell_pdx_log_wgene_melt,aes(gene,log_fpkm,group=sample)) + 
             geom_point()
 p + geom_line(aes(color=sample))

1 个答案:

答案 0 :(得分:0)

不确定您要找的是什么,但我dput()并在下面绘制了您的数据。也许你可以用它来解释你想要做什么?如果您制作一个可重复性最小的示例来与您的问题一起使用。我们可以使用的东西,用于向您展示如何解决您的问题。您可以查看this SO post如何在R中制作出色的可重复示例。

wassss

tcell_pdx_log_wgene_melt <- structure(list(gene = structure(c(3L, 1L, 2L, 5L, 4L, 
3L, 1L, 2L, 5L, 4L, 3L, 1L, 2L, 5L, 4L), 
.Label = c("ADIPOR1", "ADIPOR2", 
"ITGB1", "JAG1", "RYK"), class = "factor"), 
sample = structure(c(2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L), 
.Label = c("Sample_7630_T1_PDX_humanreads", 
"Sample_7630_T1_PDX_mousereads", "Sample_NYA_MT.05_primary_mousereads"
), class = "factor"), log_fpkm = c(4.4667698, 3.7562811, 2.48232, 
2.4521252, 1.7713867, 1.9555776, 1.7365991, 2.1131181, 1.1464496, 
0.6931472, 4.5363987, 3.5718399, 2.4756977, 1.8449842, 1.7451918
)), .Names = c("gene", "sample", "log_fpkm"), 
class = "data.frame", row.names = c(NA, -15L))

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)

 p <- ggplot( tcell_pdx_log_wgene_melt,aes(gene,log_fpkm,group=sample))
 +  geom_point()
 p + geom_line(aes(color=sample))