首页 > 解决方案 > 如何消除我在 R 中为 Cowplot 创建的两个图例之间的尴尬垂直间隙?

问题描述

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

谢谢你。

图片在这里

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)

  )



标签: rlegendcowplot

解决方案


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

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 包(v0.3.0)于 2020-01-06 创建

编辑

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

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

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

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


推荐阅读