R ggplot 2 legend border issue

Viewed 320

I know this is minor, but it's for a publication and will drive me crazy. The bottom of the P0688 box is like 1-2 pixels thinner than the rest. I don't want to make the borders thicker because then it doesn't match the rest of the bar chart.

  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
 )

Bottom of P0688 box

It's not even that noticeable at a smaller size. Still bothers me.

Thanks in advance. Let me know if there is anything else needed to help solve this.

1 Answers

This is due to the filling behaviour of the legend keys. It's a known issue, see this GitHub thread https://github.com/tidyverse/ggplot2/issues/2844. There is also a fix suggested on this site, let me show this here.

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))

Enlarged, the legend will look now like this: enter image description here

Now let's do the fix.

  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))

enter image description here

But what is actually going on here? Let's see!

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')) 

The legend has two borders! One for the legend glyph, the other for the key. You draw the border for the key with the call to theme, and the border around the glyph is created with your color argument in geom_bar

enter image description here

Created on 2020-04-04 by the reprex package (v0.3.0)

Related