根据全局环境中的值手动设置组颜色

时间:2019-05-13 03:27:52

标签: r ggplot2 fill

我有一个数据集,我想在使用ggplot时设置特定的颜色。

我看过this question,可以手动设置值的颜色。但是,我希望能够基于全局环境中的值设置颜色。

示例数据:

library(tidyverse)

df <- tibble(place = c("City", "AAAAA", "ZZZZZ", "City", "AAAAA", "ZZZZZ", "City",
                 "AAAAA", "ZZZZZ"),
             x = c(1, 2, 3, 4, 1, 2, 3, 4, 1),
             y = c(0.475308283, 0.437369818, 0.204992979, 0.263934572,
                 0.671616954, 0.955005667, 0.048954328, 0.900494188,
                 0.418262936))

这有效:

group_cols <- c("City" = "blue", "ZZZZZ" = "red", "AAAAA" = "green")

place2 <- "AAAAA"

df2 <- df %>% 
  filter(place %in% c("City", place2))

ggplot(df2, aes(x, y, fill = place)) +
  geom_col() + scale_fill_manual(values=group_cols)

enter image description here

但是我想这样做:

group_cols2 <- c("City" = "blue", place2 = "green")

place2 <- "AAAAA"

df2 <- df %>% 
  filter(place %in% c("City", place2))

ggplot(df2, aes(x, y, fill = place)) +
  geom_col() + scale_fill_manual(values=group_cols2)

使用它-'place2'变量不会显示为已填充。我用“ AAAAA”和“ ZZZZZ”表示我无法按顺序上色,因为有时可能在“城市”之前,有时在它之后。

enter image description here

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

这是因为在place2中生成的place2 <- 'AAAAA'对象不是df2$place级别。请注意,另一个转换是必要的。

library(tidyverse)

df <- tibble(place = c("City", "AAAAA", "ZZZZZ", "City", "AAAAA", "ZZZZZ", "City",
                       "AAAAA", "ZZZZZ"),
             x = c(1, 2, 3, 4, 1, 2, 3, 4, 1),
             y = c(0.475308283, 0.437369818, 0.204992979, 0.263934572,
                   0.671616954, 0.955005667, 0.048954328, 0.900494188,
                   0.418262936))
df2 <- df %>% 
  filter(place %in% c("City", place2)) 

levels(df2$place)
> levels(df2$place)
NULL

所以:

df2 <- df %>% 
  filter(place %in% c("City", place2)) %>% 
  dplyr::mutate(place = as.factor(place))
df2
> df2
# A tibble: 6 x 3
  place     x      y
  <fct> <dbl>  <dbl>
1 City      1 0.475 
2 AAAAA     2 0.437 
3 City      4 0.264 
4 AAAAA     1 0.672 
5 City      3 0.0490
6 AAAAA     4 0.900 
ggplot(df2, aes(x, y, fill = place)) +
  geom_col()

enter image description here

group_cols2 <- c("City" = "blue", place2 = "green")
place2 <- "AAAAA"
ggplot(df2, aes(x, y, fill = place)) +
  geom_col() + scale_fill_manual(values=group_cols2)

enter image description here

要解决此问题,最简单的两种方法是:

  1. 像以前一样更改色阶。
group_cols2 <- c("City" = "blue", "AAAAA" = "green")
ggplot(df2, aes(x, y, fill = place)) +
  geom_col() + scale_fill_manual(values=group_cols2)

enter image description here

  1. 更改数据框中的信息。
group_cols2 <- c("City" = "blue", place2 = "green")
df2 <- df %>% 
  filter(place %in% c("City", place2)) %>% 
  dplyr::mutate(place = as.factor(place), 
                place = dplyr::case_when(place == "AAAAA" ~ "place2",
                                         place == "City" ~ "City"),
                place = forcats::fct_relevel(place, "place2"))

ggplot(df2, aes(x, y, fill = place)) +
  geom_col() + scale_fill_manual(values=group_cols2)

enter image description here

答案 1 :(得分:1)

这有效

var arr = [{car:"Honda", model:["a","b","c"]},
       {car:"Nissan", model:["a","b","c"]},
       {car:"Chevrolet", model:["a","b","c"]},
      ]
      
const res = arr.flatMap(x => x.model);

console.log(res)