5

In ggplot2, I don't know how to increase the gap between legend items. I've read some similar questions from these posts 1, 2, but it did not work for my case. Below is my code, which generates the figure as attached. I'd like to increase the gap between legend items as shown in the attached figure. Any helps or hints would be very appreciated. Thank you.

My code:

## data:
df <- data.frame(supp=rep(c(" link ratio 1:1 ", " link ratio 1:2 ", " link ratio 1:3 ", 
                             " link ratio 1:4 ", " link ratio 1:5 ", " link ratio 1:6 "), 
                           each=7, ## number of bargroups
                           ordered = TRUE), ## nrows
                  test_X=rep(c("1.0", "1.2", "1.4", "1.6", "1.8", "2.0", "2.2"), 6), ## ncols
                  test_Y=c(
                    8,  9,  16, 18, 23, 28, 27,
                    14, 15, 27, 30, 38, 47, 47,
                    8,  8,  11, 15, 21, 25, 22,
                    12, 13, 23, 25, 33, 39, 39,
                    7,  8,  13, 13, 18, 24, 24,
                    10, 12, 19, 22, 27, 33, 33)) 


## reorder legend items
df$supp <- factor(df$supp, c(" link ratio 1:1 ", " link ratio 1:2 ", " link ratio 1:3 ", 
                               " link ratio 1:4 ", " link ratio 1:5 ", " link ratio 1:6 "))

## libs
require(ggthemes)
require(ggplot2)

g<-ggplot(data=df, aes(clarity, x=test_X, y=test_Y, fill=supp)) +
  geom_bar(width=0.75, stat="identity", position=position_dodge(width=0.75), colour="#000000", size=1.35) +  
  scale_fill_brewer(palette="Greens") + 
  theme_bw(base_size = 30, base_family = "") + 
  theme(panel.border = element_rect(fill = NA, colour = "black", size = 2.75), legend.position="top",
        legend.title = element_blank(),
        legend.key = element_rect(fill = NA, colour = "black"),
        legend.key.width = unit(1.4, "cm"),
        legend.key.height = unit(0.5, "cm"),
        legend.margin = unit(0.65, "cm"),
        legend.text = element_text(size = 55, face= "bold")
  ) + scale_y_continuous(expand = c(0,0), limits=c(0, 55)) 
g<-g + guides(fill=guide_legend(ncol=2, byrow = TRUE)) 
g<-g + labs(x = "Rate", y="#links")

print(g)

Figure:

how to increase the gap between legend items?

My desired figure:

Big space between legend items

Community
  • 1
  • 1
Tung Le
  • 109
  • 1
  • 3
  • 11
  • In linked code snippet I added horizontal space. If you change ``widths`` to ``heights`` and find the correct index for the 2 lines you want to add (in my example it's 6, you would have 2 such lines to add), you might get there: https://pastebin.com/NnxMiTeH – PatrickT Dec 18 '17 at 20:17

1 Answers1

3

There is now an easy way to achieve this thanks to legend.spacing.x and legend.spacing.y:

library(ggplot2)

df <- data.frame(
  supp = rep(c("link ratio 1:1", "link ratio 1:2", "link ratio 1:3", 
               "link ratio 1:4", "link ratio 1:5", "link ratio 1:6"), 
             each = 7, ordered = TRUE),
  test_X = rep(c("1.0", "1.2", "1.4", "1.6", "1.8", "2.0", "2.2"), 6),
  test_Y = c(8,  9,  16, 18, 23, 28, 27,
             14, 15, 27, 30, 38, 47, 47,
             8,  8,  11, 15, 21, 25, 22,
             12, 13, 23, 25, 33, 39, 39,
             7,  8,  13, 13, 18, 24, 24,
             10, 12, 19, 22, 27, 33, 33)
)

g <- ggplot(data = df, aes(clarity, x = test_X, y = test_Y, fill = supp)) +
  geom_bar(width = 0.75, stat = "identity",
           position = position_dodge(width = 0.75),
           colour = "#000000", size = 1.35) +  
  scale_fill_brewer(palette = "Greens") + 
  theme_bw(base_size = 30) + 
  theme(
    panel.border = element_rect(fill = NA, colour = "black", size = 2.75), legend.position = "top",
    legend.title = element_blank(),
    legend.key = element_rect(fill = NA, colour = "black"),
    legend.key.width = unit(1.4, "cm"),
    legend.key.height = unit(0.5, "cm"),
    legend.spacing.x = unit(0.5, 'cm'),
    legend.spacing.y = unit(1.0, 'cm'),
    legend.text = element_text(size = 55, face = "bold")
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 55)) +
  guides(fill = guide_legend(ncol = 2, byrow = TRUE)) +
  labs(x = "Rate", y = "#links")

g

enter image description here

Note 1: you don't need ggthemes and you don't need to reorder your legend items, so I removed those from your code.

Note 2: beside a little reformatting, I kept your code as is, but it might be better to decrease the size of the legend (a lot).

Note 3: credit goes to Tung for his answer here.

prosoitos
  • 6,679
  • 5
  • 27
  • 41