繁体   English   中英

R 中有两个数值变量的条形图

[英]Barchart with two numeric variables in R

我有以下数据:

structure(list(cells = 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"), Response = c(0, 8, 0, 5, 4, 4, 3, 3, 2, 3, 8, 5, 3, 
0, 1, 1, 0, 2, 1, 5, 3, 3, 2, 2, 7, 4, 3, 5, 2, 2, 8, 0, 1, 2, 
3, 3, 2, 8), No_Response = c(6, 0, 1, 1, 2, 2, 1, 3, 3, 1, 1, 
2, 1, 2, 3, 5, 2, 2, 3, 1, 1, 2, 2, 1, 0, 0, 5, 0, 1, 0, 0, 3, 
1, 1, 5, 3, 1, 3)), class = "data.frame", row.names = c(NA, -38L
))

我想制作一个条形图,以便对于每种单元格类型,我得到蓝色的Response编号和红色的No_Response编号。 或多或少看起来像这样的东西:(x轴上的单元格和y轴上的值):

在此处输入图像描述

library(ggplot2)

data <-
  structure(
    list(
      cells = 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"
      ),
      Response = c(
        0,
        8,
        0,
        5,
        4,
        4,
        3,
        3,
        2,
        3,
        8,
        5,
        3,
        0,
        1,
        1,
        0,
        2,
        1,
        5,
        3,
        3,
        2,
        2,
        7,
        4,
        3,
        5,
        2,
        2,
        8,
        0,
        1,
        2,
        3,
        3,
        2,
        8
      ),
      No_Response = c(
        6,
        0,
        1,
        1,
        2,
        2,
        1,
        3,
        3,
        1,
        1,
        2,
        1,
        2,
        3,
        5,
        2,
        2,
        3,
        1,
        1,
        2,
        2,
        1,
        0,
        0,
        5,
        0,
        1,
        0,
        0,
        3,
        1,
        1,
        5,
        3,
        1,
        3
      )
    ),
    class = "data.frame",
    row.names = c(NA,-38L)
  )
df <- cbind(data[, 1], data[, 2], rep("Response", nrow(data)))
df <-
  as.data.frame(rbind(df, cbind(data[, 1], data[, 3], rep(
    "No_Response", nrow(data)
  ))))
colnames(df) <- c("cells", "quant.response", "type.response")
p <-
  ggplot(data = df, aes(x = cells, y = quant.response, fill = type.response)) +
  geom_bar(stat = "identity")

print(p)
library(ggplot2)
library(data.table)

setDT(df)
df <- melt(df, id.vars = c("cells"), measure.vars = c("Response", "No_Response"))
colnames(df) <- c("cell", "resp", "val")

ggplot(df, aes(x = cell, y = val, fill = resp)) + 
  geom_bar(stat = "identity") +
  scale_fill_manual(values = c("blue", "red")) +
  theme(axis.text.x = element_text(angle = 90))

堆积条形图

暂无
暂无

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

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