首页 > 解决方案 > if (is.double(data$x) && !has_groups(data) && any(data$x != data$x[1L])) { 中的错误:需要 TRUE/FALSE 的缺失值

问题描述

我正在尝试使用 ggplot,并希望创建一个箱线图,它在 x 轴上有四个类别,用于悬挂数据(低、低、高、高)和 y 轴上的农场。

认为将悬挂柱分成四组。但是 ggplot 对我很不高兴。这是错误:

```
Error in if (is.double(data$x) && !has_groups(data) && any(data$x != data$x[1L])) { : missing value where TRUE/FALSE needed
```

这是我的代码:

```{r}
# To break suspension_rate_total_pct data into groups for clearer visualization, I found the min, and max
merged_data$suspension_rate_total_pct <-
  as.numeric(merged_data$suspension_rate_total_pct)
max(merged_data$suspension_rate_total_pct, na.rm=TRUE)
min(merged_data$suspension_rate_total_pct, na.rm=TRUE)
low_suspension <- merged_data$suspension_rate_total_pct > 0 & merged_data$suspension_rate_total_pct < 0.5
low_ish_suspension <- merged_data$suspension_rate_total_pct > 0.5 & merged_data$suspension_rate_total_pct < 1
high_ish_suspension <- merged_data$suspension_rate_total_pct > 1 & merged_data$suspension_rate_total_pct < 1.5
high_suspension <- merged_data$suspension_rate_total_pct > 1.5 & merged_data$suspension_rate_total_pct < 2

ggplot(merged_data, aes(x = suspension_rate_total_pct , y = farms_pct)) +
        geom_boxplot()
```

这是数据:

    merged_data <- structure(list(schid = c("1030642", "1030766", "1030774", "1030840", 
    "1130103", "1230150"), enrollment = c(159, 333, 352, 430, 102, 
    193), farms = c(132, 116, 348, 406, 68, 130), foster = c(2, 0, 
    1, 8, 1, 4), homeless = c(14, 0, 8, 4, 1, 4), migrant = c(0, 
    0, 0, 0, 0, 0), ell = c(18, 12, 114, 45, 7, 4), suspension_rate_total = c(NA, 
    20, 0, 0, 95, 5), suspension_violent = c(NA, 9, 0, 0, 20, 2), 
        suspension_violent_no_injury = c(NA, 6, 0, 0, 47, 1), suspension_weapon = c(NA, 
        0, 0, 0, 8, 0), suspension_drug = c(NA, 0, 0, 0, 9, 1), suspension_defiance = c(NA, 
        1, 0, 0, 9, 1), suspension_other = c(NA, 4, 0, 0, 2, 0), 
        farms_pct = c(0.830188679245283, 0.348348348348348, 0.988636363636364, 
        0.944186046511628, 0.666666666666667, 0.673575129533679), 
        foster_pct = c(0.0125786163522013, 0, 0.00284090909090909, 
        0.0186046511627907, 0.00980392156862745, 0.0207253886010363
        ), migrant_pct = c(0, 0, 0, 0, 0, 0), ell_pct = c(0.113207547169811, 
        0.036036036036036, 0.323863636363636, 0.104651162790698, 
        0.0686274509803922, 0.0207253886010363), homeless_pct = c(0.0880503144654088, 
        0, 0.0227272727272727, 0.00930232558139535, 0.00980392156862745, 
        0.0207253886010363), suspension_rate_total_pct = c(NA, 2, 
        1, 1, 2, 2)), row.names = c(NA, -6L), class = c("tbl_df", 
    "tbl", "data.frame"))

如果可以的话,请帮助我安抚ggplot,这样它会给我带来漂亮的可视化效果。目前,这感觉就像一段单方面的情感过山车。

标签: r

解决方案


我认为您可以使用cut()您的数据将其划分为 4 组。然后您可以将该变量与绘图一起使用

merged_data <- transform(merged_data, 
          group = cut(
            suspension_rate_total_pct, 
            c(0, .5, 1, 1.5, 2), 
            include.lowest = TRUE,
            labels = c("low", "lowish", "highish", "high")))

ggplot(merged_data, aes(x = group , y = farms_pct)) +
  geom_boxplot()

推荐阅读