r - 如何在 ggplot facets 中按值对数据进行排序

标签 r ggplot2 facet facet-grid r-faq

我有以下数据框:

library(tidyverse)

tdat <- structure(list(term = c("Hepatic Fibrosis / Hepatic Stellate Cell Activation", 
"Cellular Effects of Sildenafil (Viagra)", "Epithelial Adherens Junction Signaling", 
"STAT3 Pathway", "Nitric Oxide Signaling in the Cardiovascular System", 
"LXR/RXR Activation", "NF-κB Signaling", "PTEN Signaling", "Gap Junction Signaling", 
"G-Protein Coupled Receptor Signaling", "Role of Osteoblasts, Osteoclasts and Chondrocytes in Rheumatoid Arthritis", 
"Osteoarthritis Pathway", "VDR/RXR Activation", "Axonal Guidance Signaling", 
"Basal Cell Carcinoma Signaling", "Putrescine Degradation III", 
"Tryptophan Degradation X (Mammalian, via Tryptamine)", "Factors Promoting Cardiogenesis in Vertebrates", 
"Dopamine Degradation", "Complement System", "Role of BRCA1 in DNA Damage Response", 
"Granzyme B Signaling", "GADD45 Signaling", "ATM Signaling", 
"Hereditary Breast Cancer Signaling", "Aryl Hydrocarbon Receptor Signaling", 
"Role of Oct4 in Mammalian Embryonic Stem Cell Pluripotency", 
"Factors Promoting Cardiogenesis in Vertebrates", "Sumoylation Pathway", 
"Hepatic Fibrosis / Hepatic Stellate Cell Activation", "GP6 Signaling Pathway", 
"Hepatic Fibrosis / Hepatic Stellate Cell Activation", "Intrinsic Prothrombin Activation Pathway", 
"Atherosclerosis Signaling", "Gap Junction Signaling", "LXR/RXR Activation", 
"FXR/RXR Activation", "HIF1α Signaling", "Bladder Cancer Signaling", 
"Ephrin A Signaling"), tissue = c("tissue-A", "tissue-A", "tissue-A", 
"tissue-A", "tissue-A", "tissue-A", "tissue-A", "tissue-A", "tissue-A", "tissue-A", 
"tissue-B", "tissue-B", "tissue-B", "tissue-B", "tissue-B", "tissue-B", 
"tissue-B", "tissue-B", "tissue-B", "tissue-B", "tissue-C", "tissue-C", 
"tissue-C", "tissue-C", "tissue-C", "tissue-C", "tissue-C", "tissue-C", "tissue-C", 
"tissue-C", "tissue-D", "tissue-D", "tissue-D", "tissue-D", "tissue-D", 
"tissue-D", "tissue-D", "tissue-D", "tissue-D", "tissue-D"), score = c(2.85, 
2.81, 2.53, 2.28, 2.19, 2.18, 2.13, 2.01, 1.97, 1.94, 6.01, 5.78, 
4.29, 2.85, 2.75, 2.67, 2.56, 2.32, 2.22, 2.11, 5.61, 2.91, 2.6, 
2.55, 2.23, 1.86, 1.56, 1.4, 1.34, 1.31, 6.26, 5.87, 4.47, 3.94, 
3.2, 3.17, 3.07, 2.97, 2.71, 2.61)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -40L), .Names = c("term", "tissue", 
"score"))

tdat

#> # A tibble: 40 x 3
#>    term                                                tissue   score
#>    <chr>                                               <chr>    <dbl>
#>  1 Hepatic Fibrosis / Hepatic Stellate Cell Activation tissue-A  2.85
#>  2 Cellular Effects of Sildenafil (Viagra)             tissue-A  2.81
#>  3 Epithelial Adherens Junction Signaling              tissue-A  2.53
#>  4 STAT3 Pathway                                       tissue-A  2.28
#>  5 Nitric Oxide Signaling in the Cardiovascular System tissue-A  2.19
#>  6 LXR/RXR Activation                                  tissue-A  2.18
#>  7 NF-κB Signaling                                     tissue-A  2.13
#>  8 PTEN Signaling                                      tissue-A  2.01
#>  9 Gap Junction Signaling                              tissue-A  1.97
#> 10 G-Protein Coupled Receptor Signaling                tissue-A  1.94
#> # ... with 30 more rows

我想要做的是制作一个像按组织分组的图一样的条形图,并根据每组中的分数降序排列。

我试过这个:

term_order <- tdat$term[order(tdat$tissue, tdat$score)]
tdat$term <- factor(tdat$term, levels = unique(term_order))
tdat$tissue <- factor(tdat$tissue, levels = c("tissue-C", "tissue-A", "tissue-D", "tissue-B"), ordered = TRUE)

tp <- ggplot(tdat, aes(x = score, y = term)) + 
  geom_segment(aes(yend = term), xend = 0, colour = "grey50") + 
  geom_point(size = 3, aes(colour = tissue)) + 
  theme_bw() +
  scale_colour_brewer(palette = "Dark2") +
  theme(panel.grid.major.y = element_blank()) + 
  facet_grid(tissue ~ ., scales = "free_y", space = 'free_y')    

tp

但我得到的是这个情节:

enter image description here

请注意,在组织 D 中,术语未相应排序。
有什么办法呢?

最佳答案

我们可以用

