【问题标题】:Why is geom_bar y-axis unproportional to actual numbers?为什么 geom_bar y 轴与实际数字不成比例?
【发布时间】:2019-09-05 09:00:36
【问题描述】:

抱歉,如果这个问题已经存在 - 现在已经用谷歌搜索了一段时间,但没有找到任何东西。 我对 R 相对较新,并且在做所有这些的同时也在学习。 我应该通过 r markdown 创建一些 PDF,以分析具有特定主诊断和辅助诊断的患者数据。为此,我应该通过 ggplot(geom_bar 和 geom_boxplot)绘制一些数字。

所以到目前为止我所做的是,我通过 SQL 检索包含这两个代码的数据集,然后将它们加载到 data.table-objects 中。之后我加入他们以获取我需要的数据。 在此之后,我添加了包含这些代码的子字符串的列以及包含这些特定子字符串的计数的其他列(因此我可以绘制每个代码的出现)。 例如,我现在想将某些 data.table 放入 geom_bar 或 geom_boxplot 并使其可见。这确实有效,但我的 y 轴有一个奇怪的比例,不适合它实际应该显示的数字。条形的比例也不准确。

例如:一个诊断出现 600 次,另一个诊断出现 1000 次。 y 轴显示 0 - 500.000 - 1.000.000 - 1.500.000 - ...的步长。 显示 600 的 Bar 超小,显示 1000 的 Bar 上升到 1.500.000

如果我之前创建了一个新变量并通过 count() 计算我需要的内容并绘制它,它就可以工作。我为 y 轴放置的行在两个变量中具有相同的数据类型(整数)

这就是我创建用于绘图的 data.table 的方式

exazerbationsHdComorbiditiesNd <- allExazerbationsHd[allComorbiditiesNd, on="encounter_num", nomatch=0]
exazerbationsHdComorbiditiesNd <- exazerbationsHdComorbiditiesNd[, c("i.DurationGroup", "i.DurationInDays", "i.start_date", "i.end_date", "i.duration", "i.patient_num"):=NULL]
exazerbationsHdComorbiditiesNd[ , IcdHdCodeCount := .N, by = concept_cd]
exazerbationsHdComorbiditiesNd[ , IcdHdCodeClassCount := .N, by = IcdHdClass]

如果我现在想通过 IcdHdCodeClassCount 绘制条形图,例如 IcdHdClass,我会执行以下操作:

ggplot(exazerbationsHdComorbiditiesNd, aes(exazerbationsHdComorbiditiesNd$IcdHdClass, exazerbationsHdComorbiditiesNd$IcdHdCodeClassCount, label=exazerbationsHdComorbiditiesNd$IcdHdCodeClassCount)) + geom_bar(stat = "identity") + geom_text(vjust = 0, size = 5)

它以奇怪的比例输出所述条形图。 如果我先做:

plotTest <- count(exazerbationsHdComorbiditiesNd, exazerbationsHdComorbiditiesNd$IcdHdClass)

然后绘制条形图:

ggplot(plotTest, aes(plotTest$`exazerbationsHdComorbiditiesNd$IcdHdClass`, plotTest$n, label=plotTest$n)) + geom_bar(stat = "identity") + geom_text(vjust = 0, size = 5)

这一切都很完美并且有效。 我还检查了我需要的列的数据类型:

sapply(exazerbationsHdComorbiditiesNd, class)
sapply(plotTest, class)

在这两个变量中,我需要的列都是字符和整数类型

编辑: 不幸的是我不能发布图片。所以这里只是那些链接。 以下是 y 轴错误的绘图截图: https://ibb.co/CbxX1n7 这是右图的截图: https://ibb.co/Xb8gyx1

这是我复制出 data.table 对象的一些示例数据: Exampledata

【问题讨论】:

  • 如果可能,请提供一些示例数据,这将有助于显示一些有此问题的图。 stackoverflow.com/help/minimal-reproducible-example
  • 感谢您的回答。不幸的是,创建一个最小的可复制示例有点棘手。编辑了 2 个链接,但您可以在其中找到绘图。
  • 我怀疑在其中一种情况下聚合失败。因此,请检查您输入 ggplot 的数据。

标签: r ggplot2 rstudio geom-bar


【解决方案1】:

由于您将类计数添加为附加列(而不是聚合),因此发生的情况是,对于数据中的每一行,类计数相互叠加:

library(tidyverse)

set.seed(42)

df <- tibble(class = sample(letters[1:3], 10, replace = TRUE)) %>% 
  add_count(class, name = "count")

df # this is essentially what your data looks like
#> # A tibble: 10 x 2
#>    class count
#>    <chr> <int>
#>  1 a         5
#>  2 a         5
#>  3 a         5
#>  4 a         5
#>  5 b         3
#>  6 b         3
#>  7 b         3
#>  8 a         5
#>  9 c         2
#> 10 c         2

ggplot(df, aes(class, count)) + geom_bar(stat = "identity")

您可以使用position = "identity",这样条形图就不会堆积:

ggplot(df, aes(class, count)) +
  geom_bar(stat = "identity", position = "identity")

但是,这会在您的绘图中创建一大堆您看不到的不必要的层。更好的方法是在绘图之前从数据中删除额外的行:

df %>%
  distinct(class, count)
#> # A tibble: 3 x 2
#>   class count
#>   <chr> <int>
#> 1 a         5
#> 2 b         3
#> 3 c         2

df %>% 
  distinct(class, count) %>%
  ggplot(aes(class, count)) +
  geom_bar(stat = "identity")

reprex package (v0.3.0.9000) 于 2019-09-05 创建

【讨论】:

  • 抱歉回答延迟!但是非常感谢Mikko!在准备我的数据时并没有真正考虑到这一点,而且完全有道理。工作得很好,现在看起来不错!
猜你喜欢
  • 2012-01-26
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多