R-如何将双变量图例添加到ggplot2图表中?

R-如何将双变量图例添加到ggplot2图表中?,r,ggplot2,legend,R,Ggplot2,Legend,我正试图在我的ggplot2图表中添加一个双变量图例,但我不知道(a)通过一些指南选项是否可行,以及(b)如何实现它 我能够产生接近预期结果的唯一方法是专门创建一个类似于图例的新图表(下面名为p.legend),并通过cowplot包将其插入原始图表的某个地方(下面名为p.chart)。但肯定有比这更好的方法,因为这种方法首先需要创建图例,并调整其大小/位置,以适应原始图表 下面是我的方法的一个虚拟示例的代码: library(tidyverse) # Create Dummy Data #

我正试图在我的
ggplot2
图表中添加一个双变量图例,但我不知道(a)通过一些
指南
选项是否可行,以及(b)如何实现它

我能够产生接近预期结果的唯一方法是专门创建一个类似于图例的新图表(下面名为
p.legend
),并通过
cowplot
包将其插入原始图表的某个地方(下面名为
p.chart
)。但肯定有比这更好的方法,因为这种方法首先需要创建图例,并调整其大小/位置,以适应原始图表

下面是我的方法的一个虚拟示例的代码:

library(tidyverse)

# Create Dummy Data #
set.seed(876)
n <- 2
df <- expand.grid(Area = LETTERS[1:n],
                  Period = c("Summer", "Winter"),
                  stringsAsFactors = FALSE) %>% 
      mutate(Objective = runif(2 * n, min = 0, max = 2),
             Performance = runif(2 * n) * Objective) %>% 
      gather(Type, Value, Objective:Performance)

# Original chart without legend #
p.chart <- df %>% 
            ggplot(., aes(x = Area)) + 
              geom_col(data = . %>% filter(Type == "Objective"),
                       aes(y = Value, fill = Period),
                       position = "dodge", width = 0.7, alpha = 0.6) + 
              geom_col(data = . %>% filter(Type == "Performance"),
                       aes(y = Value, fill = Period),
                       position = "dodge", width = 0.7) + 
              scale_fill_manual(values = c("Summer" = "#ff7f00", "Winter" = "#1f78b4"), guide = FALSE) + 
              theme_minimal() + 
              theme(panel.grid.major.x = element_blank(),
                    panel.grid.minor.y = element_blank())

# Create a chart resembling a legend #
p.legend <- expand.grid(Period = c("Summer", "Winter"),
                        Type   = c("Objective", "Performance"),
                        stringsAsFactors = FALSE) %>% 
              ggplot(., aes(x = Period, y = factor(Type, levels = c("Performance", "Objective")),
                                                   fill = Period, alpha = Type)) + 
                geom_tile() + 
                scale_fill_manual(values = c("Summer" = "#ff7f00", "Winter" = "#1f78b4"), guide = FALSE) + 
                scale_alpha_manual(values = c("Objective" = 0.7, "Performance" = 1), guide = FALSE) + 
                ggtitle("Legend") + 
                theme_minimal() + 
                theme(plot.title = element_text(hjust = 0.5),
                      rect = element_rect(fill = "transparent"),
                      axis.title = element_blank(),
                      panel.grid.major = element_blank())

# Add legend to original chart #
p.final <- cowplot::ggdraw() + 
                  cowplot::draw_plot(plot = p.chart) + 
                  cowplot::draw_plot(plot = p.legend, x = 0.5, y = 0.65, width = 0.4, height = 0.28, scale = 0.7)

# Save chart #
cowplot::ggsave("Bivariate Legend.png", p.final, width = 8, height = 6, dpi = 500)
库(tidyverse)
#创建虚拟数据#
结实的种子(876)
n%
收集(类型、价值、目标:绩效)
#没有图例的原始图表#
p、 图表%
ggplot(,aes(x=面积))+
geom_col(数据=.%>%过滤器(类型=“目标”),
aes(y=值,填充=周期),
位置=“减淡”,宽度=0.7,α=0.6)+
geom_col(数据=.%>%过滤器(类型=“性能”),
aes(y=值,填充=周期),
位置=“减淡”,宽度=0.7+
刻度填充手册(数值=c(“夏季”=“#ff7f00”,“冬季”=“#1f78b4”),指南=FALSE)+
theme_minimal()+
主题(panel.grid.major.x=element_blank(),
panel.grid.minor.y=元素_blank()
#创建一个类似于图例的图表#
p、 图例%
ggplot(,aes(x=周期,y=系数(类型,等级=c(“绩效”,“目标”))),
填充=周期,alpha=类型))+
geom_tile()+
刻度填充手册(数值=c(“夏季”=“#ff7f00”,“冬季”=“#1f78b4”),指南=FALSE)+
量表α手册(值=c(“目标”=0.7,“性能”=1),指南=FALSE)+
ggtitle(“图例”)+
theme_minimal()+
主题(plot.title=element\u text(hjust=0.5),
rect=元素(fill=“transparent”),
axis.title=元素_blank(),
panel.grid.major=元素_blank()
#将图例添加到原始图表中#

p、 final这可能在某个时候起作用,但现在颜色框似乎忽略了所有的中断、名称和标签(@ClausWilke?)。可能是因为
multiscales
软件包处于真正的早期阶段

发布,因为它可能会工作时,未来的读者在这里

library(multiscales)

df %>% 
  mutate(
    period = as.numeric(factor(Period)),
    type = as.numeric(factor(Type))
  ) %>% 
  ggplot(., aes(x = Area, y = Value, fill = zip(period, type), group = interaction(Area, Period))) + 
  geom_col(width = 0.7, position = 'dodge') + 
  bivariate_scale(
    "fill", 
    pal_hue_sat(c(0.07, 0.6), c(0.4, 0.8)),
    guide = guide_colorbox(
      nbin = 2, 
      name = c("Period", "Type"),                       #ignored
      breaks = list(1:2, 1:2),                          #ignored
      labels = list(levels(.$Period), levels(.$Type))   #ignored
  )

你看过这个吗哇!我没有看到,没有。从我在那里看到的情况来看,我们都在使用“创建一个图表,看起来像一个图例,并将其添加到原始图表”的方法。但是有没有一种方法可以直接通过
ggplot2
(特别是因为这是一篇3年前的文章,从那时起功能可能已经改进了)?你可以通过感谢@Axeman来实现这一点。我会试试看,然后回来汇报。