【问题标题】:ggplot2: Issue with y-axis when generating graph with geom_colggplot2:使用 geom_col 生成图形时出现 y 轴问题
【发布时间】:2022-08-04 22:54:40
【问题描述】:

我回到 R 为发布图制作了一些图表。但是,我非常生疏,仍在学习过程中,并且在基于条形/列的图形生成上苦苦挣扎。

上下文:基本上我们有结果体外用来自不同患者的不同类型的细胞进行分析。变量如下:

  • 患者
  • 细胞类型
  • 比率(效应细胞/应答细胞,1:3、1:10 和 1:30)
  • 癌症类型

我想生成一个图表,条形图表示根据一种细胞类型/条件的稀释度的体外测定值(一种条件为 1:3、1:10、1:30,然后是 1:3、1:10 , 1:30 秒等)。有点像这样:

exemple from publication

我已经使用虚拟数据框运行了以下代码

df <- data.frame(
  Patient = rep(c(\"Ptn1\", \"Ptn2\", \"Ptn3\"), each = 8),
  Cells = rep(rep(c(\"T1\", \"T2\"), each = 3), 4),
  Ratio = rep(c(\"1:3\", \"1:10\", \"1:30\"), 8),
  Value = c(0.2, 0.5, 0.9, 0.7, 0.8, 0.9, 0.1, 0.4, 0.8, 0.6, 0.8, 0.9, 0.15, 0.45, 0.85, 0.55, 0.85, 0.95, 0.1, 0.3, 0.8, 0.7, 0.8, 0.9))

df$variable <- interaction(df$Cells, df$Ratio)
df$variable <- factor(df$variable, levels = c(\"T1.1:30\", \"T1.1:10\", \"T1.1:3\", \"T2.1:30\", \"T2.1:10\", \"T2.1:3\"))
levels(df$variable)

ggplot(df, aes(x = variable, y = Value)) + geom_col()

这给了我这张图:

Dummy graph

奇怪的是,我在 df 中的值都没有高于 1(0.1 到 0.9),但是 y 比例给出的值上升到 3 或 4。我对真实数据集有同样的问题。

此外,根据一种条件/单元格类型“收集”条并在单元格类型/条件之间留出小间隙的优雅方式是什么?

谢谢你的帮助

    标签: r ggplot2 yaxis geom-col


    【解决方案1】:

    geom_col() 不计算任何摘要信息,而是将y 保持原样,然后堆叠结果。我想您可能正在寻找geom_bar()stat = "summary"

    此外,reconsider dynamite plots

    library(ggplot2)
    
    df <- data.frame(
      Patient = rep(c("Ptn1", "Ptn2", "Ptn3"), each = 8),
      Cells = rep(rep(c("T1", "T2"), each = 3), 4),
      Ratio = rep(c("1:3", "1:10", "1:30"), 8),
      Value = c(0.2, 0.5, 0.9, 0.7, 0.8, 0.9, 0.1, 0.4, 0.8, 0.6, 0.8, 0.9, 0.15, 0.45, 0.85, 0.55, 0.85, 0.95, 0.1, 0.3, 0.8, 0.7, 0.8, 0.9))
    
    df$variable <- interaction(df$Cells, df$Ratio)
    df$variable <- factor(df$variable, levels = c("T1.1:30", "T1.1:10", "T1.1:3", "T2.1:30", "T2.1:10", "T2.1:3"))
    
    ggplot(df, aes(x = variable, y = Value)) +
      geom_errorbar(
        stat = "summary", 
        fun.data = ~ mean_sdl(.x, mult = 1),
        width = 0.2
      ) +
      geom_bar(
        stat = "summary", fun = mean
      )
    

    reprex package (v2.0.1) 于 2022 年 8 月 4 日创建

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2020-10-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      相关资源
      最近更新 更多