Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何根据条件向变量添加颜色_R_Ggplot2 - Fatal编程技术网

R 如何根据条件向变量添加颜色

R 如何根据条件向变量添加颜色,r,ggplot2,R,Ggplot2,图书馆(GG2) 如何根据条件更改变量的颜色 预期产量 我的输出 使用teunbrand评论中提到的问题的数据: x %>% mutate(status = factor(status, levels = c("Open", "Closed"))) %>% group_by(Type, status) %>% summarise(n = sum(Total)) %>% add_count(Type, wt =

图书馆(GG2)

如何根据条件更改变量的颜色

预期产量

我的输出


使用teunbrand评论中提到的问题的数据:

  x %>%
  mutate(status = factor(status, levels = c("Open", "Closed"))) %>%
  group_by(Type, status) %>%
  summarise(n = sum(Total)) %>%
  add_count(Type, wt = n, name = "total") %>%
  ggplot(aes(n, Type)) +
  geom_col(aes(fill = status)) +
  geom_text(
    aes(label = n, color = status), 
    position = ggplot2::position_stack(vjust = 0.5),
    show.legend = FALSE,
    fontface = "bold"
  ) +
  geom_text(aes(label = total, x = total), hjust = -0.1) +
  scale_fill_manual(NULL, values = c(Closed = "#284a8d", Open = "#00B5CE")) +
  scale_color_manual(NULL, values = c(Closed = "white", Open = "black")) +
  scale_x_continuous(expand = c(0,0), limits = c(0, 650)) +
  theme_classic() +
  labs(x = NULL, y = NULL) +
  theme(
    legend.position = "bottom",
    axis.ticks = element_blank()
  ) +
  guides(fill = guide_legend(reverse = TRUE))

不同的问题,但根据图像判断相关的数据结构(人们可能会使用,因为这个问题不包括数据):你能告诉我如何根据条件@teunbrand改变单元格的颜色吗?“guides(fill=guide\u legend(reverse=TRUE)”中的意外符号获取此错误
  x %>%
  mutate(status = factor(status, levels = c("Open", "Closed"))) %>%
  group_by(Type, status) %>%
  summarise(n = sum(Total)) %>%
  add_count(Type, wt = n, name = "total") %>%
  ggplot(aes(n, Type)) +
  geom_col(aes(fill = status)) +
  geom_text(
    aes(label = n, color = status), 
    position = ggplot2::position_stack(vjust = 0.5),
    show.legend = FALSE,
    fontface = "bold"
  ) +
  geom_text(aes(label = total, x = total), hjust = -0.1) +
  scale_fill_manual(NULL, values = c(Closed = "#284a8d", Open = "#00B5CE")) +
  scale_color_manual(NULL, values = c(Closed = "white", Open = "black")) +
  scale_x_continuous(expand = c(0,0), limits = c(0, 650)) +
  theme_classic() +
  labs(x = NULL, y = NULL) +
  theme(
    legend.position = "bottom",
    axis.ticks = element_blank()
  ) +
  guides(fill = guide_legend(reverse = TRUE))