R语言 如何在ggplot中使文本换行?

u5rb5r59  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(301)

对于上周的TidyTuesday挑战,我知道我问这个有点晚了,我试图用一个多面体图来绘制BigTech公司的股价。尽管如此,一些公司的名称比其他公司的名称长,我想把这些特定公司名称的文本打包。以下是我到目前为止取得的成果:

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)

产生了这幅图

从图中可以看出,思科系统和英特尔的长度比Adobe和Apple长,因此我想把文本换行,我的问题是我怎么做?

31moq8wy

31moq8wy1#

您可以使用str_wrap(通过stringr)强制文本www.example.com的宽度stringr.tidyverse.org/reference/str_wrap.html或者...
您可以在绘图之前用新行替换公司名称中的空格,就像我在这里所做的那样:

big_tech_stock_prices$company2 <- gsub(' ', ' \n',big_tech_stock_prices$company)

plot <- ggplot(big_tech_stock_prices, aes(x = date, y = close)) +geom_line(aes(x = date, y = close, color=company)) + 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.2, colour = "gray50" , aes(color=company2))) + 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 = 400, label = company2, color = company2), size = 4) +  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 = 11, linewidth = 0.2),  axis.title.x = element_markdown(family = "axis", size = 11, linewidth = 0.2), axis.text.y = element_markdown(family = "axis", size = 10),axis.text.x = element_markdown(family = "axis", hjust = 0.43, size = 10),  plot.title = element_markdown(family = "title", size = 12, hjust = 0.5, lineheight = 0.15, linewidth = 0.1),   plot.subtitle = element_markdown(family = "subtitle", size = 11, hjust = 0.5, lineheight = 0.15, linewidth = 0.1),  plot.caption = element_markdown(family = "title", size = 11, lineheight = 0.15, linewidth = 0.1, hjust = 0.5),  plot.background = element_rect(fill = "white", color = "white")) + theme(plot.margin=unit(c(1,1,1,1),"cm"))

相关问题