如何在ggplot中实现文本换行?大科技公司股价分面图优化
问题描述
我参与了上周的TidyTuesday挑战,正在绘制大科技公司股价的分面图。部分公司名称过长(比如Cisco Systems, Inc.、Intel Corporation),希望对这些特定的公司名称文本进行换行处理。以下是我目前的代码:
library(tidyverse) library(showtext) library(patchwork) library(janitor) library(glue) library(ggtext) library(gghighlight) library(lubridate) font_add_google("Archivo", family = "title") font_add_google("Nunito", family = "subtitle") font_add_google("Martel", family = "axis") font_add_google("Spartan", family = "caption") font_add('fa-reg', 'fonts/Font Awesome 6 Free-Regular-400.otf') font_add('fa-brands', 'fonts/Font Awesome 6 Brands-Regular-400.otf') font_add('fa-solid', 'fonts/Font Awesome 6 Free-Solid-900.otf') showtext_auto() dat <- tidytuesdayR::tt_load(2023, week = 6) big_tech_stock_prices <- dat$big_tech_stock_prices big_tech_company_name <- dat$big_tech_companies big_tech_stock_prices <- big_tech_stock_prices %>% left_join(big_tech_company_name, by = "stock_symbol") %>% mutate(company = case_when(company == "International Business Machines Corporation" ~ "IBM", TRUE ~ company)) plot <- ggplot(big_tech_stock_prices, aes(x = date, y = close, group = company)) + geom_line() + gghighlight(company %in% c("Apple Inc.", "Adobe Inc.", "Amazon.com, Inc.", "Salesforce, Inc.", "Cisco Systems, Inc.", "Alphabet Inc.", "IBM", "Intel Corporation", "Meta Platforms, Inc.", "Microsoft Corporation", "Netflix, Inc.", "NVIDIA Corporation", "Oracle Corporation", "Tesla, Inc."), use_direct_label = FALSE, unhighlighted_params = list(linewidth = 0.1, colour = alpha("grey20", 0.3))) + geom_area(aes(date, close, colour = stock_symbol, fill = stock_symbol), big_tech_stock_prices, alpha = 0.2, size = 0.5) + geom_text( aes(x = lubridate::ymd("2014-01-01"), y = 500, label = company, color = company), size = 30) + facet_wrap(~company) + theme_minimal() + labs(title = "Stock Market Values for BigTech Companies (2010-2022)", subtitle = "The plot demonstrates the stock market value for 14 BigTech companies. Note that the values<br>displays the closed prices for each days", y = "US Dollar - $", x = "") + theme(legend.position = "none", strip.background = element_blank(), strip.text = element_blank(), axis.title.y = element_markdown(family = "axis", size = 55, linewidth = 0.2), axis.title.x = element_markdown(family = "axis", size = 55, linewidth = 0.2), axis.text.y = element_markdown(family = "axis", size = 35), axis.text.x = element_markdown(family = "axis", hjust = 0.43, size = 35), plot.title = element_markdown(family = "title", size = 95, hjust = 0.5, lineheight = 0.15, linewidth = 0.1), plot.subtitle = element_markdown(family = "subtitle", size = 75, hjust = 0.5, lineheight = 0.15, linewidth = 0.1), plot.caption = element_markdown(family = "title", size = 45, lineheight = 0.15, linewidth = 0.1, hjust = 0.5), plot.background = element_rect(fill = "white", color = "white")) ggsave("deneme.png", height = 6, width = 7.5, dpi = 720)

从图表可见,长名称的公司文本超出了分面区域,需要换行优化。
解决方案
方法1:手动替换特定公司名称,添加换行符
在数据处理阶段,针对需要换行的公司名称插入\n换行符,同时调整文本对齐和行间距:
# 修改数据处理部分的mutate逻辑 big_tech_stock_prices <- big_tech_stock_prices %>% left_join(big_tech_company_name, by = "stock_symbol") %>% mutate(company = case_when( company == "International Business Machines Corporation" ~ "IBM", company == "Cisco Systems, Inc." ~ "Cisco Systems\nInc.", company == "Intel Corporation" ~ "Intel\nCorporation", company == "Microsoft Corporation" ~ "Microsoft\nCorporation", # 其他需要换行的公司可继续添加 TRUE ~ company )) # 修改geom_text的参数,优化换行后的显示 geom_text( aes(x = lubridate::ymd("2014-01-01"), y = 500, label = company, color = company), size = 30, hjust = 0.5, # 居中对齐文本 lineheight = 0.8 # 调整换行后的行间距 )
方法2:结合ggtext使用富文本换行(更灵活)
利用已加载的ggtext包,用<br>标签实现换行,并替换geom_text为支持富文本的geom_richtext:
# 修改公司名称为带<br>的格式 big_tech_stock_prices <- big_tech_stock_prices %>% left_join(big_tech_company_name, by = "stock_symbol") %>% mutate(company = case_when( company == "International Business Machines Corporation" ~ "IBM", company == "Cisco Systems, Inc." ~ "Cisco Systems<br>Inc.", company == "Intel Corporation" ~ "Intel<br>Corporation", TRUE ~ company )) # 替换geom_text为geom_richtext,去掉背景和边框保持原风格 geom_richtext( aes(x = lubridate::ymd("2014-01-01"), y = 500, label = company, color = company), size = 30, hjust = 0.5, fill = NA, # 移除背景色 label.color = NA, # 移除边框 lineheight = 0.8 )
方法3:自动批量处理长文本(可选)
如果有大量长名称,可使用stringr的str_wrap函数自动按指定宽度换行:
library(stringr) big_tech_stock_prices <- big_tech_stock_prices %>% left_join(big_tech_company_name, by = "stock_symbol") %>% mutate(company = case_when( company == "International Business Machines Corporation" ~ "IBM", str_length(company) > 15 ~ str_wrap(company, width = 12), # 长度超过15的自动换行 TRUE ~ company ))
内容的提问来源于stack exchange,提问作者mzkrc
相关产品推荐
相关产品推荐

