【问题标题】:changing the legends in ggplot2 to have groups of similar labels将 ggplot2 中的图例更改为具有相似标签组
【发布时间】:2021-08-01 01:55:39
【问题描述】:

这个问题建立在enter link description here提供的这个解决方案之上

如下。

library(ggplot2)
library(gtable)
library(grid)

diamonds$cut = factor(diamonds$cut, levels=c("Fair","Good"," ","Very Good",
                                             "Premium","Ideal"))

p = ggplot(diamonds, aes(color, fill = cut)) + 
       geom_bar() + 
       scale_fill_manual(values = 
              c(hcl(seq(15, 325, length.out = 5), 100, 65)[1:2], 
              "white",
              hcl(seq(15, 325, length.out = 5), 100, 65)[3:5]),
              drop = FALSE) +
  guides(fill = guide_legend(ncol = 2, title.position = "top")) +
  theme(legend.position = "bottom", 
        legend.key = element_rect(fill = "white"))

# Get the ggplot grob
g = ggplotGrob(p)

# Get the legend
leg = g$grobs[[which(g$layout$name == "guide-box")]]$grobs[[1]]

# Set up the two sub-titles as text grobs
st = lapply(c("First group", "Second group"), function(x) {
   textGrob(x, x = 0, just = "left", gp = gpar(cex = 0.8)) } )

# Add a row to the legend gtable to take the legend sub-titles
leg = gtable_add_rows(leg, unit(1, "grobheight", st[[1]]) + unit(0.2, "cm"), pos =  3)

# Add the sub-titles to the new row
leg = gtable_add_grob(leg, st, 
            t = 4, l = c(2, 6), r = c(4, 8), clip = "off")

# Add a little more space between the two columns
leg$widths[[5]] = unit(.6, "cm")

# Move the legend to the right
 leg$vp = viewport(x = unit(.95, "npc"), width = sum(leg$widths), just = "right")

# Put the legend back into the plot
g$grobs[[which(g$layout$name == "guide-box")]] = leg

# Draw the plot
grid.newpage()
grid.draw(g)

这提供了图(从那里)。

但是,我想做两件事。

  1. 第一组:我想将“Fair”和“Good”分别重新标记为“Very Good”和“Premium”。所以他们在每组(第一和第二)中都非常好和优质。

  2. 我想做的第二件事是将它们放在一行中(第一组和第二组的两列并排,所有内容都在一行中,以节省垂直空间。

我该怎么做?如果我的问题不清楚,请告诉我。我觉得我在这里很近,但这个解决方案并不能完全回答我的问题。非常感谢!

【问题讨论】:

  • 您的问题似乎不清楚。(不鼓励多部分问题。)为什么不在 ggplot 调用之前修改 data 参数?并放入“一条线”?这是什么意思?

标签: r ggplot2 r-grid gtable


【解决方案1】:

首先,您可以通过scale_fill_manuallabels 参数简单地更改出现在图例中的标签。其次,与其摆弄gtable,如果您希望将组放在一行中,这可能会更加苛刻,您可以使用ggnewscale 包,正如this answer 在您添加为链接的同一帖子中已经提出的那样.虽然该答案还使用了 relayer 包,但我的方法有点不同,因为我重新排列了层的顺序,因此不需要 relayer 包:

library(ggplot2)
library(ggnewscale)

diamonds$cut = factor(diamonds$cut, levels=c("Fair","Good", "Very Good",
                                             "Premium","Ideal"))

labels <- levels(diamonds$cut)
labels <- setNames(labels, labels)
labels["Fair"] <- "Very Good"
labels["Good"] <- "Premium"

colors <- hcl(seq(15, 325, length.out = 5), 100, 65)
colors <- setNames(colors, levels(diamonds$cut))

ggplot() + 
  geom_bar(data = diamonds, aes(color, fill = cut)) + 
  scale_fill_manual(aesthetics = "fill", values = colors, labels = labels[1:2],
                    breaks = names(colors)[1:2], name = "First Group:",
                    guide = guide_legend(title.position = "left", order = 1)) +
  new_scale_fill() +
  geom_bar(data = diamonds, aes(color, fill = cut)) + 
  scale_fill_manual(aesthetics = "fill", values = colors, labels = labels[3:5],
                    breaks = names(colors)[3:5], name = "Second Group:",
                    guide = guide_legend(title.position = "left", order = 0)) +
  theme(legend.position = "bottom", 
        legend.direction = "horizontal",
        legend.key = element_rect(fill = "white"))

【讨论】:

  • 谢谢。我的问题是我真的在使用箱线图,所以这是对函数的一次调用。我不知道我是否可以让您的建议与箱线图一起使用,每组一个。我认为传奇问题会是一样的。但是,这回答了这个特定的问题,所以我将它标记为这样。再次非常感谢!
  • 我决定在这里用一个例子发布我更复杂的问题,因为我无法从这里解决这个问题。新问题在这里:stackoverflow.com/questions/68611542/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 2011-11-11
  • 1970-01-01
  • 2015-09-20
  • 2014-10-20
相关资源
最近更新 更多