R 如何组合填充和形状图例

R 如何组合填充和形状图例,r,ggplot2,R,Ggplot2,我有以下数据: df <- structure(list(x = c(94.6, 84.7, 95.5, 77.2, 86.5, 85.7, 91.6, 87.6, 86, 88.2, 88.8, 78.4, 95.3, 78.4, 79.2, 91.7, 86.9, 84.9, 84.2, 1, 89.9, 80.8, 95.6, 90.6, 77.1, 91.5, 85.2, 81.1, 87.1, 90.1, 89.5, 90.4, 71.5, 96, 90.8, 91.7,

我有以下数据:

df <- structure(list(x = c(94.6, 84.7, 95.5, 77.2, 86.5, 85.7, 91.6, 
87.6, 86, 88.2, 88.8, 78.4, 95.3, 78.4, 79.2, 91.7, 86.9, 84.9, 
84.2, 1, 89.9, 80.8, 95.6, 90.6, 77.1, 91.5, 85.2, 81.1, 87.1, 
90.1, 89.5, 90.4, 71.5, 96, 90.8, 91.7, 92, 78.1, 83.8, 93.2, 
95.7, 75.5, 85, 87.3, 83.6, 75.6, 90.3, 95, 79.3, 86.6, 95.6, 
79.5, 71.9), y = c(45.7, 54.8, 42.5, 57.3, 65.1, 40.4, 47.4, 
46.1, 38.8, 67.6, 70.2, 64.5, 66.6, 68.8, 76.9, 46, 69.7, 46.7, 
63.6, 51.4, 59.6, 72.9, 55.9, 60.9, 56.7, 30.3, 70.5, 65.5, 26.2, 
59.1, 32, 31.5, 43, 52, 44.5, 45.8, 54.5, 44, 47.8, 44.3, 49.1, 
68, 36.5, 56.1, 71.9, 39.2, 43.4, 59, 69.4, 35.1, 40.9, 69.9, 
43.4), facet = c(2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), is_active = structure(c(2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), method = c("method B", 
"method A", "method B", "method B", "method A", "method A", "method A", 
"method A", "method B", "method A", "method A", "method A", "method A", 
"method A", "method A", "method B", "method A", "method A", "method A", 
"method B", "method A", "method A", "method B", "method A", "method A", 
"method B", "method A", "method A", "method A", "method A", "method A", 
"method B", "method B", "method A", "method A", "method B", "method B", 
"method A", "method A", "method B", "method B", "method A", "method A", 
"method B", "method A", "method B", "method A", "method B", "method A", 
"method A", "method B", "method A", "method B")), class = "data.frame", row.names = c(NA, 
-53L))

到目前为止,一切都很好。但我也想添加一个
形状
美学:

df %>% 
  ggplot() + 
  geom_point(aes(x = x, y = y, fill = is_active, shape = method), stroke = 1, size = 3, colour = 'red') + 
  facet_wrap(~facet) +
  scale_fill_manual(values = c('1' = 'red', '0' = 'white')) +
  scale_shape_manual(values = c('method A' = 21, 'method B' = 24))

它开始变得混乱,因为
fill
美学不再有效,即使在
shape=21
上也是如此

我的最终目标是将这两个传说合并为一个,因此我遵循了中提供的解决方案 并尝试了以下方法:

df %>% 
  ggplot() + 
  geom_point(aes(x = x, y = y, fill = is_active, shape = method), stroke = 1, size = 3, colour = 'red') + 
  facet_wrap(~facet) +
  scale_fill_manual(name = 'Is active vs method',
                    labels = c('Active, method A', 'Inactive, method A', 'Active, method B', 'Inactive, method B'),
                    values = c('1' = 'red', '0' = 'white', '1' = 'red', '0' = 'white')) +
  scale_shape_manual(name = 'Is active vs method',
                     labels = c('Active, method A', 'Inactive, method A', 'Active, method B', 'Inactive, method B'),
                     values = c('method A' = 21, 'method A' = 21, 'method B' = 24, 'method B' = 24))
结果仍然是错误的:


如何将空点分配给方法A且活动==0,将填充点分配给方法A且活动==1,空的traingle到方法B和active==0,填充三角形到方法B和active==1,并为组合美学制作单个图例?

主要技巧是使用
交互
创建
是active
方法
的交叉因子。其余的
dplyr
管道
%%>%%
是装饰性的

df %>%
  mutate(is_active_desc = as.integer(as.character(is_active)) + 1,
         is_active_desc = c("Inactive", "Active")[is_active_desc],
         `Is active vs. method` = interaction(is_active_desc, method, sep = " ")) %>%
  select(-is_active_desc) %>%
  ggplot(aes(x = x, y = y, fill = `Is active vs. method`, shape = `Is active vs. method`)) +
  geom_point(colour = 'red', stroke = 1, size = 3) +
  facet_wrap(~facet) +
  scale_fill_manual(values = c('red', 'white', 'red', 'white')) +
  scale_shape_manual(values = c(21, 21, 24, 24))

df %>%
  mutate(is_active_desc = as.integer(as.character(is_active)) + 1,
         is_active_desc = c("Inactive", "Active")[is_active_desc],
         `Is active vs. method` = interaction(is_active_desc, method, sep = " ")) %>%
  select(-is_active_desc) %>%
  ggplot(aes(x = x, y = y, fill = `Is active vs. method`, shape = `Is active vs. method`)) +
  geom_point(colour = 'red', stroke = 1, size = 3) +
  facet_wrap(~facet) +
  scale_fill_manual(values = c('red', 'white', 'red', 'white')) +
  scale_shape_manual(values = c(21, 21, 24, 24))