rggplot2ggrepel

Making only part of a label bold while using geom_label_repel() in ggplot


I have a dataset:

> dput(df.inn3)
structure(list(PC1 = c(-0.148688238177717, -0.908694225222203, 
1.15048295817949, -0.403789747957251, 0.785402066942118), PC2 = c(0.371103940258495, 
-1.20385188925406, -0.357165542408208, 0.707690895976991, -0.447999850097107
), distances = c(0.399782849366721, 1.50830519664051, 1.20464860508996, 
0.814783753399696, 0.904190396124612), label = c("<b>Innovation need:</b>  Improving\ncharacterization of\nnon-traditional anglers", 
"<b>Innovation need:</b>  Improving\nefficiency", "<b>Innovation need:</b>  Gathering\nself-reporting recreational data", 
"<b>Innovation need:</b>  Improving\ncharacterization of preferences\nand behaviors", 
"<b>Innovation need:</b>  Using emerging\ntechnology")), row.names = c("Innovation need: Improving\ncharacterization of\nnon-traditional anglers", 
"Innovation need: Improving\nefficiency", "Innovation need: Gathering\nself-reporting recreational data", 
"Innovation need: Improving\ncharacterization of preferences\nand behaviors", 
"Innovation need: Using emerging\ntechnology"), class = "data.frame")
> dput(df.imm3)
structure(list(PC1 = c(-1.23590280262104, 0.201388002767894, 
0.914977218413494, 0.126721368707057, 0.0872621215582316, 0.204537959161584
), PC2 = c(0.840721410891728, 0.0708437839704083, 1.18262760256924, 
-0.323000400083109, -0.353599966664034, 0.00713742281189857), 
    distances = c(1.49474687765465, 0.213485290795612, 1.49525628457946, 
    0.34696911064364, 0.364208201834678, 0.204662452693164), 
    label = c("<b>Import:</b>  Characterizing angler\npreferences and behaviors", 
    "<b>Import:</b>  Improving public\ninterface", "<b>Import:</b>  Making management\ndecisions", 
    "<b>Import:</b>  Measuring fishery\nperformance", "<b>Import:</b>  Monitoring species of\nconcern", 
    "<b>Import:</b>  Measuring exploitation")), row.names = c("Import: Characterizing angler\npreferences and behaviors", 
"Import: Improving public\ninterface", "Import: Making management\ndecisions", 
"Import: Measuring fishery\nperformance", "Import: Monitoring species of\nconcern", 
"Import: Measuring exploitation"), class = "data.frame")
> dput(df.pca)
structure(list(PC1 = c(-0.0141206273717713, -0.209463263804952, 
0.0074598774923903, 0.197873115466226, -0.0201814499150566, 0.197873115466226, 
-0.0815799733773477, -0.0488464843658022, -0.209463263804952, 
0.20544862737961, 0.197873115466226, -0.0141206273717715, -0.00654511545838752, 
0.00745987749238988, -0.209463263804952, -0.00654511545838752, 
-0.209463263804952, -0.0488464843658022, -0.209463263804952, 
0.12776765600661, 0.197873115466226, -0.0488464843658022, -0.0488464843658022, 
-0.0488464843658022, -0.25819411374976, -0.0114745139177322, 
-0.0488464843658022, 0.197873115466226, -0.209463263804952, -0.229529079299015, 
-0.209463263804952, -0.0141206273717715, -0.0114745139177322, 
-0.0141206273717715, 0.197873115466226, 0.197873115466226, 0.197873115466226, 
-0.209463263804952, 1.60010997514765e-05, -0.0141206273717715, 
-0.209463263804952, 0.12776765600661, 0.116408776509872, -0.209463263804952, 
-0.0141206273717715, -0.209463263804952, -0.0488464843658022, 
0.100245741207939, 0.197873115466226, 0.32575640589383, -0.0488464843658022, 
-0.0141206273717715, 0.32575640589383, 0.12776765600661, -0.00757173372560303, 
0.12776765600661, -0.25819411374976, 0.32575640589383, -0.0488464843658022, 
-0.209463263804952, -0.0488464843658022, -0.209463263804952, 
-0.0890360726819566, -0.216919363109561, 0.32575640589383, -0.0488464843658022, 
0.197873115466226, -0.0114745139177322, -0.00757173372560303, 
-0.00757173372560303, -0.0141206273717715, -0.209463263804952, 
-0.209463263804952, 0.197873115466226, 0.12776765600661, 0.197873115466226, 
0.32575640589383, -0.209463263804952, -0.0488464843658022, -0.130310823322156, 
0.116408776509872, -0.0815799733773477, -0.25819411374976, -0.0114745139177322, 
-0.0488464843658022, -0.0815799733773477, -0.209463263804952, 
0.32575640589383, 0.32575640589383, -0.229529079299015, -0.22346825675573, 
0.32575640589383, 0.197873115466226, -0.095584966328125, -0.209463263804952, 
-0.0488464843658022, -0.229529079299015, -0.0628514773165796, 
0.12776765600661, 0.12776765600661, 0.32575640589383, 0.12776765600661, 
-0.209463263804952, 0.32575640589383, -0.252832989072648, 0.033235578332924, 
-0.0135684775992678, -0.252832989072647, -0.270045628592021, 
-0.252832989072647, 0.229914762652247, -0.252832989072647, -0.00953344360972124, 
-0.253016683861236, -0.0305974223300524, -0.236943656174202, 
0.245804095550691, 0.00250455008776582, -0.00724164915480696, 
-0.0145153991165325, -0.254156295693576, 0.229731067863658, 0.245804095550691, 
-0.252832989072647, -0.208038044199645, -0.00971713839831004, 
-0.252832989072647, -0.0265521344527103, -0.00953344360972124, 
-0.252832989072647, -0.00724164915480696, -0.00724164915480696, 
-0.00953344360972124, -0.0135684775992677, 0.27653512379585, 
-0.00339030995384935, 0.274893402313837, 0.014381217331538, 0.230069918824527, 
-0.27004562859202, 0.290782735212282, 0.274893402313837, 0.229914762652247, 
-0.00953344360972124, -0.252832989072647, -0.00724164915480696, 
0.0330518835443355, -0.0139271145620746, 0.00250455008776582, 
-0.283212980234052, 0.229731067863658, -0.252832989072647, 0.253425691999773, 
-0.0265521344527103, 0.228591456031318, 0.274893402313837, -0.0145153991165325, 
-0.00724164915480696, -0.0267460831290948, -0.00724164915480696, 
0.27653512379585, 0.274893402313837, -0.252832989072647, -0.191965016512612, 
-0.00724164915480696, -0.00953344360972124, 0.00250455008776582, 
-0.00953344360972124, -0.00953344360972124, 0.00635588928872343, 
-0.252832989072647, -0.252832989072647, -0.00724164915480696, 
-0.252832989072647, 0.27653512379585, 0.0315938568509116, 0.27653512379585, 
-0.00724164915480696, 0.0354451960518692, -0.00953344360972124, 
0.229914762652247, 0.229914762652247, 0.0315938568509116, -0.190323295030599, 
0.212702123132873, -0.252832989072647, 0.037086917533882, -0.252832989072647, 
0.0474831897493562, -0.27004562859202, 0.0354451960518692, 0.0354451960518692, 
-0.00953344360972124, -0.00953344360972124, 0.229914762652247, 
0.229914762652247, 0.0302705502299826, 0.27653512379585, 0.00250455008776582, 
0.245804095550691, -0.252832989072647, 0.229914762652247, -0.236943656174202, 
-0.0147080894316077, 0.228591456031318, -0.00953344360972124, 
-0.0265521344527103), PC2 = c(0.203829248209386, -0.127203726947651, 
0.147296661133637, 0.00171278711832731, 0.101864889975649, 0.00171278711832772, 
-0.251127222987841, 0.286980948313469, -0.127203726947651, 0.0329199061713358, 
0.00171278711832778, 0.203829248209385, 0.235036367262393, 0.147296661133638, 
-0.127203726947651, 0.235036367262393, -0.127203726947651, 0.286980948313469, 
-0.127203726947651, -0.00783395395956061, 0.00171278711832778, 
0.286980948313469, 0.286980948313469, 0.286980948313469, 0.0436876792851882, 
-0.241580481909953, 0.286980948313469, 0.00171278711832778, -0.127203726947651, 
-0.141428379052632, -0.127203726947651, 0.203829248209385, -0.241580481909953, 
0.203829248209385, 0.00171278711832778, 0.00171278711832778, 
0.00171278711832778, -0.127203726947651, 0.120779035400652, 0.203829248209385, 
-0.127203726947651, -0.00783395395956061, -0.365503977950143, 
-0.127203726947651, 0.203829248209385, -0.127203726947651, 0.286980948313469, 
-0.0250981765713009, 0.00171278711832778, -0.122210708921862, 
0.286980948313469, 0.203829248209385, -0.122210708921862, -0.00783395395956061, 
0.11304997157387, -0.00783395395956061, 0.0436876792851882, -0.122210708921862, 
0.286980948313469, -0.127203726947651, 0.286980948313469, -0.127203726947651, 
-0.2541667934946, -0.13024329745441, -0.122210708921862, 0.286980948313469, 
0.00171278711832778, -0.241580481909953, 0.11304997157387, 0.11304997157387, 
0.203829248209385, -0.127203726947651, -0.127203726947651, 0.00171278711832778, 
-0.00783395395956061, 0.00171278711832778, -0.122210708921862, 
-0.127203726947651, 0.286980948313469, -0.080235816755002, -0.365503977950143, 
-0.251127222987841, 0.0436876792851882, -0.241580481909953, 0.286980948313469, 
-0.251127222987841, -0.127203726947651, -0.122210708921862, -0.122210708921862, 
-0.141428379052632, -0.0394640208188951, -0.122210708921862, 
0.00171278711832778, -0.163387516859085, -0.127203726947651, 
0.286980948313469, -0.141428379052632, 0.374720654442225, -0.00783395395956061, 
-0.00783395395956061, -0.122210708921862, -0.00783395395956061, 
-0.127203726947651, -0.122210708921862, 0.0512592612275564, -0.177101873096904, 
-0.189260764541249, 0.0512592612275568, 0.0184432352823786, 0.0512592612275568, 
0.0537506715078537, 0.0512592612275567, 0.291291955043046, 0.0482805189939425, 
-0.219098048252813, -0.0276564079786102, -0.0251649976983132, 
-0.265197691513802, -0.270848502252605, -0.201352649568953, -0.0604724339237883, 
0.0507719292742395, -0.0251649976983132, 0.0512592612275567, 
0.0426706239725855, 0.288313212809431, 0.0512592612275567, -0.210164910991586, 
0.291291955043046, 0.0512592612275567, -0.270848502252605, -0.270848502252605, 
0.291291955043046, -0.189260764541249, 0.0629308207185855, 0.206725475098076, 
0.0481407764864968, -0.22470794327417, 0.0206426883617808, 0.0184432352823787, 
-0.0307748927196702, 0.0481407764864968, 0.0537506715078538, 
0.291291955043046, 0.0512592612275567, -0.270848502252605, -0.180080615330518, 
-0.193524726701002, -0.265197691513802, -0.00543965340157225, 
0.0507719292742395, 0.0512592612275567, -0.0311077656380111, 
-0.210164910991586, -0.0579810236434912, 0.0481407764864968, 
-0.201352649568953, -0.270848502252605, 0.258475929097868, -0.270848502252605, 
0.0629308207185855, 0.0481407764864968, 0.0512592612275567, -0.0332663029999672, 
-0.270848502252605, 0.291291955043046, -0.265197691513802, 0.291291955043046, 
0.291291955043046, 0.212376285836879, 0.0512592612275567, 0.0512592612275567, 
-0.270848502252605, 0.0512592612275567, 0.0629308207185855, -0.191891917328992, 
0.0629308207185855, -0.270848502252605, 0.285682060021689, 0.291291955043046, 
0.0537506715078538, 0.0537506715078538, -0.191891917328992, -0.0184762587678785, 
0.0209346455626758, 0.0512592612275567, 0.300472104253777, 0.0512592612275567, 
-0.270807586535159, 0.0184432352823787, 0.285682060021689, 0.285682060021689, 
0.291291955043046, 0.291291955043046, 0.0537506715078538, 0.0537506715078538, 
-0.303623612480337, 0.0629308207185855, -0.265197691513802, -0.0251649976983132, 
0.0512592612275567, 0.0537506715078538, -0.0276564079786102, 
-0.29801371745898, -0.0579810236434912, 0.291291955043046, -0.210164910991586
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
"17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", 
"28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", 
"39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", 
"61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", 
"72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", 
"83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", 
"94", "95", "96", "97", "98", "99", "100", "101", "102", "103", 
"104", "sit1", "sit2", "sit3", "sit4", "sit5", "sit6", "sit7", 
"sit8", "sit9", "sit10", "sit11", "sit12", "sit13", "sit14", 
"sit15", "sit16", "sit17", "sit18", "sit19", "sit20", "sit21", 
"sit22", "sit23", "sit24", "sit25", "sit26", "sit27", "sit28", 
"sit29", "sit30", "sit31", "sit32", "sit33", "sit34", "sit35", 
"sit36", "sit37", "sit38", "sit39", "sit40", "sit41", "sit42", 
"sit43", "sit44", "sit45", "sit46", "sit47", "sit48", "sit49", 
"sit50", "sit51", "sit52", "sit53", "sit54", "sit55", "sit56", 
"sit57", "sit58", "sit59", "sit60", "sit61", "sit62", "sit63", 
"sit64", "sit65", "sit66", "sit67", "sit68", "sit69", "sit70", 
"sit71", "sit72", "sit73", "sit74", "sit75", "sit76", "sit77", 
"sit78", "sit79", "sit80", "sit81", "sit82", "sit83", "sit84", 
"sit85", "sit86", "sit87", "sit88", "sit89", "sit90", "sit91", 
"sit92", "sit93", "sit94", "sit95", "sit96", "sit97", "sit98", 
"sit99", "sit100", "sit101", "sit102", "sit103"))

I am trying to label just "Import:" and "Innovation need:" as bold so that they show up in the label in bold for each label inserted into geom_label_repel(). I tried to do this with HTML code:

df.imm3$label <- gsub("Import:", "<b>Import:</b> ", rownames(df.imm3))
df.inn3$label <- gsub("Innovation need:", "<b>Innovation need:</b> ", rownames(df.inn3))

# Generate the plot
pc.plot <- ggplot(df.rda, aes(x = PC1, y = PC2)) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  geom_vline(xintercept = 0, linetype = "dotted") +
  coord_fixed() +
  theme_bw(base_size = 15) +
  geom_polygon(aes(group = group, color = group, fill = group),
               data = hull, alpha = 0.2) +
  scale_fill_manual(values = c("dodgerblue4", "#56B4E9", "orange", "#D94801",
                               "#C994C7", "#DF65B0", "#67001F")) +
  geom_polygon(aes(group = group, fill = group, color = group),
               data = hull, alpha = 0.4) +
  geom_point(alpha = 0.3, color = "grey20") +
  scale_color_manual(values = c("dodgerblue4", "#56B4E9", "orange", "#D94801",
                                "#C994C7", "#DF65B0", "#67001F")) +
  geom_segment(data = df.imm3, aes(x = 0, xend = PC1, y = 0, yend = PC2), 
               color = "#08306B", size = 0.75, arrow = arrow(length = unit(0.01, "npc"))) +
  geom_segment(data = df.inn3, aes(x = 0, xend = PC1, y = 0, yend = PC2), 
               color = "#3F007D", size = 0.75, arrow = arrow(length = unit(0.01, "npc"))) +
  geom_label_repel(data = df.imm3, aes(x = PC1, y = PC2, label = label),
                   color = "#08306B", size = 4, alpha = 0.7, parse = T) +
  geom_label_repel(data = df.inn3, aes(x = PC1, y = PC2, label = label),
                   color = "#3F007D", size = 4, alpha = 0.7, parse = T)

When I run the code I get the error:

Error in `geom_label_repel()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 8th layer.
Caused by error in `parse()`:
! <text>:1:1: unexpected '<'
1: <
    ^

I can get it to run if I take out the parse=T argument in geom_label_repel() but then my plots look like this: enter image description here

How can I make a label in ggplot with geom_label_repel() so that only the sections I want in the label are in bold? I'd also like to keep the text left aligned too instead of center aligned.


Solution

  • parse=TRUE only allows to parse math expressions which use ?plotmath notation.

    Formatting using HTML, CSS or markdown is only supported when using so-called rich text as provided by the gridtext package. Unfortunately, as to the best of my knowledge ggrepel does not support rich text as of the moment.

    Hence, if you want to use ggrepel and format only some of the words you have to use ?plotmath which requires slightly more effort to make your labels proper plotmath expressions. Additionally, note that plotmath does not allow for line-breaks but we can fake one using atop.

    library(ggplot2)
    library(ggrepel)
    
    df.imm3$label <- gsub("Import:", "bold('Import:')", rownames(df.imm3))
    df.inn3$label <- gsub("Innovation need:", "bold('Innovation need:')", rownames(df.inn3))
    
    df.imm3$label <- gsub(" ", "~", df.imm3$label)
    df.inn3$label <- gsub(" ", "~", df.inn3$label)
    df.imm3$label <- gsub("^(.*?)\\n(.*)$", "atop(\\1, \\2)", df.imm3$label)
    df.inn3$label <- gsub("(.*?)\\n(.*)$", "atop(\\1, \\2)", df.inn3$label)
    df.inn3$label <- gsub("\\n", "~", df.inn3$label)
    
    
    ggplot(df.pca, aes(x = PC1, y = PC2)) +
      geom_hline(yintercept = 0, linetype = "dotted") +
      geom_vline(xintercept = 0, linetype = "dotted") +
      coord_fixed() +
      theme_bw(base_size = 15) +
      # geom_polygon(aes(group = group, color = group, fill = group),
      #   data = hull, alpha = 0.2
      # ) +
      scale_fill_manual(values = c(
        "dodgerblue4", "#56B4E9", "orange", "#D94801",
        "#C994C7", "#DF65B0", "#67001F"
      ), aesthetics = c("fill", "color")) +
      # geom_polygon(aes(group = group, fill = group, color = group),
      #   data = hull, alpha = 0.4
      # ) +
      geom_point(alpha = 0.3, color = "grey20") +
      geom_segment(
        data = df.imm3, aes(x = 0, xend = PC1, y = 0, yend = PC2),
        color = "#08306B", size = 0.75, arrow = arrow(length = unit(0.01, "npc"))
      ) +
      geom_segment(
        data = df.inn3, aes(x = 0, xend = PC1, y = 0, yend = PC2),
        color = "#3F007D", size = 0.75, arrow = arrow(length = unit(0.01, "npc"))
      ) +
      geom_label_repel(
        data = df.imm3, aes(x = PC1, y = PC2, label = label),
        color = "#08306B", size = 4, alpha = 0.7, parse = TRUE
      ) +
      geom_label_repel(
        data = df.inn3, aes(x = PC1, y = PC2, label = label),
        color = "#3F007D", size = 4, alpha = 0.7, parse = TRUE
      )
    

    enter image description here