R ggplot_构建的组列如何对应于原始因子水平?

R ggplot_构建的组列如何对应于原始因子水平?,r,ggplot2,R,Ggplot2,基于此,我有一个后续问题,在scale\u fill\u手册位中的以下代码行中: 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

基于此,我有一个后续问题,在
scale\u fill\u手册
位中的以下代码行中:

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
颜色值和图例标签是通过对
ggplot\u build
生成的数据框中
列的某种映射来确定的。我的问题与此映射的确定有关,尤其是当
列是从具有不完整因子水平的因子列派生而来时

例如:

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]))

我们看到
key
的值已映射到组号1-4。我的问题是,这个映射是如何完成的,以及如何从
tmp\u raw\u df
中的
列恢复关键或因子级别的原始值,即使您尝试使用
drop=FALSE
来保持因子完整性,当前版本的ggplot也会将其保留在图例显示中,但最终还是会在
网格
图形的最终数据构建中删除它们。可以使用手动填充值提供反向映射:

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
库(tidyverse)
种子(111)
数据帧(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()%>%
突变(键=因子(键,级别=字母[1:5])->tmp_df
因子_映射%
ggplot(aes(x=值,填充=键))+
几何图形直方图(binwidth=0.1,位置='stack')+
比例\填充\手动(下降=假,值=系数\映射)->p

tmp_raw_df我不能说映射是如何完成的(尽管我怀疑是通过重构
key
、删除未使用的级别并转换为整数),但我确信
ggplot\u build(p)$data
不再包含
key
的原始值。这些值现在应该存在于绘图的一个刻度中。谢谢,那么我们应该如何/在哪里检索这些值呢?很可能,我们应该进入绘图的
ggproto
对象之一,但是考虑到这些值是如何计算的,我不确定您是否能够(轻松地)检索这些值检索您要查找的内容。通常,因子在像@Fr.所说的那样重构后按字母顺序分配。因此,如果您想进行检查,可以手动创建一个柱状图或一个包含与
ggplot
中相同仓位的仓位的仓位,并将它们与
tmp\u raw\u df
中的非零仓位进行比较。您可能已经知道了这一点,但对于
因子
对象,您可以使用
levels()
检索值,然后将它们匹配起来。但是,数据在消失在
ggplot
中后基本上丢失。
> 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
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