How to remove the gap in the ggplot legend?

Rancho :

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.

Allan Cameron :

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

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

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related