Home > database >  How to remove the huge space between two legends?
How to remove the huge space between two legends?

Time:10-28

I am making a graph with ggplot2 and I have two legends, one showing color and one showing shape and it is creating this very large space in between the legends and taking away from the graph.

Here is my code:

pchla.pco2.l <- ggplot(data = surf.new)  
  geom_point(aes(x =pCO2, y = pchla, color = Area, shape= factor(Location)), size =4)   
  labs(x=expression(bold(paste("Log(", bolditalic('p'), "CO"['2']," 1)"))), y=expression(bold(paste("Log (Chl-",bolditalic ("a"), "  1)"))), 
       title =expression(bold(paste(bolditalic('p'), "CO"['2']," vs. <20 µm Chl-", bolditalic ("a")))))  
  geom_smooth(aes(x=pCO2, y = pchla), method = lm, formula = y ~ poly(x, 2, raw = T),color="orange1", se = T, data = surf.new)  
  scale_shape_manual(values = location_type)  
  scale_colour_manual(values = gom_region_colors)  
  labs(color  = "Area", shape = "Location")  
  expand_limits(y=c(0, 1.25), x=c(5.2, 6.2))  
  theme(axis.text.x = element_text(margin = margin(t = 10, r = 0, b = 10, l = 0),size=12, face = "bold"),
        axis.text.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 10), size=12, face = "bold"), 
        axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 10, l = 0), size=14, face = "bold"),
        axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 10), size=14, face = "bold"),
        legend.position = "top",
        legend.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0), size=12, face = "bold"),
        legend.text = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0), size=10), 
        plot.title = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0),hjust = 0.5, size = 14, face = 'bold'),
        axis.line.x = element_line(color="black", size = 1), #add axis line
        axis.line.y = element_line(color="black", size = 1),
        panel.background = element_blank(), legend.key=element_blank(), legend.box = "vertical")

and here is what it is producing: enter image description here

In the picture you can see the large space in between the "location" and "Area" legends.

I tried adjusting the margins of the legend and that had no change.

CodePudding user response:

The spacing between legends could be set via legend.spacing, legend.spacing.x and legend.spacing.y. Additionally you could remove or reduce the margin around each legend via legend.margin:

Using a minimal reproducible example based on mtcars:

library(ggplot2)

ggplot(mtcars, aes(hp, mpg, color = factor(cyl), shape = factor(cyl)))  
  geom_point()  
  labs(color  = "Area", shape = "Location")  
  theme(legend.position = "top", 
        legend.box = "vertical",
        legend.spacing.y = unit(0, "pt"), 
        legend.margin = margin(t = 1, b = 1))

And as reference here the plot with the default spacing and margin:


ggplot(mtcars, aes(hp, mpg, color = factor(cyl), shape = factor(cyl)))  
  geom_point()  
  labs(color  = "Area", shape = "Location")  
  theme(legend.position = "top", 
        legend.box = "vertical")

  • Related