我正在绘制我的研究区域的地图,但在编辑图例标题时遇到问题。
我需要它是“预计 40 年休耕期的水果生产率(水果 ha^-1)”,用四行写成。我可以使用 bquote() 将 -1 绘制为上标。但它创建了一个我无法提供的额外空间想办法把它去掉。只有当标题被分成多行时,才会出现多余的空格。
另外,表达式(atop())创建上标,但一旦我尝试将其分成两行以上,它就不会显示第三行和第四行。
This is the Map with the extra space using bquote()
This is the Map with the four line title using expression(atop())
我确实尝试了在互联网上找到的不同解决方案,包括这个帖子 https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression。但他们都用额外的空间绘制第四行,或者只绘制第一行或第二行。
下面是我正在使用的代码。欢迎任何帮助。
评论是不同的尝试。
Data = Spatial_dist_fallows.csv https://drive.google.com/file/d/10wbXaYUTFzVNgwMcKME8hCNbRcdf-Mut/view?usp=sharing
library(sf) #sf = simple feature
library(ggplot2)
library(dplyr)
PAECM_fallows <-read.csv("spatial_dist_fallows.csv")
PAECM_fallows_sp <- st_as_sf(PAECM_fallows,coords = c("X", "Y"),crs = "+proj=longlat +datum=WGS84 +no_defs")
custom_bins_fruit = c(0,60,120,180,240,1400)
PAECM_fallows_fruit <- PAECM_fallows_sp %>%
mutate(prod_cat_fallow = cut(prod_40, breaks= custom_bins_fruit),
age_cat_fallow = cut(age, breaks = c(11,17,22,29,60)))
prod_map_PAECM_fruit<-ggplot()+
geom_sf(data = PAECM_fallows_fruit,aes(size = prod_cat_fallow), shape = 18, show.legend = "point")+
scale_size_manual(values= c(2,3,4,5,6),
# name = "Projected fruit\nproductivity in\nfallows in 40 yrs \n(fruits ha^-1)",
name = bquote("Projected fruit\nproductivity in\nfallows in 40 yrs \n( fruits"*ha^-1*")"),
# name = expression(paste("Projected fruit productivity\nin fallows in 40 yrs\n"),bquote(paste("("*fruits~ha^-1*")"))),#(Fruits/ha)
name = expression(atop("Projected fruit",
"productivity in",
"fallows in 40 yrs",
"( fruits ha"^-1,")")),
breaks= c(NA,"(0,60]","(60,120]","(120,180]","(180,240]","(240,1.4e+03]"),
labels= c("NA","\u2264 60","60 - 120","120 - 180","180 - 240","> 240"),
guide = guide_legend(override.aes = list(linetype = "blank", shape = 18, fill = NA)))+
# labs(size = expression(atop("Projected fruit\nproductivity in\nfallows in 40 yrs\n(fruits"*ha^-1*")", sep="")))+ #comment name line at the scale_size_manual
# labs(size = bquote("Projected fruit productivity \nin fallows in 40 yrs \n( fruits"*ha^-1*")"))+ #comment name line at the scale_size_manual
ggplot2::theme_minimal()+
ggplot2::theme(legend.text.align=0.5,
legend.title.align = 0.5,
plot.background = element_blank(),
panel.grid = element_line(colour = "white"),
panel.background = element_rect(fill = "grey87", color = "white"))+#,
coord_sf(xlim = c(-68.45,-68.2), ylim = c(-11.05,-10.8))
prod_map_PAECM_fruit
额外的问题。一旦我开始使用 bquote,我就无法使用 theme(legend.title.align = 0.5) 对齐标题文本,还有其他想法吗?