【问题标题】:Mapping more than one variable and label to the bar-plot in R将多个变量和标签映射到 R 中的条形图
【发布时间】:2021-09-06 11:17:25
【问题描述】:

这是我的数据框

dput(LSC)
structure(list(WBC = c(23.8, 162.4, 77.1, 155.1, 135.4, 23.3, 
NA, 88.2), Age = c(72.2638888888889, 65.0833333333333, 65.6972222222222, 
59.7666666666667, 22.7361111111111, 71.1972222222222, 67.7805555555556, 
46.8888888888889), Pre_leukemic_burden = c(96.2, 98.14, 100, 
99.84, 81.5, 97.64, 93.32, 100), Donor = structure(1:8, .Label = c("L1", 
"L2", "L3", "L4", "L5", "L6", "L7", "L8"), class = "factor"), 
    Survival = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L), .Label = c("Alive", 
    "Deceased"), class = "factor")), class = "data.frame", row.names = c(NA, 
-8L))

所以在这里我有一个变量是生存,我也有更多,但其他变量是连续变量。 我对每个患者的目标是我想显示每个 WBC 的水平、白血病负担和年龄。

df.long <- gather(LSC, variable,value, -Donor,-Survival)
df.long

ggplot(data = df.long, aes(x = Donor, y = value, fill = variable)) +
  geom_col(position = position_dodge()

上面的代码完成了这项工作,我得到了这样的结果

现在我有更多的分类变量,其中一个是生存。那么有没有办法可以将 survival 标签合并到情节中。

任何建议或帮助将不胜感激。

【问题讨论】:

  • facet_wrap(~survival, scale = "free_x")怎么样
  • 是的,谢谢。这是我每天的奋斗生物学家尝试数据分析 + 数据可视化的简单概念,但没有引起我的注意

标签: r ggplot2


【解决方案1】:

将这两个变量合二为一是否可行? 这是一个例子

library(tidyverse)
data = structure(list(WBC = c(23.8, 162.4, 77.1, 155.1, 135.4, 23.3, 
                            NA, 88.2), Age = c(72.2638888888889, 65.0833333333333, 65.6972222222222, 
                                               59.7666666666667, 22.7361111111111, 71.1972222222222, 67.7805555555556, 
                                               46.8888888888889), Pre_leukemic_burden = c(96.2, 98.14, 100, 
                                                                                          99.84, 81.5, 97.64, 93.32, 100), Donor = structure(1:8, .Label = c("L1", 
                                                                                                                                                             "L2", "L3", "L4", "L5", "L6", "L7", "L8"), class = "factor"), 
                    Survival = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L), .Label = c("Alive", 
                                                                                       "Deceased"), class = "factor")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                            -8L))

data.long <- gather(data, variable,value, -Donor,-Survival)
data.long = data.long %>%  mutate(surv_var = paste(Survival, variable, sep = "-"))

ggplot(data = data.long, aes(x = Donor, y = value, fill = surv_var)) +
  geom_col(position = position_dodge())
#> Warning: Removed 1 rows containing missing values (geom_col).

Survival 进行分面

           

ggplot(data = data.long, aes(x = Donor, y = value, fill = variable)) +
  geom_col(position = position_dodge()) +
  facet_wrap(~Survival)
#> Warning: Removed 1 rows containing missing values (geom_col).

reprex package (v2.0.0) 于 2021 年 9 月 6 日创建

【讨论】:

    【解决方案2】:

    我了解到您正在寻找条形图。我只是想提供这种另一种表示方式。如果您的案例数量更多,条形图将更难以阅读(这对于我展示的点图来说是正确的,但程度较小)。

    我将您的数据命名为df

    p1 <- ggplot(df, aes(Age, Pre_leukemic_burden)) +
          ggrepel::geom_text_repel(aes(Age, Pre_leukemic_burden, label=Donor)) +
          geom_point(color="red") +
          facet_wrap(~Survival)
    p2 <- ggplot(df, aes(Age, WBC)) +
      ggrepel::geom_text_repel(aes(Age, WBC, label=Donor)) +
      geom_point(color="gray50") +
      facet_wrap(~Survival)            
    cowplot::plot_grid(p1,p2, nrow=2)
    
    

    【讨论】:

    • 这是一种有趣的放置方式,也许我可以在我拥有大量元数据的情况下扩展数据
    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2015-01-22
    • 1970-01-01
    相关资源
    最近更新 更多