Specific label in bold (ggplot2)

Hi,

I've been trying to find a way how to make (only) my 'Česká rep.' bold yet unsuccessfuly. Do you have any suggestions?

data_wide <- structure(list(Country = c("Greece", "Lithuania", "Finland", 
                                        "Estonia", "Croatia", "Netherlands", "France", "Denmark", "Portugal", 
                                        "Belgium", "Luxembourg", "Hungary", "Germany", "Slovakia", "Austria", 
                                        "Sweden", "Czechia", "Ireland", "Romania", "Italy", "Slovenia", 
                                        "Spain", "Bulgaria", "Poland", "Latvia"), 
                            CZE = structure(c(25L, 24L, 23L, 22L, 21L, 20L, 19L, 17L, 18L, 15L, 16L, 14L, 13L, 12L, 
                                           11L, 10L, 8L, 9L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), 
                                           levels = c("Lotyšsko", "Polsko", "Bulharsko", "Španělsko", "Slovinsko", "Itálie", 
                                               "Rumunsko", "Česká rep.", "Irsko", "Švédsko", "Rakousko", 
                                                "Slovensko", "Německo", "Maďarsko", "Belgie", "Lucembursko", 
                                               "Dánsko", "Portugalsko", "Francie", "Nizozemsko", "Chortvatsko", 
                                          "Estonsko", "Finsko", "Litva", "Řecko"), class = "factor"), 
                            `2009` = c(8.3, 13.1, 10, 11.9, 11.1, 9.4, 12.6, 10.6, 11.5, 
                                       10.1, 11.9, 11.5, 11.2, 10.7, 11.8, 7.3, 12.1, 11, 11, 7.3, 
                                       10.5, 10, 10.5, 10.2, 9.9), `2019` = c(6.3, 11.1, 8.2, 10.4, 
                                                                              9.6, 8.2, 11.4, 9.5, 10.4, 9.2, 11, 10.7, 10.6, 10.3, 11.6, 
                                                                              7.1, 11.9, 10.8, 11, 7.7, 11.1, 10.7, 11.2, 11, 11.6), 
                            rozdil = c(-2, -2, -1.8, -1.5, -1.5, -1.2, -1.2, -1.1, -1.1, -0.9, -0.9, 
                                        -0.800000000000001, -0.6, -0.399999999999999, -0.200000000000001, 
                                      -0.2, -0.199999999999999, -0.199999999999999, 0, 0.4, 0.6, 
                                            0.699999999999999, 0.699999999999999, 0.800000000000001, 1.7)), 
                       row.names = c(NA, -25L), class = c("tbl_df", "tbl", "data.frame"))
                                                                          
library(tidyverse)

ggplot(data_wide, aes(x = rozdil, y = CZE)) +
  geom_col(width = 0.9, fill = if_else(data_wide$CZE == "Česká rep.","#ECB925", "#00254B")) +
  theme_minimal() + 
  theme(legend.position = 'none',
        legend.margin=margin(0,0,0,0),
        legend.title = element_blank(),
        text = element_text(size = 15),
        axis.text.x = element_text(size = 13),
        axis.text.y = element_text(size = 13,
                                   angle = 0,
                                   hjust = 0.7,
                                   face = ifelse(data_wide$CZE == "Česká rep.", "bold", "plain")),
        axis.title.x = element_text(size = 11.5),
        plot.margin = margin(10, 10, 10, 10),
        axis.title.y = element_blank(),
        panel.spacing = unit(2, "lines"),
        panel.grid.major.y = element_blank()) +
  scale_x_continuous(name = "změna ve spotřebě alkoholu mezi lety 2009 a 2019",
                     label = scales::comma_format(accuracy = 1, scale = 1, prefix = "", suffix = "",
                                                  big.mark = " ", decimal.mark = ","))
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> ℹ Results may be unexpected or may change in future versions of ggplot2.

Screenshot 2023-08-23 at 11.50.45

Many thanks!

Hi @Jakub_Komarek! Below is one approach that uses the ggtext package to bold the desired CZE value. First, in the data, CZE is mutated to add the <b></b> tags. Then, since the factor was changed to character to add these tags, the data is arranged and CZE is set as an ordered factor again. Next, geom_col() is updated to look for the newly changed CZE value for proper color fill. Finally, axis.text.y() is updated to use element_markdown().

library(tidyverse)
library(ggtext)

data_wide = data_wide |>
  mutate(CZE = ifelse(CZE == "Česká rep.", "<b>Česká rep.</b>", as.character(CZE))) |> 
  arrange(desc(rozdil)) |>
  mutate(CZE = factor(CZE, levels = CZE, ordered = T)) # 

ggplot(data_wide, aes(x = rozdil, y = CZE)) +
  geom_col(width = 0.9, fill = if_else(data_wide$CZE == "<b>Česká rep.</b>","#ECB925", "#00254B")) +
  theme_minimal() + 
  theme(legend.position = 'none',
        legend.margin=margin(0,0,0,0),
        legend.title = element_blank(),
        text = element_text(size = 15),
        axis.text.x = element_text(size = 13),
        axis.text.y = element_markdown(size = 13,
                                       angle = 0,
                                       hjust = 0.7),
        axis.title.x = element_text(size = 11.5),
        plot.margin = margin(10, 10, 10, 10),
        axis.title.y = element_blank(),
        panel.spacing = unit(2, "lines"),
        panel.grid.major.y = element_blank()) +
  scale_x_continuous(name = "změna ve spotřebě alkoholu mezi lety 2009 a 2019",
                     label = scales::comma_format(accuracy = 1, scale = 1, prefix = "", suffix = "",
                                                  big.mark = " ", decimal.mark = ","))

Created on 2023-08-23 with reprex v2.0.2

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.