Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase space between legend keys without increasing legend keys

Tags:

r

ggplot2

This is a follow-up on https://stackoverflow.com/questions/32275113

The problem is to tweak the legend elements to increase the space between legend keys without simultaneously extending the legend keys themselves. The solution is likely to be in tweaking the correct legend theme option.

Desired result: more vertical space between the legend key text labels, but without stretching the legend key lines.

d <- data.frame(x = mtcars$mpg, y = 0.10)
vlines <- rbind(aggregate(d[1], d[2], mean), 
                aggregate(d[1], d[2], median))
vlines$stat <- rep(c("mean", "median"), each = nrow(vlines)/2)
library("ggplot2")
ggplot(data = d, aes(x = x, y = ..density..)) + 
    geom_histogram(fill = "lightblue", color = "black") + 
    geom_vline(data = vlines, mapping = aes(xintercept = x, colour = stat), 
            show.legend = TRUE) +
    theme(legend.direction = "vertical", 
        legend.position = "right",
        #          legend.key = element_rect(size = 2),
        legend.key.size = unit(3, "cm"),
        #          legend.key.width = unit(2, "cm"),
        #          legend.key.height = unit(1, "cm")
        )

Increasing legend.key.size, as suggested in answers to the linked question (see above), has the undesired side effect of increasing the vertical lines as well.

enter image description here

Edit Based on PoGibas's clever workaround, here's a screenshot of the desired result, included here to make sure the purpose is clear:

enter image description here

Following PoGibas, I used: shape = 73, legend.key.height = unit(2, "cm") and size = 6 inside the color guide.

like image 394
PatrickT Avatar asked Apr 05 '18 09:04

PatrickT


People also ask

Is there a way to change the spacing between legend items in ggplot2?

position = "top" or "bottom" , you can change the spacing between legend keys with legend. spacing. x . You can supply a unit object to this argument, e.g. unit(1.0, "cm") for 1 cm space between legend keys.

How do I reduce space between legends?

If you want to adjust the line space between lines in the legend, you can right-click the legend to select Properties... from the context menu to open the Text Object dialog. In the Text tab of this dialog, for the Line Spacing(%) item, select a value from the drop-down list or enter a value in the combo box directly.

How do you change the size of the legend symbol?

In the Symbology pane, under Properties, change Size accordingly, and click Apply.

How do I change labels in legend?

Change the legend name using select dataSelect your chart in Excel, and click Design > Select Data. Click on the legend name you want to change in the Select Data Source dialog box, and click Edit. Note: You can update Legend Entries and Axis Label names from this view, and multiple Edit options might be available.


1 Answers

One solution is to replace lines with points (requires additional geom layer):

Create plot with invisible points (size = 0 and rectangle shape shape = 15).

p <- ggplot(d, aes(x, ..density..)) + 
    geom_histogram(fill = "lightblue", color = "black") + 
    geom_vline(data = vlines, mapping = aes(xintercept = x, colour = stat)) +
    geom_point(data = vlines, aes(0, 0, colour = stat), size = 0, shape = 15)

Add legend theme to:

  • Mask background color in legend (legend.key = element_rect(fill = "white"))
  • Create large legend (legend.key.height = unit(3, "cm"))
  • Remove lines (linetype = 0) and make large points (size = 5)

Code:

p + 
    theme(legend.direction = "vertical", 
          legend.position = "right",
          legend.key = element_rect(fill = "white"),
          legend.key.height = unit(3, "cm")) +
    guides(color = guide_legend(override.aes = list(linetype = 0, size = 5)))

enter image description here

PS.:

  • This is not a perfect solution as there's a gap between legend label and boxes.
  • If you want lines instead of rectangles use shape = 73
like image 72
pogibas Avatar answered Oct 25 '22 05:10

pogibas