Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce padding in ggplot2 legend

Tags:

r

ggplot2

I am trying to obtain a graph using ggplot2. The problem is that I am not able to reduce the white-space between the legend border and the text. For example: sample legend,

In this, I want to reduce the white space at the top that I have marked with some curly lines.

I searched a lot, but at most places, the answers suggest to reduce the key.height or key.width, but that is not what I want; I am pretty happy with those. I just want to get rid of the white space at the top.

like image 516
Sonu Mishra Avatar asked Dec 30 '16 01:12

Sonu Mishra


1 Answers

You can remove the space for the legend title with:

theme(legend.title=element_blank())

If that's not sufficient then do:

theme(legend.title=element_blank(),
      legend.margin=margin(c(1,5,5,5)))

and play around with the values (they're in the order top, right, bottom, left) until you get something you like.

like image 178
eipi10 Avatar answered Oct 27 '22 00:10

eipi10