为什么 geom_bar y 轴与实际数字不成比例?

Why is geom_bar y-axis unproportional to actual numbers?

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

到目前为止我所做的是,我通过 SQL 检索包含这两个代码的数据集,然后将它们加载到 data.table 对象中。之后我加入他们以获取我需要的数据。 在此之后,我添加了包含这些代码的子字符串的列和包含这些特定子字符串的计数的其他列(这样我就可以绘制每个代码的出现次数)。 例如,我现在想将某些 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)

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

编辑: 不幸的是,我不能 post 图片。所以这里只是这些的链接。 这是 y 轴错误的图的屏幕截图: https://ibb.co/CbxX1n7 这是右侧显示的情节的屏幕截图: https://ibb.co/Xb8gyx1

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

由于您将 class 计数添加为附加列——而不是聚合——发生的情况是,对于数据中的每一行,class 计数堆叠在每一列之上其他:

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 创建