R语言 使用gghighlight突出显示一组数据时出错

ntjbwcob  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(131)

我有关于院内心脏骤停的数据,其中包含月份、事件位置(单位)、该月该位置的事件数量(单位计数)以及事件被目击的百分比、是否给予肾上腺素、是否发生除颤、是否确认气道以及是否监测CPR质量(质量指标)。
我试图创建记分卡,突出显示每个位置的质量指标,与其他位置相比,使用gghighlight。这里是一个最小的数据集和绘图代码:

# Session info
# R version 4.2.2 (2022-10-31 ucrt)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19045)

# Matrix products: default

# locale:
# [1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
# [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.utf8    

# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     

# other attached packages:
#  [1] gridExtra_2.3     gghighlight_0.4.0 forcats_1.0.0     stringr_1.5.0    
#  [5] purrr_1.0.1       tidyr_1.3.0       tibble_3.1.8      ggplot2_3.4.1    
#  [9] tidyverse_1.3.2   lubridate_1.9.2   dplyr_1.1.0       readr_2.1.4      

# loaded via a namespace (and not attached):
#  [1] Rcpp_1.0.10         pillar_1.8.1        compiler_4.2.2      cellranger_1.1.0   
#  [5] dbplyr_2.3.1        tools_4.2.2         bit_4.0.5           jsonlite_1.8.4     
#  [9] googledrive_2.0.0   lifecycle_1.0.3     gargle_1.3.0        gtable_0.3.1       
# [13] timechange_0.2.0    pkgconfig_2.0.3     rlang_1.0.6         reprex_2.0.2       
# [17] DBI_1.1.3           cli_3.6.0           rstudioapi_0.14     ggrepel_0.9.3      
# [21] parallel_4.2.2      haven_2.5.1         xml2_1.3.3          withr_2.5.0        
# [25] httr_1.4.5          generics_0.1.3      vctrs_0.5.2         fs_1.6.1           
# [29] hms_1.1.2           bit64_4.0.5         googlesheets4_1.0.1 grid_4.2.2         
# [33] tidyselect_1.2.0    glue_1.6.2          R6_2.5.1            fansi_1.0.4        
# [37] readxl_1.4.2        vroom_1.6.1         farver_2.1.1        modelr_0.1.10      
# [41] tzdb_0.3.0          magrittr_2.0.3      backports_1.4.1     scales_1.2.1       
# [45] ellipsis_0.3.2      rvest_1.0.3         colorspace_2.1-0    labeling_0.4.2     
# [49] utf8_1.2.3          stringi_1.7.12      munsell_0.5.0       broom_1.0.3        
# [53] crayon_1.5.2       

library("readr")
library("lubridate")
library("tidyverse")
library("gghighlight")

df <- structure(list(month = structure(c(18993, 18993, 18993, 18993, 
19024, 19024, 19024, 19052, 19052, 19052, 19083, 19083), class = "Date"), 
    Unit = c("CICU", "MICU", "NSICU", "SICU", "CICU", "MICU", 
    "NSICU", "CICU", "MICU", "SICU", "CICU", "MICU"), unitCount = c(4L, 
    6L, 2L, 2L, 5L, 12L, 1L, 2L, 6L, 3L, 1L, 8L), witness = c(75, 
    83, 100, 50, 60, 67, 100, 100, 100, 100, 100, 100), epi = c(75, 
    83, 100, 100, 80, 83, 100, 100, 100, 100, 0, 88), defib = c(0, 
    100, NaN, NaN, 50, 100, NaN, NaN, NaN, NaN, NaN, NaN), airwayConfirm = c(75, 
    50, 0, 50, 40, 33, 0, 0, 33, 67, 0, 25), CPRQuality = c(25, 
    33, 0, 50, 0, 25, 0, 0, 17, 33, 0, 12)), row.names = c(NA, 
12L), class = "data.frame")

# Function for plotting witnessed, epi, airway confirmation, and CPR quality metrics by unit
plotUnitMetric <- function(y, unitHighlight) {
  ggplot(df, aes(x = month, y, color = Unit)) +
    geom_line(linewidth = 0.5) +
    geom_point(shape = 21, fill = "white", stroke = 1.5, size = 2) +
    gghighlight(Unit == unitHighlight, use_direct_label = FALSE) + 
    scale_x_date(date_breaks = "1 month", date_labels = "%b") +
    labs(x = "Month", y = "Percentage") +
    theme_bw() +
    theme(legend.position = "none")
}

unitWitness <- plotUnitMetric(y = df$witness, "MICU") + 
  labs(title = "Percentage of CPAs that were witnessed by unit and month")

但是,调用gghighlight的行在运行时会收到以下警告消息:

Warning messages:
1: Tried to calculate with group_by(), but the calculation failed.
Falling back to ungrouped filter operation... 
2: Tried to calculate with group_by(), but the calculation failed.
Falling back to ungrouped filter operation...

当我尝试打印时,我得到以下结果:

Error in `geom_line()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (12)
✖ Fix the following mappings: `y`
Run `rlang::last_error()` to see where the error occurred.

运行rlang::last_error()得到以下结果:

<error/rlang_error>
Error in `geom_line()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (12)
✖ Fix the following mappings: `y`
---
Backtrace:
  1. base::print(unitWitness)
  2. ggplot2:::print.ggplot(unitWitness)
  4. ggplot2:::ggplot_build.ggplot(x)
  5. ggplot2:::by_layer(...)
 12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 13. l$compute_aesthetics(d, plot)
 14. ggplot2 (local) compute_aesthetics(..., self = self)
 15. ggplot2:::check_aesthetics(evaled, n)

如果我将gghighlight从函数中取出,并将其连接到定义“unitWitness”的行中,我会得到这个错误。
我不确定我在哪里出错了,因为我已经反复检查了几次语法。我希望最终得到一个看起来像这样的图:

xzlaal3s

xzlaal3s1#

一般来说,ggplot2 &所有遵循它的包都不喜欢<dataframe>$<variable>形式的美学Map,而这正是您试图以df$witness形式传递到函数中的Map。
plotUnitMetric()中的ggplot(df, aes(x = month, y, color = Unit))替换为ggplot(df, aes(x = month, get(y), color = Unit)),并使用

plotUnitMetric(y = "witness", "MICU")

应该能帮你渡过难关
至于警告消息,我没有看到任何理由在你的用例中使用dplyr::group_by。如果警告困扰你,在gghighlight()中添加use_group_by = FALSE将删除它。
我的修改版本:

plotUnitMetric <- function(y, unitHighlight) {
  ggplot(df, aes(x = month, get(y), color = Unit)) +
    geom_line(linewidth = 0.5) +
    geom_point(shape = 21, fill = "white", stroke = 1.5, size = 2) +
    gghighlight(Unit == unitHighlight, use_direct_label = FALSE,
                use_group_by = FALSE) + 
    scale_x_date(date_breaks = "1 month", date_labels = "%b") +
    labs(x = "Month", y = "Percentage") +
    theme_bw() +
    theme(legend.position = "none")
}

unitWitness <- plotUnitMetric(y = "witness", "MICU") + 
  labs(title = "Percentage of CPAs that were witnessed by unit and month")

unitWitness

相关问题