1

For the last week's TidyTuesday challenge, I know I am a bit late to ask this, I am trying to plot the BigTech companies' stock prices in a facetted graph. Nevertheless, some companies names are longer than others and I would like to wrap the text for these particular company names. Here is what I have achieved so far:

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)
  

which produced this plot

enter image description here

So, as can be seen from the plot, Cisco Systems and Intel have longer lengths compared to Adobe and Apple and therefore I would like to wrap the text. My question is how can I do that?

user438383
  • 5,716
  • 8
  • 28
  • 43
mzkrc
  • 219
  • 2
  • 7
  • Referred to: https://stackoverflow.com/questions/47446259/wrapping-long-geom-text-labels – Freakazoid Feb 20 '23 at 14:17
  • one simple option would be to substitute spaces with \n to create breaks. Otherwise related: https://stackoverflow.com/questions/25106508/ggplot2-is-there-an-easy-way-to-wrap-annotation-text?noredirect=1&lq=1 – tjebo Feb 20 '23 at 14:58
  • Another excellent thread links to some good solutions for very similar problems: https://stackoverflow.com/questions/65956833/ggplot2-how-to-dynamically-wrap-resize-rescale-x-axis-labels-so-they-wont-over – tjebo Feb 20 '23 at 15:08
  • I tried to replace the spaces with the new lines as suggested in this thread, however, it just did not work out because this time, the problem was that the labels were not shown up for some reason I could not understand. – mzkrc Feb 21 '23 at 08:10
  • No worries, I just found out a solution to solve that by modifying the geom_text function. – mzkrc Feb 21 '23 at 08:43

1 Answers1

0

You can use str_wrap (via stringr) to force the width of the text stringr.tidyverse.org/reference/str_wrap.html OR...

You can replace the spaces in the company names with new lines prior to plotting as I've done here:

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"))

enter image description here

  • When I add the first code to replace spaces with new lines, the labels that were supposed to be shown into the plot as company names do not exist. I could not understand what the problem would be. PS. I wanted to add the screenshot, but it does not allow me to do so. Can you confirm that problem as I already provided the code I have? – mzkrc Feb 21 '23 at 07:01
  • No worries, I just found out a solution to solve that by modifying the geom_text function. – mzkrc Feb 21 '23 at 08:44
  • I had to change your code a bit to get this to work but the main issue was that if you change the "company" column by adding spaces dataframe, you also need to update your other references to that column in your figure (ex/ gghighlight was still referring to c("Apple Inc") with a space rather than a "\n"). So you either need to give the new column a new name for the geom_text ("company2") or use the new format in gghighlight (ex/ "Apple\nInc") which you can extract with for_plot <- unique(big_tech_stock_prices$company) and then use gghighlight(company %in% c(for_plot),... – alexandradevon Feb 21 '23 at 15:23