You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot气泡图优化求助:标签放大、线条样式及轴标题字号调整

气泡图优化问题解决方案

原始问题

  1. 无法放大右上角紫色气泡的'Hamble'文本标签,尝试subset失败
  2. 代码设置红色虚线,但实际显示为实线
  3. 无法调整轴标题字体大小,仅能修改轴标签字号

原始图表:
当前气泡图

原始代码:

install.packages("ggrepel")
library(ggrepel)
install.packages("gghighlight")
library(gghighlight)
options(ggrepel.max.overlaps = Inf)
data %>%
  arrange(desc(PM10.2024)) %>%
  mutate(Name = factor(Name, Name)) %>%
  ggplot(aes(x=PM10.2024, y=PM2.5.2024, size = PM2.5.2024, fill=Classification)) +
  geom_point(alpha=0.6, shape=21, color="black") +
  scale_size(range = c(10,40), name=bquote(PM[2.5]))  +
  scale_fill_viridis(discrete=TRUE, guide=FALSE, option="D") +
  theme_ipsum() +
  xlab(expression(Concentration~PM[10])) +
  ylab(expression(Concentration~PM[2.5])) +
  theme(legend.position = "none") +
  theme(axis.text.x = element_text(size=12, angle = 0, vjust = 0.5, hjust=1)) +
  theme(axis.text.y = element_text(size=12, angle = 0, vjust = 0.5, hjust=1)) +
  geom_hline(aes(yintercept=7.9, linetype="dashed", color="red",))+
  geom_vline(aes(xintercept = 12.9, colour="red", linetype="dashed")) + 
  geom_text_repel(aes(label = Name), fontface=1, size = 3.9, family = "Calibri")+ #adds bubble labels
  scale_y_continuous(limits = c(5, 13))+
  scale_x_continuous(limits = c(5, 20))

数据结构:

structure(list(Quarry = c("Roke Manor, Romsey", "Grunden, Frith End",
"Blashford, Ringwood", "Masters Quarry", "Summerleaze, Uxbridge",
"Moreton C Cullimore Gravel, Swindon", "Aylesford Quarry ",
"Berkswell Quarry & Recovery Site ", "Bramshill Quarry & Concrete Plant ",
"Cromwell Quarry ", "Datchet Quarry and Recovery Site ", "Denge Quarry ",
"Dove Holes Quarry, Asphalt Plant & Dry Silo Mortar ", "East Leake Quarry ",
"Halkyn Quarry and Asphalt Plant ", "Hamer Warren Quarry and Recovery Site ",
"Hatfield Concrete Plant & Quarry ", "Hints Quarry ", "Kingsmead Concrete Plant and Quarry ",
"Rugeley Quarry ", "Ryall Concrete Plant and Quarry ", "Sandgate Quarry ",
"Spaunton Quarry ", "Swinderby Quarry ", "West Heath Quarry ",
"Willington Quarry ", "Hamble"), Operator = c("Raymond Brown",
"Grundon", "Tarmac", "NMSB", "Summerleaze", "Moreton", "CEMEX",
"CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX",
"CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX",
"CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX"), X.1x1 = c(432500L,
481500L, 414500L, 387500L, 504500L, 412500L, 573500L, 422500L,
478500L, 479500L, 499500L, 608500L, 408500L, 456500L, 320500L,
413500L, 518500L, 415500L, 502500L, 401500L, 386500L, 512500L,
471500L, 489500L, 478500L, 428500L, 447500L), Y.1x1 = c(122500L,
139500L, 107500L, 88500L, 184500L, 196500L, 159500L, 281500L,
159500L, 361500L, 177500L, 119500L, 377500L, 324500L, 372500L,
108500L, 208500L, 303500L, 175500L, 318500L, 240500L, 112500L,
486500L, 362500L, 122500L, 328500L, 110500L), Classification = c("Rural",
"Rural", "Rural", "Rural", "Urban", "Rural", "Urban", "Rural",
"Rural", "Rural", "Urban", "Rural", "Rural", "Rural", "Rural",
"Rural", "Urban", "Rural", "Urban", "Rural", "Rural", "Rural",
"Rural", "Rural", "Rural", "Rural", "Hamble"), UK.Urban.mean.in.2021 = c(7.9,
7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9,
7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, NA
), PM10.2023 = c(12.3, 12.1, 11.8, 12.3, 15.2, 13.1, 15.2, 12.2,
14, 15.7, 16.7, 11.4, 9.1, 14.2, 10.9, 11.5, 14.5, 13.4, 14,
10.2, 12.5, 13.8, 12.1, 14.4, 12.7, 13.5, NA), PM10.2024 = c(12.2,
12, 11.6, 12.2, 15, 12.9, 15, 12, 13.8, 15.6, 16.6, 11.3, 9.1,
14, 10.8, 11.4, 14.3, 13.3, 13.8, 10.1, 12.4, 13.6, 12, 14.3,
12.5, 13.3, 15.2), PM10.2025 = c(12, 11.8, 11.4, 12, 14.8, 12.8,
14.8, 11.9, 13.6, 15.4, 16.4, 11.1, 9, 13.9, 10.7, 11.2, 14.1,
13.2, 13.6, 10, 12.3, 13.4, 11.9, 14.1, 12.4, 13.2, NA), PM2.5.2023 = c(8.1,
8, 7.7, 7, 10.2, 8, 9.7, 7.7, 8.7, 8.4, 11.2, 7.5, 5.6, 8, 6.7,
7.6, 9.3, 8.2, 9.6, 6.6, 8, 8.3, 6.7, 8.1, 8, 8.2, NA), PM2.5.2024 = c(7.9,
7.9, 7.6, 6.9, 10.1, 7.9, 9.6, 7.6, 8.6, 8.3, 11.1, 7.4, 5.6,
7.9, 6.6, 7.5, 9.2, 8.1, 9.4, 6.5, 7.9, 8.2, 6.6, 8, 7.8, 8.1,
10.1), PM2.5.2025 = c(7.8, 7.8, 7.5, 6.8, 9.9, 7.8, 9.4, 7.5,
8.5, 8.2, 10.9, 7.2, 5.5, 7.8, 6.5, 7.3, 9, 8, 9.3, 6.4, 7.8,
8, 6.5, 7.9, 7.7, 8, NA), Name = c("Roke Manor", "Frith End",
"Blashford", "Masters", "Summerleaze", "Moreton", "Aylesford Quarry ",
"Berkswell", "Bramshill", "Cromwell", "Datchet", "Denge", "Dove Holes",
"East Leake", "Halkyn Quarry", "Hamer Warren", "Hatfield", "Hints",
"Kingsmead", "Rugeley", "Ryall", "Sandgate", "Spaunton", "Swinderby",
"West Heath", "Willington", "Hamble"), X = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), X.1 = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA,
-27L))

问题解决方法及修改后代码

1. 放大'Hamble'标签

解决方法:添加两个geom_text_repel层,一个用于所有普通大小标签,另一个单独过滤出'Hamble'数据并设置更大字号,分层处理避免同一层内的逻辑冲突。

2. 修复红色虚线显示

原因:将linetype和color放在aes()内会被ggplot识别为数据映射规则,而非直接的图形属性设置。
解决方法:将这两个参数移出aes(),直接作为geom_hline()/geom_vline()的参数传递。

3. 调整轴标题字号

解决方法:在theme()中添加axis.title.x和axis.title.y参数设置字号,注意要放在theme_ipsum()之后,避免被预设主题覆盖。


修改后完整代码

install.packages("ggrepel")
library(ggrepel)
install.packages("gghighlight")
library(gghighlight)
options(ggrepel.max.overlaps = Inf)

data %>%
  arrange(desc(PM10.2024)) %>%
  mutate(Name = factor(Name, Name)) %>%
  ggplot(aes(x=PM10.2024, y=PM2.5.2024, size = PM2.5.2024, fill=Classification)) +
  geom_point(alpha=0.6, shape=21, color="black") +
  scale_size(range = c(10,40), name=bquote(PM[2.5]))  +
  scale_fill_viridis(discrete=TRUE, guide=FALSE, option="D") +
  theme_ipsum() +
  xlab(expression(Concentration~PM[10])) +
  ylab(expression(Concentration~PM[2.5])) +
  # 统一设置主题属性,包括轴标题字号
  theme(
    legend.position = "none",
    axis.text.x = element_text(size=12, angle = 0, vjust = 0.5, hjust=1),
    axis.text.y = element_text(size=12, angle = 0, vjust = 0.5, hjust=1),
    axis.title.x = element_text(size=14), # 自定义X轴标题字号
    axis.title.y = element_text(size=14)  # 自定义Y轴标题字号
  ) +
  # 修复虚线:直接设置属性而非映射
  geom_hline(yintercept=7.9, linetype="dashed", color="red")+
  geom_vline(xintercept=12.9, linetype="dashed", color="red") + 
  # 普通大小标签
  geom_text_repel(aes(label = Name), fontface=1, size = 3.9, family = "Calibri")+
  # 单独放大Hamble标签
  geom_text_repel(
    data = subset(data, Name == "Hamble"), 
    aes(label = Name), 
    fontface=1, 
    size = 6, 
    family = "Calibri",
    color = "black"
  )+
  scale_y_continuous(limits = c(5, 13))+
  scale_x_continuous(limits = c(5, 20))

内容的提问来源于stack exchange,提问作者Vicky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:50:25