ggplot_build的group列如何与原始因子级别相对应?

时间:2017-01-03 02:33:33

标签: r ggplot2

我有一个基于此answer的后续问题,在以下代码行的protected override void OnActivated(IActivatedEventArgs args) { base.OnActivated(args); if (args.Kind == ActivationKind.Protocol) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); rootFrame.Navigate(typeof(MainPage)); Window.Current.Content = rootFrame; } } Window.Current.Activate(); } 位中:

scale_fill_manual

使用ggplot(data = temp2, aes(x = x, y = y2, fill = group)) + geom_bar(width = 0.1, stat = "identity") + scale_fill_manual(name = "key", labels = c("a", "b", "c", "d", "e", "others"), values = c("#F8766D", "#A3A500", "#00BF7D", "#00B0F6", "#E76BF3", "#000000")) + labs(x = "value", y = "count") -> g2 生成的数据框中的group列的某些映射来确定颜色值和图例标签。我的问题是关于这种映射的确定,特别是当ggplot_build列来自具有不完整因子水平的因子列时。

例如:

group

现在创建堆积条形图并生成基础原始数据:

set.seed(111)
tmp_df <-  
    data.frame(a = rnorm(100, 0, 1),
               b = rnorm(100, 0.5, 1),
               c = rnorm(100, -0.5, 1),
               d = rnorm(100, 1, 1),
               e = rnorm(100, -1, 1)) %>%
    tidyr::gather() %>%
    mutate(key = factor(key, levels = letters[1:5]))

检查tmp_df %>% filter(key != "c") %>% ggplot(aes(x = value, fill = key)) + geom_histogram(binwidth = 0.1, position = 'stack') -> p tmp_raw_df <- ggplot_build(p)$data[[1]]

tmp_raw_df

我们看到> head(tmp_raw_df) fill y count x xmin xmax density ncount ndensity PANEL group ymin ymax colour size linetype alpha 1 #C77CFF 1 1 -4.2 -4.25 -4.15 0.1 0.125 1.25 1 4 0 1 NA 0.5 1 NA 2 #00BFC4 1 0 -4.2 -4.25 -4.15 0.0 0.000 0.00 1 3 1 1 NA 0.5 1 NA 3 #7CAE00 1 0 -4.2 -4.25 -4.15 0.0 0.000 0.00 1 2 1 1 NA 0.5 1 NA 4 #F8766D 1 0 -4.2 -4.25 -4.15 0.0 0.000 0.00 1 1 1 1 NA 0.5 1 NA 5 #C77CFF 0 0 -4.1 -4.15 -4.05 0.0 0.000 0.00 1 4 0 0 NA 0.5 1 NA 6 #00BFC4 0 0 -4.1 -4.15 -4.05 0.0 0.000 0.00 1 3 0 0 NA 0.5 1 NA 的值已映射到组编号1-4。我的问题是,如何完成此映射,以及如何从key中的group列恢复密钥或因子级别的原始值?

1 个答案:

答案 0 :(得分:1)

是的,即使您尝试使用drop=FALSE来保持因子完整性,ggplot的当前版本将保留图例显示的内容,但仍会最终删除它们以用于grid的最终数据构建画画。您可以使用手动填充值来提供反向映射:

library(tidyverse)

set.seed(111)

data.frame(a = rnorm(100, 0, 1),
           b = rnorm(100, 0.5, 1),
           c = rnorm(100, -0.5, 1),
           d = rnorm(100, 1, 1),
           e = rnorm(100, -1, 1)) %>%
  tidyr::gather() %>%
  mutate(key = factor(key, levels = letters[1:5])) -> tmp_df

factor_map <- c(a="#111111", b="#222222", c="#333333", d="#444444", e="#555555")
rev_map <- setNames(names(factor_map), unname(factor_map))

tmp_df %>%
  filter(key != "c") %>%
  ggplot(aes(x = value, fill = key)) +
  geom_histogram(binwidth = 0.1, position = 'stack') +
  scale_fill_manual(drop=FALSE, values=factor_map) -> p

tmp_raw_df <- tbl_df(ggplot_build(p)$data[[1]])

tmp_raw_df <- mutate(tmp_raw_df, orig_factor=rev_map[fill])

distinct(tmp_raw_df, fill, group, orig_factor)
## # A tibble: 4 × 3
##      fill group orig_factor
##     <chr> <int>       <chr>
## 1 #555555     4           e
## 2 #444444     3           d
## 3 #222222     2           b
## 4 #111111     1           a