我想在图例中添加一个变量而不将其包括在图中。
我认为当我不使用alpha
时也不会出现问题(请参阅:How do I add a variable to the legend without including it in the graph?)
library(tidyverse)
name_color <- c('black', "blue", "orange", "pink")
names(name_color) <- letters[1:4]
tibble(name = rep(letters[1:4], each = 2),
respond = rep(c("yes", "no"), 4),
n = rep(50, 8),
me = "i") %>%
filter(name != "c") %>%
ggplot(aes(me, n, fill = name, alpha = respond)) +
facet_wrap(~name) +
geom_bar(stat = "identity") +
scale_fill_manual(values = name_color, drop = FALSE)
答案 0 :(得分:1)
该问题与void switchArray(int array2D[][4], int numRow, int numCol){
for(int i=0; i < numRow; i++){
switchRow(array2D[i], COL);
}
}
void switchRow(int arr[], int length){
if (length == 0)
return;
if (arr[0] == 1)
arr[0] = 2;
else
arr[0] = 1;
switchRow(++arr, length - 1);
}
无关。问题在于您的数据类别。当您使用alpha
创建数据时,tibble
列属于name
类。您需要一个character
类来“记住”未使用的级别:
factor
在您链接的第一个问题中,您明确地进行了因子转换,这就是它起作用的原因。
name_color <- c('black', "blue", "orange", "pink")
names(name_color) <- letters[1:4]
d = tibble(name = rep(letters[1:4], each = 2),
respond = rep(c("yes", "no"), 4),
n = rep(50, 8),
me = "i") %>%
class(d$name)
# [1] "character"
d %>% mutate(name = factor(name)) %>%
filter(name != "c") %>%
ggplot(aes(me, n, fill = name, alpha = respond)) +
facet_wrap(~name) +
geom_bar(stat = "identity") +
scale_fill_manual(values = name_color, drop = FALSE)