(1) reorder_within() 重新排序功能 termtissue方面。

library(tidyverse)
library(forcats)

tdat <- tdat %>% 
  mutate(term = factor(term),
         tissue = factor(tissue, levels = c("tissue-C", "tissue-A", "tissue-D", "tissue-B"), 
                         ordered = TRUE))

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}

ggplot(tdat, aes(reorder_within(term, score, tissue), score)) +
  geom_segment(aes(xend = reorder_within(term, score, tissue), yend = 0), 
               colour = "grey50") +
  geom_point(size = 3, aes(colour = tissue)) + 
  scale_x_reordered() +
  facet_grid(tissue ~ ., scales = "free", space = "free") +
  coord_flip() +
  scale_colour_brewer(palette = "Dark2") +
  theme_bw() +
  theme(panel.grid.major.y = element_blank()) + 
  theme(legend.position = "bottom")



或(2)类似的想法

### https://trinkerrstuff.wordpress.com/2016/12/23/ordering-categories-within-ggplot2-facets/
tdat %>% 
  mutate(term = reorder(term, score)) %>%
  group_by(tissue, term) %>% 
  arrange(desc(score)) %>% 
  ungroup() %>% 
  mutate(term = factor(paste(term, tissue, sep = "__"), 
                       levels = rev(paste(term, tissue, sep = "__")))) %>%
    ggplot(aes(term, score)) +
        geom_segment(aes(xend = term, yend = 0), 
                   colour = "grey50") +
        geom_point(size = 3, aes(colour = tissue)) + 
        facet_grid(tissue ~., scales = "free", space = 'free') +
        scale_x_discrete(labels = function(x) gsub("__.+$", "", x)) +
        coord_flip() +
        scale_colour_brewer(palette = "Dark2") +
        theme_bw() +
        theme(panel.grid.major.y = element_blank()) + 
        theme(legend.position = "bottom",
              axis.ticks.y = element_blank())



或者 (3) 对整个数据框进行排序,并对每个构面组内的类别 ( tissue ) 进行排序!

### https://drsimonj.svbtle.com/ordering-categories-within-ggplot2-facets
# 
tdat2 <- tdat %>% 
  # 1. Remove grouping
  ungroup() %>% 
  # 2. Arrange by
  #   i.  facet group (tissue)
  #   ii. value (score)
  arrange(tissue, score) %>%
  # 3. Add order column of row numbers
  mutate(order = row_number())
tdat2

#> # A tibble: 40 x 4
#>    term                                                tissue  score order
#>    <fct>                                               <ord>   <dbl> <int>
#>  1 Hepatic Fibrosis / Hepatic Stellate Cell Activation tissue~  1.31     1
#>  2 Sumoylation Pathway                                 tissue~  1.34     2
#>  3 Factors Promoting Cardiogenesis in Vertebrates      tissue~  1.4      3
#>  4 Role of Oct4 in Mammalian Embryonic Stem Cell Plur~ tissue~  1.56     4
#>  5 Aryl Hydrocarbon Receptor Signaling                 tissue~  1.86     5
#>  6 Hereditary Breast Cancer Signaling                  tissue~  2.23     6
#>  7 ATM Signaling                                       tissue~  2.55     7
#>  8 GADD45 Signaling                                    tissue~  2.6      8
#>  9 Granzyme B Signaling                                tissue~  2.91     9
#> 10 Role of BRCA1 in DNA Damage Response                tissue~  5.61    10
#> # ... with 30 more rows

ggplot(tdat2, aes(order, score)) +
  geom_segment(aes(xend = order, yend = 0), 
               colour = "grey50") +
  geom_point(size = 3, aes(colour = tissue)) +
  facet_grid(tissue ~ ., scales = "free", space = "free") +
  coord_flip() +
  scale_colour_brewer(palette = "Dark2") +
  theme_bw() +
  theme(panel.grid.major.y = element_blank()) + 
  theme(legend.position = "bottom")



# To finish we need to replace the numeric values on each x-axis 
# with the appropriate labels
ggplot(tdat2, aes(order, score)) +
  geom_segment(aes(xend = order, yend = 0), 
               colour = "grey50") +
  geom_point(size = 3, aes(colour = tissue)) + 
  scale_x_continuous(
    breaks = tdat2$order,
    labels = tdat2$term) +
  # scale_y_continuous(expand = c(0, 0)) +
  facet_grid(tissue ~ ., scales = "free", space = "free") +
  coord_flip() +
  scale_colour_brewer(palette = "Dark2") +
  theme_bw() +
  theme(panel.grid.major.y = element_blank()) + 
  theme(legend.position = "bottom",
        axis.ticks.y = element_blank())

关于r - 如何在 ggplot facets 中按值对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52214071/

相关文章:

r - R中从多个起点和终点生成序列的向量化方法

r - lapply 估计许多因变量,然后用 Stargazer 制表

r - ggplot2 - 打印绘图气球内存

r - 情节中的文字错误 | Jupyter-R

r - 分组变量上的 ggplot 和 ggsignif 错误

r - ggplot : Order bars in faceted bar chart per facet

regex - 用r中的条件替换数据框中的部分字符串

r - 循环遍历 data.table 并根据某些条件创建新列

r - 在 R 中使用 facet_grid 单独突出显示数据

python - Seaborn FacetGrid + 热图灰色边框