【问题标题】:Adjust Height and Width of Legend Glyphs Generated by key_glyph ggplot调整 key_glyph ggplot 生成的图例字形的高度和宽度
【发布时间】:2020-06-04 05:42:51
【问题描述】:

我很高兴发现我可以通过将key_glyph = draw_key_rect 添加到我的 geom 图层来更改图例中使用的字形。我想让图例更宽更短,以类似于Timo Grossenbacher 这张地图中的图例:

我尝试调整 scale_fill_manual(guide = guide_legend(keyheight = unit(0.01, units = "mm") , keywidth = unit(40, units = "mm"))) 来改变图例的尺寸,但似乎只有在我将字形变大时才会起作用。我似乎无法将键高调小。

有没有更好的方法来调整图例字形的尺寸?

这里是简化代码:

df <- data_frame(x_value = c(1:10),
                 y_value = c(rev(1:10)),
                 value = c("a","a","a","a","b","b","b","b","c","c"))
library(ggplot2)

ggplot(data = df) + 
  geom_point(aes(x_value, y_value, fill = value),
             shape = 21,
             size = 9,
             key_glyph = draw_key_rect) +
  theme(legend.justification = c(0,0), # set which corner of legend legen.position references
        legend.position = c(0.05, 0.04)) +
  scale_fill_manual(values = c("red", "green", "blue"),
                    guide = guide_legend(direction = "horizontal",
                                         keyheight = unit(0.01, units = "mm"),
                                         keywidth = unit(40, units = "mm"),
                                         title.position = 'top',
                                         label.position = "bottom"))

【问题讨论】:

  • 你也看过here吗?

标签: r ggplot2 legend-properties


【解决方案1】:

You could create a fake legend。或者这里是另一个温和的 hacky 解决方案。

问题似乎出在geom_point 中的尺寸参数上,因此您可以做出虚假的审美,尤其是使用线条,例如像这样:

  • 画透明线(geom_line(alpha = 0)
  • geom_line 制作颜色图例,颜色与填充颜色相同。
  • 将颜色图例的 alpha 设置为 1 (override.aes)
  • 删除填充图例。
  • 控制theme中的图例高度
library(ggplot2)

df <- data.frame(x_value = c(1:10),
                 y_value = c(rev(1:10)),
                 value = c("a","a","a","a","b","b","b","b","c","c"))

ggplot(data = df) + 
  geom_point(aes(x_value, y_value, fill = value), shape = 21, size = 7, 
             show.legend = FALSE) + # remove fill legend
  geom_line(aes(x_value, y_value, color = value), 
            alpha = 0, #invisible line. If your data is big, you could create a very small data frame for that
            key_glyph = draw_key_rect) +
  theme(legend.justification = c(0,0), 
        legend.position = c(0.05, 0.04), 
        legend.key.height = unit(0.1, 'in')) + # control height
  scale_fill_manual(values = c("red", "green", "blue")) +
  scale_color_manual(values = c("red", "green", "blue")) +
  guides(color = guide_legend(override.aes = list(alpha = 1), # make line visible in legend 
                              direction = "horizontal",
                              keywidth = unit(20, units = "mm"),
                              title.position = 'top',
                              label.position = "bottom"))

reprex package (v0.3.0) 于 2020 年 2 月 20 日创建

另一种选择是删除您的key_glyph 参数并使用geom_line 中的size 参数控制字形高度

【讨论】:

  • 这是一个创造性的解决方法,谢谢。有一条评论(现在消失了)与这个 Discrete Color Bar Function 相关联,这实际上正是我想用我的真实数据做的事情!
  • 谢谢,很高兴你喜欢它。我已经在我的回答中链接到同一个线程...... ;)
  • 对!隐藏在普通视图中
猜你喜欢
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 2014-04-04
  • 1970-01-01
  • 2013-09-05
  • 2017-01-30
相关资源
最近更新 更多