优化ggplot2系数图:减少Roads与Timing区域下方空白
解决ggplot2系数图分类区域下方空白过多的问题
问题说明
在使用ggplot2绘制的系数图中,需要减少Roads和Timing区域下方的空白,让图表布局更紧凑整洁。当前使用的代码如下:
plotdata <- structure( list( term = c("Category 1_a","Category 1_b","Category 1_c","Category 1_d","Category 2", "Category 3_a","Category 3_b"), estimate = c(-0.033882004,0.001508041,0.122957935,-0.033882004,0.001508041,-0.033882004,0.001508041), ymin = c( -0.13278953,-0.007547426,0.025116265,-0.13278953,-0.007547426,-0.13278953,-0.007547426), ymax = c(0.065025521,0.010563508,0.220799605,0.065025521,0.010563508,0.065025521,0.010563508) ), row.names = c(NA,-7L), spec = structure(list(cols = list( term = structure(list(), class = c("collector_character", "collector")), estimate = structure(list(), class = c("collector_double", "collector")), ymin = structure(list(), class = c("collector_double", "collector")), ymax = structure(list(), class = c("collector_double", "collector")) ), default = structure(list(), class = c("collector_guess", "collector")), delim = ","), class = "col_spec"),class = c("spec_tbl_df","tbl_df", "tbl", "data.frame")) # Libraries library(dplyr) library(broom) library(ggplot2) # Plot plotdata %>% mutate(category = rep(c("Pollution", "Roads", "Timing"), times = c(4, 1, 2))) %>% mutate(term = factor(term, levels = rev(term))) %>% ggplot(aes(x = term, y = estimate)) + geom_hline(yintercept = 0, color="black", size = 0.5, linetype = "dashed") + geom_pointrange(aes(ymin = ymin, ymax = ymax), size = 0.5, fill="#0202fe", color="#b5b5b5", shape=21, stroke = 1) + labs(x = NULL, y = "Coefficient Estimate") + scale_x_discrete(expand = c(0.4, 0.1)) + coord_flip() + facet_grid(category~., switch = 'y', scales = 'free_y') + theme_classic(base_size = 13) + theme(strip.placement = 'outside', strip.background = element_blank(), strip.text.y.left = element_text(face = 'bold', angle = 0, vjust = 1, hjust = 1), panel.spacing.y = unit(0, 'mm'))
解决方案
问题核心是scale_x_discrete()中的expand参数设置过大,导致每个分面内的分类项上下产生过多空白。调整该参数为更小的值即可解决问题。
修改后的代码
plotdata %>% mutate(category = rep(c("Pollution", "Roads", "Timing"), times = c(4, 1, 2))) %>% mutate(term = factor(term, levels = rev(term))) %>% ggplot(aes(x = term, y = estimate)) + geom_hline(yintercept = 0, color="black", size = 0.5, linetype = "dashed") + geom_pointrange(aes(ymin = ymin, ymax = ymax), size = 0.5, fill="#0202fe", color="#b5b5b5", shape=21, stroke = 1) + labs(x = NULL, y = "Coefficient Estimate") + # 调整expand参数,减少分类轴的空白 scale_x_discrete(expand = c(0.1, 0.05)) + coord_flip() + facet_grid(category~., switch = 'y', scales = 'free_y') + theme_classic(base_size = 13) + theme(strip.placement = 'outside', strip.background = element_blank(), strip.text.y.left = element_text(face = 'bold', angle = 0, vjust = 1, hjust = 1), panel.spacing.y = unit(0, 'mm'))
关键修改说明
- 将
scale_x_discrete(expand = c(0.4, 0.1))改为scale_x_discrete(expand = c(0.1, 0.05)):expand的两个数值分别控制轴两端的扩展比例,减小数值后,分类项与图表边缘的空白会显著减少,在Roads(仅1个分类项)和Timing(2个分类项)这类分类较少的区域效果尤为明显。 - 保留其他设置不变,确保图表的样式、分面布局等原有特性不受影响。
内容的提问来源于stack exchange,提问作者Neicooo
相关产品推荐
相关产品推荐

