如何消除我在 R 中为 Cowplot 创建的两个图例之间尴尬的垂直间隙?

How to remove awkward vertical gap between the two legends I created for Cowplot in R?

我试图创建两个单独的图例,因为我想以不同的方式对图例进行分组。但是,这两个传说之间存在着尴尬的差距。我想缩小两个图例之间尴尬的垂直差距(如图所示)。我该怎么做呢?

谢谢。

library(cowplot)

data2 <- tibble::tribble(
  ~Color,    ~Item, ~Count,       ~Year,
  "Blue",    "Bag",     50, "2009-2011",
  "Blue", "Wallet",     60, "2009-2011",
  "Green",  "Shoes",     80, "2009-2011",
  "Green",  "Shirt",     90, "2009-2011",
  "Yellow", "Flower",     20, "2009-2011",
  "Yellow",   "Bees",     30, "2009-2011",
  "Blue",    "Bag",     50, "2009-2012",
  "Blue", "Wallet",     60, "2009-2012",
  "Green",  "Shoes",     90, "2009-2012",
  "Green",  "Shirt",     20, "2009-2012",
  "Yellow", "Flower",     10, "2009-2012",
  "Yellow",   "Bees",      5, "2009-2012"
)

palette1 <- c("#4575B4","#74ADD1","#ABD9E9","#800026","#FC4E2A","#FEB24C")
palette2 <- c("#800026","#FC4E2A","#FEB24C")

data2$Count_final <- with(data2, Count * c(1, -1)[(Color == "Yellow") + 1])


full_plot <- ggplot(data=data2)+ 
    geom_bar(aes(x=Year,y=Count_final,fill=Item),stat="identity",position="identity") +
    scale_y_continuous(labels=abs)+
    scale_fill_manual(values=palette1)+
    theme_bw(base_size=18) +
    ylab("Count")+
    theme(legend.position="none")


Legend1 <- 
    data2 %>%
    filter(Item %in% c("Bag","Wallet","Shoes")) %>%
    ggplot(aes(x=Year,fill=Item))+geom_bar()+
    scale_fill_manual(values=palette1,name="Accessories")

Legend2 <- 
    data2 %>%
    filter(Item %in% c("Shirt","Flower","Bees")) %>%
    ggplot(aes(x=Year,fill=Item))+geom_bar()+
    scale_fill_manual(values=palette2,name="Not Accessories")


plot_grid(
    full_plot
    , plot_grid(
      get_legend(Legend1)
      , get_legend(Legend2)
      , nrow = 2
    )
    , nrow = 1
    , rel_heights = c(1,0)

  )



这可能不是您想要的。您可能需要使用其他方法来获得您喜欢的颜色。这只是解决您的问题的一种方法。希望能帮助到你。

library(ggplot2)
library(cowplot)
data2 <- tibble::tribble(
  ~Color,    ~Item, ~Count,       ~Year,
  "Blue",    "Bag",     50, "2009-2011",
  "Blue", "Wallet",     60, "2009-2011",
  "Green",  "Shoes",     80, "2009-2011",
  "Green",  "Shirt",     90, "2009-2011",
  "Yellow", "Flower",     20, "2009-2011",
  "Yellow",   "Bees",     30, "2009-2011",
  "Blue",    "Bag",     50, "2009-2012",
  "Blue", "Wallet",     60, "2009-2012",
  "Green",  "Shoes",     90, "2009-2012",
  "Green",  "Shirt",     20, "2009-2012",
  "Yellow", "Flower",     10, "2009-2012",
  "Yellow",   "Bees",      5, "2009-2012"
)

data2$Count_final <- with(data2, Count * c(1, -1)[(Color == "Yellow") + 1])

data2$Item = factor(data2$Item, 
                    levels=c("Accessories","Bag","Bees", "Flower", " ",
                             "Non-accessories", "Shirt","Shoes","Wallet"))

full_plot <- ggplot(data=data2)+ 
  geom_bar(aes(x=Year,y=Count_final,fill=Item),stat="identity",position="identity") +
  scale_y_continuous(labels=abs)+
  theme_bw(base_size=18) +
  ylab("Count")

full_plot + 
  scale_fill_manual(values=c("white", "#4575B4","#74ADD1","#ABD9E9", 
                             "white","white", "#800026","#FC4E2A","#FEB24C"),
                    drop=FALSE)+
  theme(legend.position="right", 
        legend.title=element_blank())

reprex package (v0.3.0)

于 2020-01-06 创建

编辑

我认为在这里对您的问题进行解释比在评论框中更容易。此方法使用一组 ggplot 图例标签。通过添加空格和新标签,它看起来像两组图例。首先,我们向变量 term 添加 3 个新级别:"Accessories"、" "、"Non-accessories"。按以下顺序:

data2$Item = factor(data2$Item, 
                    levels=c("Accessories","Bag","Bees", "Flower", " ",
                             "Non-accessories", "Shirt","Shoes","Wallet"))

然后,在 ggplot 中,为这 3 个级别分配空格。因为这些级别没有实际值,所以默认情况下会删除它们。所以我们使用 drop = FALSE 来保留它们。

您会发现几个现有的软件包可以帮助您交替解决问题。我觉得这种方法更直观。

使用此 plot_grid 调用:

Leg <- plot_grid(get_legend(Legend1), get_legend(Legend2), nrow = 4, align = "hv")
plot_grid(full_plot, Leg)