R语言绘制适用于PDF导出的美观四象限气泡图求助
四象限气泡图ggplot2优化方案
问题背景
需绘制美观的四象限气泡图,已用ggplot2实现基础效果,但存在颜色过淡、气泡尺寸过小的问题;尝试plotly未成功,且因最终需导出为PDF(要求静态显示标签,无需交互),plotly并非最优选择。
优化后的代码
针对上述问题,对原ggplot2代码进行调整,强化视觉效果并添加静态标签,完美适配PDF导出:
library(ggplot2) library(dplyr) # 可选:给数据集添加象限标签,强化四象限区分 plot_3D <- plot_3D %>% mutate(quadrant = case_when( actes_evolution_2020_2021 > 0 & tx_0_nuit_2021 > 0 ~ "Q1", actes_evolution_2020_2021 < 0 & tx_0_nuit_2021 > 0 ~ "Q2", actes_evolution_2020_2021 < 0 & tx_0_nuit_2021 < 0 ~ "Q3", actes_evolution_2020_2021 > 0 & tx_0_nuit_2021 < 0 ~ "Q4" )) p <- ggplot(plot_3D, aes(x = actes_evolution_2020_2021, y = tx_0_nuit_2021)) + # 添加象限背景色,增强视觉区分度 geom_rect(aes(xmin = -Inf, xmax = 0, ymin = 0, ymax = Inf), fill = "#E8F4F8", alpha = 0.2) + geom_rect(aes(xmin = 0, xmax = Inf, ymin = 0, ymax = Inf), fill = "#E8F8E8", alpha = 0.2) + geom_rect(aes(xmin = -Inf, xmax = 0, ymin = -Inf, ymax = 0), fill = "#F8E8E8", alpha = 0.2) + geom_rect(aes(xmin = 0, xmax = Inf, ymin = -Inf, ymax = 0), fill = "#F8F4E8", alpha = 0.2) + # 气泡:移除半透明解决颜色淡问题,绑定分组色和气泡大小 geom_point(aes(color = regroupement, size = actes_2021)) + # 更换为更饱和的配色方案 scale_color_manual(values = c("#009E73", "#E69F00", "#D55E00", "#CC79A7", "#56B4E9", "#0072B2")) + # 增大气泡尺寸范围,强化类别间差异 scale_size(range = c(6, 16)) + # 坐标轴参考线,加粗增强存在感 geom_hline(yintercept = 0, color = "black", linewidth = 1) + geom_vline(xintercept = 0, color = "black", linewidth = 1) + # 坐标轴格式调整 scale_y_continuous(labels = ~ paste0(.x, '%'), limits = c(-100, 100)) + scale_x_continuous(labels = ~ paste0(.x, '%'), limits = c(-100, 100)) + # 添加静态标签,直接显示分组名称(适配PDF静态需求) geom_text(aes(label = regroupement), hjust = 0.5, vjust = -1.2, size = 4) + # 图表标签配置 labs( x = "évolution du nombre d'actes entre 2020 et 2021", y = "taux de 0 nuits en 2021", color = "Regroupement", size = "Actes 2021" ) + # 主题优化提升美观度 theme_minimal() + theme( axis.title = element_text(size = 12), legend.position = "right", legend.title = element_text(face = "bold"), panel.grid = element_line(color = "#E0E0E0", linewidth = 0.5) ) p # 导出为PDF ggsave("quadrant_bubble_plot.pdf", p, width = 10, height = 8, dpi = 300)
关键修改说明
- 解决颜色过淡:移除原代码中的
alpha=0.5参数,同时更换为高饱和配色,让气泡颜色更鲜明 - 增大气泡尺寸:将
scale_size的范围从c(5,12)调整为c(6,16),强化不同类别间的尺寸差异 - 添加静态标签:使用
geom_text直接显示分组名称,满足PDF静态展示需求 - 四象限区分:添加半透明背景色,让四个象限的视觉边界更清晰
- 主题优化:采用
theme_minimal()并调整网格线、字体样式,提升整体美观度
复现数据集
plot_3D <- structure(list(code_1 = c("M01b", "C01", "C02", "C04", "M01", "Total"), regroupement = c("Endoscopies digestives", "Ortho (+ rhumato et rachis)", "Chirurgie digestive", "Uro-néphro", "Gastro", "autres"), actes_2019 = c(36079, 29520, 14618, 6515, 9179, 33707), actes_2020 = c(30192, 25451, 12845, 7376, 7933, 24154), actes_2021 = c(42333, 24055, 13735, 8196, 6504, 26115), tx_0_nuit_2019 = c(96.0955377574371, 63.5718186783179, 41.4435389988359, 36.2817362817363, 3.92397302268547, 783.752765264434 ), tx_0_nuit_2020 = c(96.0936155567028, 67.3069146785281, 40.5855855855856, 34.344262295082, 3.29588014981273, 895.206473285694), tx_0_nuit_2021 = c(96.7053774299419, 73.2892690513219, 51.0503369005153, 41.9021364576154, 3.6068530207394, 849.200890941822), actes_evolution_2019_2020 = c(-16.3169710912165, -13.7838753387534, -12.1288822000274, 13.2156561780507, -13.5744634491775, -28.3412940932151), actes_evolution_2020_2021 = c(40.2126391096979, -5.48504970335154, 6.92876605683145, 11.117136659436, -18.0133619059624, 8.11873809720957)), row.names = c(NA, 6L), class = "data.frame")
内容的提问来源于stack exchange,提问作者gerardlambert
相关产品推荐
相关产品推荐

