我正在用 ggplot2 制作一個圖表,我有兩個圖例,一個顯示顏色,一個顯示形狀,它在圖例之間創建了這個非常大的空間并從圖表中移除。
這是我的代碼:
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")
這就是它正在生產的東西:

在圖片中,您可以看到“位置”和“區域”圖例之間的大空間。
我嘗試調整圖例的邊距,但沒有任何變化。
uj5u.com熱心網友回復:
圖例之間的間距可以通過legend.spacing,legend.spacing.x和設定legend.spacing.y。此外,您可以通過以下方式洗掉或減少每個圖例周圍的邊距legend.margin:
使用基于以下的最小可重現示例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))

并作為參考這里具有默認間距和邊距的情節:
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")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522296.html
標籤:rggplot2传奇
