使用长标签名称gglot设置标准图例关键字大小 [英] Set standard legend key size with long label names ggplot

查看:8
本文介绍了使用长标签名称gglot设置标准图例关键字大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个gggraph可视化,其中一些填充美学具有非常长的变量名,而其他变量名则很短。添加长名称会更改与长文本对应的图例键的大小-将其加长以与文本匹配。我想知道是否有一种方法可以标准化所有变量的图例键高度,并更改图例项之间的间距。

我尝试修改theme(legend.key.height())theme(legend.key.width()),但没有解决问题。

以下是示例代码:

#load neccesary package
library('ggplot2')

#create the dataframe
df <- data.frame(year = as.integer(c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)),
                 class = c('A', 'B', 'C', 'D', 'E'), 
                 value = c(50, 50))

#Create plot
g <- ggplot(df, aes(x = year, y = value, fill = class)) + 
  geom_col(position = 'stack') + 
  scale_fill_discrete(labels = c('This is an
extremely
long label
name', 'short label1', 'Another
long
label
name', 'short label3', 'short label4'))

曲线图:

我想要的是所有变量的键大小都相同,键之间的空格要更改以适应长文本。所以看起来是这样的:

正在尝试g + theme(legend.key.height = unit(3, 'mm'), legend.key.width = unit(3, 'mm'))

不能解决问题。

有什么想法吗?

推荐答案

您可以通过定义自己的图例类来做到这一点。这当然比主题中的简单选项更冗长,了解一些表/网格可能很方便,但它可以完成工作。

library(ggplot2)
library(grid)

#create the dataframe
df <- data.frame(year = as.integer(c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)),
                 class = c('A', 'B', 'C', 'D', 'E'), 
                 value = c(50, 50))

labs <-  c('This is an
extremely
long label
name', 'short label1', 
           'Another
long
label
name', 'short label3', 'short label4')

guide_squarekey <- function(...) {
  # Constructor just prepends a different class
  x <- guide_legend(...)
  class(x) <- c("squarekey", class(x))
  x
}

guide_gengrob.squarekey <- function(guide, theme) {
  # Make default legend
  legend <- NextMethod()

  # Find the key grobs
  is_key <- startsWith(legend$layout$name, "key-")
  is_key <- is_key & !endsWith(legend$layout$name, "-bg")

  # Extract the width of the key column
  key_col <- unique(legend$layout$l[is_key])
  keywidth <- convertUnit(legend$widths[2], "mm", valueOnly = TRUE)

  # Set the height of every key to the key width
  legend$grobs[is_key] <- lapply(legend$grobs[is_key], function(key) {
    key$height <- unit(keywidth - 0.5, "mm") # I think 0.5mm is default offset
    key
  })
  legend
}

ggplot(df, aes(x = year, y = value, fill = class)) + 
  geom_col(position = 'stack') + 
  scale_fill_discrete(labels = labs,
                      guide = "squarekey")

reprex package(v0.3.0)于2021-01-20创建

编辑:如果您还想编辑关键背景:

guide_gengrob.squarekey <- function(guide, theme) {
  legend <- NextMethod()
  is_key <- startsWith(legend$layout$name, "key-")
  is_key_bg <- is_key & endsWith(legend$layout$name, "-bg")
  is_key <- is_key & !endsWith(legend$layout$name, "-bg")
  
  key_col <- unique(legend$layout$l[is_key])
  keywidth <- convertUnit(legend$widths[2], "mm", valueOnly = TRUE)
  
  legend$grobs[is_key] <- lapply(legend$grobs[is_key], function(key) {
    key$height <- unit(keywidth - 0.5, "mm")
    key
  })
  legend$grobs[is_key_bg] <- lapply(legend$grobs[is_key_bg], function(bg) {
    bg$height <- unit(keywidth, "mm")
    bg
  })
  legend
}

这篇关于使用长标签名称gglot设置标准图例关键字大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