geom_bar、geom_point 基于列值的条件颜色

geom_bar, geom_point conditional colours based on column value

我早些时候问了一个相关问题,感谢我收到的帮助,我快到了,但在最后一个障碍上挣扎...

我想根据列中的值为条形和点分配条件颜色(如果 diff_1 >= ci 则为绿色,如果 diff_1 <= ci 则为红色).但是,到目前为止,我的代码只显示了正确的条形图。应该有一些有值但该值不大于或小于 CI 的柱。理想情况下,这些应该是灰色的。点颜色似乎工作正常。

正如您从图表中看到的那样,它几乎已经存在,但缺少未分配颜色的条。

chart

不胜感激,谢谢。

亚当

我的代码如下所示:

Ex$diff_ci1 <- factor(Ex$diff_ci1, levels = c("Red", "Green"))
Ex$diff_ci2 <- factor(Ex$diff_ci2, levels = c("Red", "Green"))

    Ex %>% 
      ggplot(aes(x = diff_1, fill = diff_ci1, y = ques)) +
      geom_bar(stat = 'identity', alpha = .6, width = .2)+
      scale_fill_manual(values=c("red", "green"))+
      geom_point(aes(x = diff_sect, colour = diff_ci2, y = ques, size = 3))+
      geom_vline(xintercept = 0)+
      theme_bw()+
      theme(legend.position = "none")+
      facet_grid(~cah, labeller=label_wrap_gen(width=10))```



Here is my data:

    ```structure(list(school = c("School1", "School1", "School1", "School1", 
    "School1", "School1", "School1", "School1", "School1"), cah = c("bio", 
    "bio", "bio", "geo", "geo", "geo", "eng", "eng", "eng"), ques = c("q1", 
    "q2", "q3", "q1", "q2", "q3", "q1", "q2", "q3"), diff_1 = c(8.68, 
    16.07, -4.68, 6.05, 1.72, -10.74, 13.22, 13.8, 15.77), ci = c(14.8872687, 
    14.8872687, 14.8872687, 4.715027198, 4.715027198, 4.715027198, 
    7.559941135, 7.559941135, 7.559941135), diff_ci1 = structure(c(NA, 
    2L, NA, 2L, NA, 1L, 2L, 2L, 2L), .Label = c("Red", "Green"), class = "factor"), 
        diff_sect = c(5.95, 0.79, -14.76, 2.04, -15.55, -20.87, 4.34, 
        -3.93, 5.99), diff_ci2 = structure(c(NA, NA, NA, NA, 1L, 
        1L, NA, NA, NA), .Label = c("Red", "Green"), class = "factor")), row.names = c(NA, 
    -9L), spec = structure(list(cols = list(school = structure(list(), class = c("collector_character", 
    "collector")), cah = structure(list(), class = c("collector_character", 
    "collector")), ques = structure(list(), class = c("collector_character", 
    "collector")), diff_1 = structure(list(), class = c("collector_double", 
    "collector")), ci = structure(list(), class = c("collector_double", 
    "collector")), diff_ci1 = structure(list(), class = c("collector_character", 
    "collector")), diff_sect = structure(list(), class = c("collector_double", 
    "collector")), diff_ci2 = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df", 
    "tbl_df", "tbl", "data.frame"))```

 

如果您注释掉 scale_fill_manual 行,您会得到灰色条。