如何调整ggplot2底部显示的图例及图例标题
问题需求
希望将fill美学对应的图例显示在图表底部,同时实现两个要求:
- a) 图例位于图表中间(x轴标签正下方);
- b) 图例标题与图例居中对齐。
当前图表状态:图例已放置在底部,但未居中于x轴标签正下方,且图例标题与图例未对齐。
原生成代码
# Libraries library(ggplot2) library(plyr) library(latex2exp) # Function to calculate calc_wet_bulb_temperature <- function(dry_temp, RH){ wet_temp <- dry_temp*atan(0.151977 * sqrt(RH + 8.313659)) + 0.00391838 * sqrt(RH^3) * atan(0.023101*RH) - atan(RH - 1.676331) + atan(dry_temp + RH) - 4.686035 return(wet_temp) } # Space to visualise dry_temp <- seq(from = -10, to = 45, by = 1) RH <- seq(from = 0, to = 100, by = 1) temp_space <- expand.grid(dry_temp, RH) names(temp_space) <- c("dry_temp", "RH") # Calculate wet bulb temperature temp_space <- ddply(temp_space, c("dry_temp", "RH"), summarise, wet_temp = calc_wet_bulb_temperature(dry_temp, RH)) # Visualise space ## Calculate value where the colour scale should be red #diff(range(temp_space$wet_temp)) #diff(c(range(temp_space$wet_temp)[1], 30)) #42.69626/57.88312 ## Actual plot ggplot(temp_space, aes(x = RH, y = dry_temp, fill = wet_temp)) + geom_tile() + coord_equal(expand = FALSE) + scale_fill_viridis_c(option = "H", values = c(0.0, 0.2, 0.4, 0.6, 0.7376289, 1.0), limits = range(temp_space$wet_temp)) + theme(legend.position = "bottom", legend.title.position = "top") + labs(title = "Wet - bulb temperature", x = "Relative humidity", y = TeX(r"( Dry temperature ($C\degree$) )"), fill = TeX(r"( Wet temperature ($C\degree$) )"))
解决方案
只需在theme()中添加三个关键参数,即可实现需求:
legend.justification = "center":让图例本身以中心对齐legend.box.just = "center":确保图例容器在底部居中于x轴标签正下方legend.title.align = 0.5:让图例标题与图例中心对齐
修改后的完整代码
# Libraries library(ggplot2) library(plyr) library(latex2exp) # Function to calculate calc_wet_bulb_temperature <- function(dry_temp, RH){ wet_temp <- dry_temp*atan(0.151977 * sqrt(RH + 8.313659)) + 0.00391838 * sqrt(RH^3) * atan(0.023101*RH) - atan(RH - 1.676331) + atan(dry_temp + RH) - 4.686035 return(wet_temp) } # Space to visualise dry_temp <- seq(from = -10, to = 45, by = 1) RH <- seq(from = 0, to = 100, by = 1) temp_space <- expand.grid(dry_temp, RH) names(temp_space) <- c("dry_temp", "RH") # Calculate wet bulb temperature temp_space <- ddply(temp_space, c("dry_temp", "RH"), summarise, wet_temp = calc_wet_bulb_temperature(dry_temp, RH)) # Visualise space ## Calculate value where the colour scale should be red #diff(range(temp_space$wet_temp)) #diff(c(range(temp_space$wet_temp)[1], 30)) #42.69626/57.88312 ## Actual plot ggplot(temp_space, aes(x = RH, y = dry_temp, fill = wet_temp)) + geom_tile() + coord_equal(expand = FALSE) + scale_fill_viridis_c(option = "H", values = c(0.0, 0.2, 0.4, 0.6, 0.7376289, 1.0), limits = range(temp_space$wet_temp)) + theme( legend.position = "bottom", legend.title.position = "top", legend.justification = "center", # 图例自身居中 legend.box.just = "center", # 图例容器在底部居中 legend.title.align = 0.5 # 图例标题居中对齐 ) + labs(title = "湿球温度", x = "相对湿度", y = TeX(r"( 干球温度 ($^\circ C$) )"), fill = TeX(r"( 湿球温度 ($^\circ C$) )"))
参数说明
legend.justification:控制图例内部的对齐基准点,设为"center"让图例整体以中心为基准对齐legend.box.just:控制图例所在容器在图表底部的位置,确保它精准位于x轴标签正下方的中间区域legend.title.align:取值0(左对齐)到1(右对齐),设为0.5即可让标题与图例的中心完全对齐
内容的提问来源于stack exchange,提问作者JAQuent
相关产品推荐
相关产品推荐

