【问题标题】:How to make a bar plot for the following data?如何为以下数据制作条形图?
【发布时间】:2022-11-23 06:56:10
【问题描述】:

我有这个数据描述了某些细胞类型的出现,在对某些药物有反应的患者和无反应的患者中:

> dput(cellsdata)

structure(list(response = c(1, 7, 2, 3, 4, 2, 1, 2, 1, 5, 5, 
4, 2, 0, 0, 2, 0, 0, 1, 3, 1, 2, 1, 0, 4, 3, 2, 3, 1, 1, 5, 0, 
2, 2, 5, 4, 4, 2), no_response = c(1, 0, 0, 0, 0, 0, 1, 0, 1, 
0, 0, 0, 0, 3, 2, 3, 2, 0, 3, 0, 0, 0, 1, 1, 0, 1, 4, 0, 0, 0, 
0, 2, 1, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c("Adipocytes", 
"B-cells", "Basophils", "CD4+ memory T-cells", "CD4+ naive T-cells", 
"CD4+ T-cells", "CD4+ Tcm", "CD4+ Tem", "CD8+ naive T-cells", 
"CD8+ T-cells", "CD8+ Tcm", "Class-switched memory B-cells", 
"DC", "Endothelial cells", "Eosinophils", "Epithelial cells", 
"Fibroblasts", "Hepatocytes", "ly Endothelial cells", "Macrophages", 
"Macrophages M1", "Macrophages M2", "Mast cells", "Melanocytes", 
"Memory B-cells", "Monocytes", "mv Endothelial cells", "naive B-cells", 
"Neutrophils", "NK cells", "pDC", "Pericytes", "Plasma cells", 
"pro B-cells", "Tgd cells", "Th1 cells", "Th2 cells", "Tregs"
))

我想要一个条形图,看起来像这样,我还需要它是有序的,从左边最高的箱子到右边。

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    我猜是这样的:

    library(tidyverse)
    
    cellsdata %>%
      rownames_to_column("Cell") %>%
      mutate(n = response + no_response) %>%
      arrange(-n) %>%
      mutate(Cell = factor(Cell, Cell)) %>%
      select(-n) %>%
      pivot_longer(-Cell, names_to = "Response") %>%
      ggplot(aes(Cell, value, fill = Response)) +
      geom_col(position = "stack") +
      theme_light(base_size = 16) +
      theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
      scale_fill_manual(values = c("#e0283D", "#3A43D8"),
                        labels = c("No response", "Response"), name = NULL)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 2014-03-31
      • 2021-03-30
      • 2020-11-10
      • 1970-01-01
      • 2021-12-13
      • 2023-02-06
      • 2021-09-22
      相关资源
      最近更新 更多