How to remove the gap in the ggplot legend?

Viewed 185

There is my data frame

Days,Observed,Simulated
0,0,424.8933328
1,1070,1116.781453
2,2360,2278.166227
3,3882,3854.781359
4,5712,5682.090936
5,7508,7565.230044
6,9126,9343.991798
7,10600,10919.17995
8,11893,12249.07067
9,13047,13332.93044
10,14022,14193.53941
11,14852,14863.84784
12,15480,15378.56415
13,16042,15769.6773
14,16362,16064.57556
15,16582,16285.66038
16,16766,16450.70955
17,16854,16573.54275
18,16854,16664.74816

And this is my code, hope I didn't miss out some information

dt <- read.csv('data.csv')
days <- dt$Days
Observed <- dt$Observed
Simulated <- dt$Simulated

require(ggplot2)
R <- ggplot(dt, aes(x = days))+geom_line(y=Simulated, color="red", size=0.5)+
  geom_point(y=Observed, color="midnightblue", size=1.75)
a <- geom_line(aes(y = Simulated, col='Simulated'))
n <- geom_point(aes(y = Observed, fill = "Observed"), col='blue')
c <- ggtitle("2.5kg of Placenta & 0.5kg of seed") 
h <- labs(x = 'Time(Days)', y = "Cumulative Biogas Yield(ml)", 
          colour = NULL, fill = "Legend")
o <- theme(plot.title = element_text(hjust = 0.1))+
  theme( plot.title = element_text(colour = "midnightblue"),
         axis.title.x = element_text(colour = "black", size = 14),
         axis.title.y = element_text(colour = "black", size = 14),
         legend.title = element_text(colour = "black", size = 14),
         legend.text = element_text(colour = "black", size = 12.5),
         axis.text.x = element_text(colour = "black", size = 14),
         axis.text.y = element_text(colour = "black", size = 14))
d <- scale_color_manual(values = 'red')
s <- scale_fill_manual(values = 'midnightblue')
Myplot <- R+a+n+c+h+o+d+s
Myplot

The result I get have a big gap between the variables and needs to be removed

The legend have a big gap between the Variables

What I want is as follows:

No gap between the variables

I have edited the graph on the painter to get what i want but its tiresome work I would like to have the code that can easy the process for me. Thanks in advance.

2 Answers

You can adjust the spacing between the two legends using a combination of two theme elements: legend.spacing and legend.margin. I played around a bit with these and this combination seems to work well:

Myplot + theme(
    legend.spacing = unit(0,'pt'),
    legend.margin = margin(t=0,b=0,unit='pt')
  )

enter image description here side note Also, just wanted to note that when you want to squish together two legends, but have one title, it is better to do it like you have (where one of the legend titles is set to NULL rather than an empty character "". NULL effectively removes the legend title as an element and makes spacing easier, whereas "" still carries the spacing of the title, even if nothing is represented. If you replace NULL with "" in your code, you'll see this... so good job with that :).

Are you just looking for theme(legend.margin)?

Myplot + theme(legend.margin = margin(0, 0, -10, 0))

enter image description here

Related