【问题标题】:R ggplot 2 legend border issueR ggplot 2图例边框问题
【发布时间】:2020-07-15 22:06:21
【问题描述】:

我知道这是次要的,但这是为了出版,会让我发疯。 P0688 盒子的底部比其他的要薄 1-2 个像素。我不想让边框变粗,因为它与条形图的其余部分不匹配。

  plot<- ggplot(tukey_letters, aes(x = variable, y = value.x, 
        fill = L1)) + 
      theme(panel.background=element_rect(fill="#ffffff", color 
      ="#000000"), panel.grid.major=element_blank(), 
    panel.grid.minor=element_blank()) +
      geom_bar(stat = "identity", position=position_dodge(),color="black")+ scale_fill_manual(values=c("#FFFFFF", "#999999"))+ guides(fill=guide_legend(title="Genotype", title.position = "left")) +
  geom_errorbar(aes(ymin=value.x-se, ymax=value.x+se), width=.1,size=.5,position=position_dodge(0.9), color="black")+
  theme(
    axis.title = element_text(size =12, face="bold"),
    axis.text = element_text(angle=30, vjust=0.5,hjust=0.6,size=8,face="bold", color="#000000"),
    axis.ticks = element_line(size = rel(1)),
    axis.ticks.length = unit(0.3, "cm"),
    legend.position = c(0.2, 0.9)
  )+
  labs(
    x="Treatment",
    y="ARI1"
  )+
  #facet_wrap(~L1)+ ## You can use face_wrap function only if you need it+
  geom_text(data =tukey_letters,
            aes(x=xpos, y=ymax+offset_asterisk,label=groups), 
           size = 4,position=position_dodge(0.9) , vjust=-0.5
 )

提前致谢。让我知道是否还有其他需要帮助解决此问题。

【问题讨论】:

  • You may want to check out this question。寻找 u/Tung 的答案(目前位于顶部)。看起来对于垂直定位的填充图例(就像你有的那样),仍然存在间距差异小的问题(它也让我感到烦恼 - 完全明白)。有关更多信息,请参阅this issue
  • 是的,我看到了。也许我执行不正确,因为 legend.spacing.y 没有改变白色和灰色框之间的距离。
  • 是的 - 在我找到帖子和填充垂直图例有点错误的特定情况之前,我摆弄了 10 分钟。 :/ 你能切换到水平吗? :)

标签: r ggplot2 formatting legend


【解决方案1】:

这是由于图例键的填充行为。这是一个已知问题,请参阅此 GitHub 线程 https://github.com/tidyverse/ggplot2/issues/2844。这个网站上也有一个修复建议,让我在这里展示一下。

library(tidyverse)

ggplot(mtcars) +
  aes(fill=factor(cyl), x=cyl) +
  geom_bar(color = 'black') +
  guides(fill=guide_legend(title.position = "left")) +
  theme(legend.key = element_rect(color="white") +
        legend.position = c(0.2, 0.7))

放大后,图例现在看起来像这样:

现在让我们进行修复。

  draw_key_polygon3 <- function(data, params, size) {
  lwd <- min(data$size, min(size) / 4)

  grid::rectGrob(
    width = grid::unit(0.7, "npc"),
    height = grid::unit(0.7, "npc"),
    gp = grid::gpar(
      col = data$colour,
      fill = alpha(data$fill, data$alpha),
      lty = data$linetype,
      lwd = lwd * .pt,
      linejoin = "mitre"
    ))
}

GeomBar$draw_key = draw_key_polygon3

ggplot(mtcars) +
  aes(fill=factor(cyl), x=cyl) +
  geom_bar(color = 'black') +
  guides(fill=guide_legend(title.position = "left")) +
  theme(legend.key = element_rect(color="white", fill = 'white'),
        legend.position = c(0.2, 0.7))

但这里到底发生了什么?让我们看看!

ggplot(mtcars) +
  aes(fill=factor(cyl), x=cyl) +
  geom_bar(color = 'black') +
  guides(fill=guide_legend(title.position = "left")) +
  theme(legend.position = c(0.2, 0.7),
        legend.key = element_rect(color="black", fill = 'white')) 

传说有两个边界!一个用于图例字形,另一个用于键。您通过调用theme 为键绘制边框,并使用geom_bar 中的颜色参数创建字形周围的边框

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2021-03-17
    相关资源
    最近更新 更多