删除ggplot图例中的重复标签

标签 r ggplot2 legend

我想使用交互在图例中创建一个包含子组的图。我创建了一些零值的虚假数据来创建副标题。

我的样本数据:

library(ggplot2)
df <- data.frame(value = runif(27, 0, 1),
                 x = rep(1:3, each = 3),
                 group1 = rep(c(letters[1:3])),
                 group2 = rep(c("fake", "X", "Y"), each = 9))
# introducing fake data
df[df$group2 == "fake", "value"] <- 0
df$group2 <- relevel(df$group2, ref = "fake")

我几乎能够创建所需的情节:

# labels
lbl1 <- c(expression(bold("HEADING 1")), paste("LABEL 1", 1:2),
          expression(bold("HEADING 2")), paste("LABEL 2", 1:2),
          expression(bold("HEADING 3")), paste("LABEL 3", 1:2))
fills <- c("white", "red1", "red3",
           "white", "blue1", "blue3",
           "white", "green1", "green3")
colo <- c("white", "black", "black",
          "white", "black", "black",
          "white", "black", "black")

ggplot(df, aes(x = x, y = value,
               fill = interaction(group2, group1), 
               colour = interaction(group2, group1))) +
  geom_col(position = "dodge") +
  scale_fill_manual("", values = fills, label = lbl1)  +
  scale_colour_manual("", values = colo, label = lbl1)

enter image description here

但是,我不想 LABEL 1 1LABEL 1 2等子组中,我只想有 LABEL 1LABEL 2

在指定此类标签时,我无法创建这样的图:

lbl2 <- c(expression(bold("HEADING 1")), paste("LABEL", 1:2),
          expression(bold("HEADING 2")), paste("LABEL", 1:2),
          expression(bold("HEADING 3")), paste("LABEL", 1:2))

ggplot(df, aes(x = x, y = value,
               fill = interaction(group2, group1), 
               colour = interaction(group2, group1))) +
  geom_col(position = "dodge") +
  scale_fill_manual("", values = fills, label = lbl2)  +
  scale_colour_manual("", values = colo, label = lbl2)

enter image description here

明确地说,这是所需的输出:

enter image description here

最佳答案

您有非唯一标签,因为您引入了一个假组来分隔绘图上的列。 根据discrete_scale帮助:

A character vector giving labels (must be same length as breaks)

缩放代码可能对字符向量调用unique,最终得到的向量的标签数少于导致奇怪图例的中断数。

看来您应该使用方面来获得类似的结果,而不是 hack(假组):

# Remove the "fake" group 2
df1 <- df[df$group2 != "fake",]

ggplot(df1, aes(x = group1, y = value, fill=group2)) +
    geom_col(position = "dodge", color ="black")+
    scale_fill_manual( values =c("red","green"),labels = c("Lable 1", "Lable 2")) +
    scale_x_discrete(labels =c("H1", "H2", "H3")) +
    facet_wrap(x~., strip.position = "bottom")

enter image description here

如果您确实想要精确的绘图,则必须使用更多技巧来更改修改 gtable 的标签:

library(grid)

fills <- c("white", "red1", "red3",
           "white", "blue1", "blue3",
           "white", "green1", "green3")
# You need the lbl1 in order to get the legend correct placed since it needs
# size of labels.
lbl1 <- c(expression(bold("HEADING 1")), paste("LABEL 1", 1:2),
          expression(bold("HEADING 2")), paste("LABEL 2", 1:2),
          expression(bold("HEADING 3")), paste("LABEL 3", 1:2))
lbl2 <- c(expression(bold("HEADING 1")), paste("LABEL", 1:2),
          expression(bold("HEADING 2")), paste("LABEL", 1:2),
          expression(bold("HEADING 3")), paste("LABEL", 1:2))
 colo <- c("white", "black", "black",
         "white", "black", "black",
         "white", "black", "black")



p <- ggplot(df, aes(x = x, y = value,
                fill = interaction(group2, group1),
                color = interaction(group2, group1))) +
    geom_col(position = "dodge") +
    scale_fill_manual("",values = fills, labels=lbl1)  +
    scale_color_manual("",values = colo, labels=lbl1)

gt <- ggplotGrob(p)

gt
#TableGrob (12 x 11) "layout": 19 grobs
#z         cells       name                                            grob
#1   0 ( 1-12, 1-11) background               rect[plot.background..rect.16164]
# ...
#14 13 ( 7- 7, 7- 7)     ylab-r                                  zeroGrob[NULL]
#15 14 ( 7- 7, 9- 9)  guide-box                               gtable[guide-box]
#16 15 ( 4- 4, 5- 5)   subtitle         zeroGrob[plot.subtitle..zeroGrob.16160]
# ...

# We want the item 15 guide-box
gt$grobs[[15]]$grobs
#21 21 ( 4- 4, 4- 4)   label-3-3             gTree[GRID.gTree.11993]
#22 22 ( 5- 5, 4- 4)   label-4-3             gTree[GRID.gTree.11994]
#23 23 ( 6- 6, 4- 4)   label-5-3             gTree[GRID.gTree.11995]
#24 24 ( 7- 7, 4- 4)   label-6-3             gTree[GRID.gTree.11996]
#25 25 ( 8- 8, 4- 4)   label-7-3             gTree[GRID.gTree.11997]
#26 26 ( 9- 9, 4- 4)   label-8-3             gTree[GRID.gTree.11998]
#27 27 (10-10, 4- 4)   label-9-3             gTree[GRID.gTree.11999]
#28 28 (11-11, 4- 4)  label-10-3             gTree[GRID.gTree.12000]
#29 29 (12-12, 4- 4)  label-11-3             gTree[GRID.gTree.12001]

# Items 21 to 29 are labels, replace the labels with the ones we want.
for(i in 1:9)
{
  gt$grobs[[15]]$grobs[[1]]$grobs[[20 +i]]$children[[1]]$children[[1]]$label <- lbl2[i]

}
# New plot
grid.draw(gt)

enter image description here

关于删除ggplot图例中的重复标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52042352/

相关文章:

r - 图右侧的线标签不重叠

python - matplotlib 中的阴影图例线

r - R中的录音

r - 循环遍历文件夹并查找 R 中的特定文件

r - 如何使情节标题部分加粗?

r - 按年份排序facet_wrap图的更简单方法

r - 使用 R 进行 3D 可视化

r - R 中的直方图 - x 轴未正确居中

html - 应用与 legend 标签相同的 CSS

r - 图例中的垂直空间