简体   繁体   中英

Make box around legend bigger in ggplot2

I have a plot with a legend, and the legend has a rectangle around it, but the rectangle overlapps a little with the things inside of the legend. Is there a way I can increase the box size a little?

在此处输入图像描述

data2 <- structure(list(Class = c("Organic", "Organic", "Organic", "Organic", 
"Organic", "Organic", "Organic", "Organic", "Organic", "Organic", 
"Organic", "Organic", "Organic"), Depth = c(69, 73, 148, 365, 
452, 965, 100, 100, 120, 120, 150, 159, 180), TrapID = c("4-22", 
"1-12", "1-12", "3-21", "3-21", "4-13", "2-14", "2-17", "3-15", 
"3-18", "2-17", "1-19", "3-18"), TrapType = c("cone", "cone", 
"cone", "net", "net", "net", "cone", "cone", "cone", "cone", 
"net", "cone", "net"), SampleType = c("plus.p", "plus.p", "plus.p", 
"plus.p", "plus.p", "plus.p", "top", "top", "top", "top", "top", 
"top", "top"), C_flux = c(1.86346195335968, 0.33708698993135, 
0.287766715331808, 0.342070253658537, 0.53058016195122, 0.162216257196462, 
0.237619178449906, 1.16823528498024, 0.82924427637051, 1.18838025889328, 
0.316782054545455, 0.420967185507246, 0.448680747228381), C_flux_umol = c(155.288496113307, 
28.0905824942792, 23.980559610984, 28.5058544715447, 44.215013495935, 
13.5180214330385, 19.8015982041588, 97.3529404150198, 69.1036896975425, 
99.0316882411067, 26.3985045454545, 35.0805987922705, 37.3900622690318
)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-13L), spec = structure(list(cols = list(Class = structure(list(), class = c("collector_character", 
"collector")), Depth = structure(list(), class = c("collector_double", 
"collector")), TrapID = structure(list(), class = c("collector_character", 
"collector")), TrapType = structure(list(), class = c("collector_character", 
"collector")), SampleType = structure(list(), class = c("collector_character", 
"collector")), C_flux = structure(list(), class = c("collector_double", 
"collector")), C_flux_umol = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))

ggplot(data = data2)  + scale_y_reverse(limits = c(1000, 0)) +
  scale_x_continuous(limits = c(0, 200)) +
  geom_point(aes(y = Depth, x = C_flux_umol, fill = SampleType, shape = TrapType),
             colour = "black", stroke = 1, size = 5) +
  #geom_point(aes(x = tot_flux2)) +
scale_shape_manual(values = c(25, 22))+
  scale_size_manual(values = c(3, 4)) +
  ylab("Depth (m)") + xlab("Flux micro-mol/m^2/day") +
  guides(fill = guide_legend(override.aes = list(shape = 25))) +
  theme_cowplot() +
  theme(legend.position = c(0.5, 0.4),
        legend.box.background = element_rect(color = "black", size = .5))

This might do the trick:

ggplot(data = data2)  + scale_y_reverse(limits = c(1000, 0)) +
  scale_x_continuous(limits = c(0, 200)) +
  geom_point(aes(y = Depth, x = C_flux_umol, fill = SampleType, shape = TrapType),
             colour = "black", stroke = 1, size = 5) +
  #geom_point(aes(x = tot_flux2)) +
scale_shape_manual(values = c(25, 22))+
  scale_size_manual(values = c(3, 4)) +
  ylab("Depth (m)") + xlab("Flux micro-mol/m^2/day") +
  guides(fill = guide_legend(override.aes = list(shape = 25))) +
  theme_cowplot() +
  theme(legend.position = c(.5, .5),
        legend.margin = margin(6, 6, 6, 6),
        legend.box.background = element_rect(color = "black", size = .5)) 

For further reference check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM