I have the following data frame:
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
What I want to do is to make a barplot like a plot grouped by tissue and ordered descendingly according to the score in each group.
I tried this:
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
But what I get is this plot:
Notice that in tissue-D the term is not sorted accordingly. What's the way to go about it?
We can use
(1)
reorder_within()
function to reorderterm
withintissue
facets.Or (2) similar idea
Or (3) orders the entire data frame, and also orders the categories (
tissue
) within each facet group!