如何在r

时间:2018-11-30 22:16:48

标签: r concatenation rbind

我有以下单独的数据帧:eGenesall_genesgeneralnsPostnsPrepost和{{1 }}。每个数据框都有一个pre列,该列与数据框的名称相等,因此当我将它们全部合并在一起时,我知道是什么,例如:

Category

eGenes

我想要做的是按照特定顺序将它们连接到单个数据帧中,这样Gene pLI Gene_Symbol Category ENSG00000000938 1.771744e-01 FGR eGenes ENSG00000000971 9.996310e-01 CFH eGenes ENSG00000001036 3.655835e-04 FUCA2 eGenes 列将按以下顺序进行:Category(稍后用于绘图)。

但是当我执行eGenes, all_genes, general, pre, post, nsPre, nsPost然后基于all_lists <- rbind(eGenes, all_genes, general, pre, post, nsPre, nsPost)进行绘图时,这些绘图的显示顺序与我在rbind()中表达的顺序不同。如何控制订单?我什至尝试按以下方法一一串联,但这仍然行不通:

Category

绘图代码:

all_lists_1 <- rbind(eGenes, all_genes, general)
all_lists_2 <- rbind(all_lists_1, pre)
all_lists_3 <- rbind(all_lists_2, post)
all_lists_4 <- rbind(all_lists_3, nsPre)
all_lists <- rbind(all_lists_4, nsPost)

更新:

p <- ggplot(all_lists, aes(x=Category, y=pLI, fill=Category)) + geom_violin() + theme(axis.text.x = element_text(angle=90, hjust=1))

unique(all_lists$Category)

[1] All eGenes All Genes General Prenatal Postnatal Prenatal (Non-specific) Postnatal (Non-specific) Levels: All eGenes All Genes General Postnatal Postnatal (Non-specific) Prenatal Prenatal (Non-specific)

summary(all_lists)

1 个答案:

答案 0 :(得分:0)

您需要通过指定级别的顺序来更改Category级别的顺序,否则ggplot将根据字母顺序进行绘制。

  all_lists$Category <- factor(all_lists$Category, levels = c("eGenes", "all_genes", "general", "pre", "post", "nsPre", "nsPost"))

带有mtcars数据集的示例代码

library(ggplot2)
mtcars$cyl <- factor(mtcars$cyl) # change cyl as factor
mtcars$cyl
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
 Levels: 6 8 4
mtcars$cyl <- factor(mtcars$cyl, levels = c("6", "8", "4")) # change the order of cyl categories
ggplot(mtcars, aes(x=cyl, y=mpg)) + # plot
  geom_violin()

enter image description here