Data = spatial_dist_fallows.csvlibrary(sf) #sf = simple featurelibrary(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)对齐标题文本,还有其他想法吗?Extra question. Once I started to use the bquote I could not align the title text using theme(legend.title.align = 0.5), any other ideas?推荐答案经过其他尝试,我确实为图例标题提供了以下解决方案.After some other tries, I did come up with the following solution for the legend title.name = expression(atop("", atop(textstyle("Projected fruit"), atop(textstyle("productivity in"), atop(textstyle("fallows in 40 yrs"), atop(textstyle("(fruits ha"^-1*")"))))))),我使用textstyle()来绘制具有相同大小的所有文本,否则每次调用atop()时,它都会被绘制得更小. Atop()在第一行和第二行之间创建一个空格,这就是为什么代码的第一行是atop(",所以第一行将是空白的原因.I used textstyle() to plot all text with the same size, otherwise it would be plotted smaller every time atop() was called. Atop() creates a space between the first and second line, that is why the first line of the code is atop("", so the first line will be a blank.这是下面地图的最终代码.This is the final code with the map below.library(sf) #sf = simple featurelibrary(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_legend_test<-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 = expression(atop("", atop(textstyle("Projected fruit"), atop(textstyle("productivity in"), atop(textstyle("fallows in 40 yrs"), atop(textstyle("(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)))+ 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_legend_test 这篇关于在超过两行中使用上标时修复图例标题-R ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 19:08