首页 > 解决方案 > R geom_bar 条件红绿为正/负

问题描述

第一篇在这里发帖并且是 R 的新手,因此为未能遵循任何约定而道歉...
我正在尝试创建组合条/点以显示与基准的差异并突出显示红色,其中值小于 ref col 和 Green 中的值价值大于价值。差异已经解决,我正在使用参考列来确定填充颜色。

这行不通。1)因为颜色是错误的(例如,我的数据中的绿色导致我的栏中的红色。2)颜色不一致。在 a1 条中,我有红色和绿色条,但在 b1 中,因为我只有负值,所以颜色会发生变化,并且 a1 中的红色填充正值,在 b1 中填充负值。

我真正想知道的是如何在下面重写我的代码,以确保红色为负值,绿色为正值。如果您能告诉我如何调整我的代码来做到这一点,我将不胜感激!

谢谢,
亚当

这是a1

使用数据和代码的 a1 条形图

这是b1

使用数据和代码的 b1 条形图

test <- (Test)
a1 <- filter(test, school =="a1")
a1 %>% 
  ggplot(aes(x = diff, fill = diff_col, y = ques)) +
  geom_bar(stat = 'identity', alpha = .6, width = .2)+
  geom_point(aes(x = diff_sect, colour = sect_diff_col, y = ques, size = 3))+
  geom_vline(xintercept = 0)+
  theme_bw()+
  theme(legend.position = "none")+
  facet_grid(~cah, labeller=label_wrap_gen(width=10))

b1 <- filter(test, school =="b1")

b1 %>% 
  ggplot (aes(x = diff, fill = diff_col, y = ques)) +
  geom_bar(stat = 'identity', alpha = .6, width = .2)+
  geom_point(aes(x = diff_sect, colour = sect_diff_col, y = ques, size = 3))+
  geom_vline(xintercept = 0)+
  theme_bw()+
  theme(legend.position = "none")+
  facet_grid(~cah, labeller=label_wrap_gen(width=10))

我正在使用的数据:

structure(list(school = c("a1", "a1", "a1", "a1", "a1", "a1", 
"b1", "b1", "b1", "b1", "b1", "b1"), cah = c("aa", "aa", "aa", 
"bb", "bb", "bb", "cc", "cc", "cc", "dd", "dd", "dd"), ques = c("q1", 
"q2", "q3", "q1", "q2", "q3", "q1", "q2", "q3", "q1", "q2", "q3"
), diff = c(12, 3, -10, 22, 33, 20, -12, -21, -15, -5, -6, -7
), ci = c(11, 11, 11, 7, 7, 7, 6, 6, 6, 4, 4, 4), diff_col = c("Green", 
"Red", "Red", "Green", "Green", "Green", "Red", "Red", "Red", 
"Red", "Red", "Red"), diff_sect = c(6, -8, -17, 18, 19, 12, -15, 
-29, -19, 3, 5, 5), sect_diff_col = c("Red", "Red", "Red", "Green", 
"Green", "Green", "Red", "Red", "Red", "Red", "Green", "Green"
)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-12L), 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 = structure(list(), class = c("collector_double", 
"collector")), ci = structure(list(), class = c("collector_double", 
"collector")), diff_col = structure(list(), class = c("collector_character", 
"collector")), diff_sect = structure(list(), class = c("collector_double", 
"collector")), sect_diff_col = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))

标签: rggplot2geom-bar

解决方案


看起来您希望您的颜色直接来自sect_differ_col变量中的文本(“Red”,“Green”,...)。为此,您可以使用该tolower函数将“红色”和“绿色”转换为 R 颜色名称“红色”和“绿色”。然后将色阶设置为scale_colour_identity()。例如:

a1 %>% 
  ggplot(aes(x = diff, fill = diff_col, y = ques)) +
  geom_bar(stat = 'identity', alpha = .6, width = .2)+
  geom_point(aes(x = diff_sect, colour = tolower(sect_diff_col), y = ques, size = 3))+
  geom_vline(xintercept = 0)+
  theme_bw()+
  theme(legend.position = "none")+
  facet_grid(~cah, labeller=label_wrap_gen(width=10)) + 
  scale_colour_identity()

推荐阅读