简体   繁体   English

使用 dplyr 在因子级别对统计数据进行排序

[英]Use dplyr to sort tallyed data on factor level

I have a dataframe df :我有一个数据框df

    structure(list(sample = structure(c(4L, 2L, 1L, 4L, 1L, 2L, 3L, 
3L, 3L, 1L), .Label = c("A1", "B1", "C1", "D2"), class = "factor"), 
    genotype = structure(c(4L, 2L, 2L, 2L, 4L, 4L, 1L, 2L, 3L, 
    1L), .Label = c("germline_private", "germline_recurrent", 
    "somatic_normal", "somatic_tumour"), class = "factor"), n = c(5L, 
    4L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 1L)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), vars = "sample", drop = TRUE, .Names = c("sample", 
"genotype", "n"), indices = list(c(2L, 4L, 9L), c(1L, 5L), 6:8, 
    c(0L, 3L)), group_sizes = c(3L, 2L, 3L, 2L), biggest_group_size = 3L, labels = structure(list(
    sample = structure(1:4, .Label = c("A1", "B1", "C1", "D2"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L), vars = "sample", drop = TRUE, .Names = "sample"))

head(df) 

sample event_no           genotype
A1        1     somatic_tumour
A1        2 germline_recurrent
A1        3 germline_recurrent
A1        4     somatic_tumour
A1        5 germline_recurrent
A1        6   germline_private

In this example, I want to tally the number of times genotype occurs in each sample, and then sort so that the samples are ordered by the number of somatic_tumour events在这个例子中,我想统计每个样本中基因型出现的次数,然后排序,以便样本按somatic_tumour事件的数量somatic_tumour

Here's what I have:这是我所拥有的:

library(tidyverse)

df <- df %>%
  group_by(sample, genotype) %>%
  tally %>%
  arrange(-n)

I then want to plot these counts for each sample, faceted by ~genotype:然后我想为每个样本绘制这些计数,由 ~genotype 分面:

p <- ggplot(df)
p <- p + geom_histogram(aes(sample, n), stat = "identity")
p <- p + facet_wrap(~genotype)
p

在此处输入图片说明

However, I want the samples in all panels to be sorted by the counts in the bottom right plot ( somatic_tumour )但是,我希望所有面板中的样本都按右下图( somatic_tumour )中的计数排序

Here is a way by creating a new_n by replacing the n of all except somatic_tumour with 0, and sort on the 2 n s, ie这是一种通过将除somatic_tumour之外的所有n替换为 0 来创建new_n的方法,并对 2 n排序,即

library(tidyverse)

df %>% 
 group_by(sample, genotype) %>% 
 tally() %>% 
 mutate(new_n = replace(n, genotype != 'somatic_tumour', 0)) %>% 
 arrange(-new_n, -n) %>% 
 select(-new_n)

which gives,这使,

 # A tibble: 11 x 3 # Groups: sample [4] sample genotype n <fct> <fct> <int> 1 A1 somatic_tumour 2 2 B1 somatic_tumour 2 3 D2 somatic_tumour 2 4 B1 germline_recurrent 4 5 A1 germline_recurrent 3 6 D2 germline_recurrent 3 7 C1 germline_private 2 8 C1 germline_recurrent 2 9 C1 somatic_normal 2 10 A1 germline_private 1 11 D2 somatic_normal 1

You also can use a left_join to add the number of occurrences of the somatic_tumour in each sample.您还可以使用left_join添加每个样本中somatic_tumour的出现次数。 Afterwards you use the n column of the somatic_tumour observations to create an ordered vector.之后,您使用somatic_tumour观测值的n列创建一个有序向量。 Thus, the x axis is arranged accordingly.因此,x 轴相应地排列。

library(dplyr)
library(ggplot2)
df %>% 
  left_join(df %>% filter(genotype == "somatic_tumour") %>% select(n, sample), 
            by = "sample") %>% 
  arrange(-n.y, -n.x) %>% 
  ungroup() %>% 
  mutate(sample = ordered(sample, 
                          df %>% filter(genotype == "somatic_tumour") %>% 
                            arrange(n) %>% 
                            select(sample) %>% 
                            as_vector(.))) %>% 
  ggplot() + 
  geom_histogram(aes(sample, n.x), stat = "identity") + 
  facet_wrap(~genotype)

Note: here NA labels are introduced, probably due to the small sample.注意:这里引入了NA标签,可能是因为样本少。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM