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

如何在R ggplot中处理散点图的过长图例标签?

解决ggplot图例长文本换行问题,保持单图可读性

针对长行业名称导致图例拥挤、缩小字号又不可读的问题,以下两种方案可在单图中实现图例文本自动换行,同时保证可读性:

方案一:预处理数据,给行业名称添加换行符

使用stringr包的str_wrap()函数提前拆分长行业名称,后续绘图直接使用处理后的字段,适合需要多次复用处理后名称的场景。

修改后的完整代码

library(ggplot2)
library(dplyr)
library(stringr)

# 加载数据集
dataset <- structure(list(Dates = c("2021-12-31", "2021-12-31", "2021-12-31", 
"2021-12-31", "2021-12-31", "2021-12-31", "2021-12-31", "2021-12-31", 
"2021-12-31", "2021-12-31", "2021-12-31", "2021-12-31", "2021-12-31", 
"2021-12-31", "2021-12-31"), Firm = c("BRYAT", "ZRGYO", "KONTR", 
"TRGYO", "DAPGM", "INVES", "ECZYT", "GWIND", "KMPUR", "SNGYO", 
"BERA", "ISGYO", "ALKA", "YGGYO", "SRVGY"), Sector = c("FINANCIAL AND INSURANCE ACTIVITIES", 
"FINANCIAL AND INSURANCE ACTIVITIES", "INFORMATION AND COMMUNICATION", 
"REAL ESTATE ACTIVITIES", "REAL ESTATE ACTIVITIES", "FINANCIAL AND INSURANCE ACTIVITIES", 
"FINANCIAL AND INSURANCE ACTIVITIES", "MANUFACTURING", "MANUFACTURING", 
"REAL ESTATE ACTIVITIES", "MANUFACTURING", "REAL ESTATE ACTIVITIES", 
"MANUFACTURING", "FINANCIAL AND INSURANCE ACTIVITIES", "FINANCIAL AND INSURANCE ACTIVITIES"
), `Number of Employees` = c(9, 32, 226, 144, 138, 6, 3, 50, 
219, 194, 33, 69, 208, 138, 11), EBITDA = c(23.8137, 1113.5212, 
113.9684, 6517.373, 387.6152, 1624.0192, -7.8884, 420.6019, 280.8024, 
4648.4098, 752.1899, 153.6431, 138.7504, 194.0958, 144.2635), 
    `Profit Margin` = c(574.7712, 706.7635, 21.3576, 357.6667, 
    34.2274, 537.1883, 372.8143, 31.6373, 14.5637, 150.702, 17.7025, 
    48.8762, 26.2978, 252.0494, 162.3127), Sales = c(47.7833, 
    186.8011, 611.8077, 1483.729, 912.4001, 312.5418, 0, 540.2263, 
    2172.6885, 2123.4395, 4217.1842, 545.2598, 700.9869, 277.6963, 
    414.2354), `Market Cap` = c(10681.875, 11405.4966, 2474.0625, 
    3920, NA, NA, 5407.5, 3123.1821, NA, 3728.536, 3320.352, 
    2435.225, 859.425, 3386.88, 4695.6), `Personnel Expense (Millions)` = c(3.4716, 
    8.4842, 7.7846, 11.869, 14.3014, 1.1789, 0.4507, 17.2696, 
    55.9705, 25.5093, 308.7577, 20.0843, 33.358, 10.5289, 14.3268
    ), `Personnel Expense Per Employee` = c(385733.5556, 265130.0938, 
    34445.1726, 82423.6111, 103633.6739, 196478.3333, 150236.6667, 
    345392.94, 255573.0411, 131491.1082, 9356294.7879, 291076.971, 
    160374.9663, 76296.7319, 1302436.6364), `Price/Earnings` = c(38.8935, 
    7.8922, 18.934, 0.7387, NA, NA, 12.5925, 18.2735, NA, 1.1651, 
    4.4476, 9.1377, 4.6621, 4.6933, 6.9838)), row.names = c(NA, 
-15L), class = c("tbl_df", "tbl", "data.frame"))

# 预处理Sector字段,设置换行宽度为15
ggplot(dataset %>%
         filter(Dates == "2021-12-31" & `Number of Employees` < 250) %>%
         mutate(Sector = str_wrap(Sector, width = 15)))+
  geom_point(aes(x = `Number of Employees`, y = `Profit Margin`, color = Sector), show.legend = TRUE)+
  lims(y = c(-100,200))+
  theme(legend.text = element_text(size = 10, hjust = 0.5))

方案二:直接在图例标签中设置换行

无需修改原始数据集,通过scale_color_discrete()的labels参数单独处理图例标签,仅影响当前图表显示。

修改后的完整代码

library(ggplot2)
library(dplyr)
library(stringr)

# 加载数据集(同上)
dataset <- structure(list(...)) # 实际使用时保留完整数据集结构

ggplot(dataset %>%
         filter(Dates == "2021-12-31" & `Number of Employees` < 250))+
  geom_point(aes(x = `Number of Employees`, y = `Profit Margin`, color = Sector), show.legend = TRUE)+
  lims(y = c(-100,200))+
  scale_color_discrete(labels = function(x) str_wrap(x, width = 15))+
  theme(legend.text = element_text(size = 10, hjust = 0.5))

关键说明

  • 可根据需求调整*width*参数(比如12或18),控制每行显示的字符数,找到最优换行效果。
  • 若使用ggplot2 3.4.0及以上版本,可直接在element_text()中用wrap = TRUE参数简化设置:
    theme(legend.text = element_text(size = 10, wrap = TRUE, width = 15, lineheight = 1.2))
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 19:35:33