Change font face (to bold) of a single label in ggplot2 legend

12.2k views Asked by At

I have a dataset in R 4.0.2 of two continuous variables divided into two groups (variable Curve: "Exp." or "Molecular\ndynamics"). I plot them with a geom_smooth layer (ggplot2 3.3.2) and color them by group Curve, and I also set that they are given different linetypes by this group, but I then modify this with scale_linetype_manual to give them dashed and solid linetypes, respectively.

I want to make the "Exp." label of the legend bold. I have tried many ways (i.e., many answers to other people's problems) to try to manually rename the legend labels (add a labels= inside the scale_linetype_manual; tried an override.aes inside guides()...), but the best I managed to do has been to get a duplicate legend with different labels that doesn't preserve the color nor the linetype and that also doesn't show "Exp." in bold. (I think the problem is that I already use a scale_xxx to manually adjust the linetypes, so the rest of the answers I've tested on how to edit the labels are incompatible with my code and I don't have the knowledge to figure out the problem).

Present code:

ggplot() +
  geom_smooth(data = data, aes(x=`T(K)`, y=dG, color=Curve, linetype=Curve), size=2, se=FALSE) +
  theme_classic() + scale_color_brewer() +
  ggtitle('Lysozyme') + xlab('T (K)') + ylab('∆G (kcal/mol)') +
  theme(panel.border = element_rect(colour = "black", fill=NA)) +
  scale_linetype_manual(values=c("dashed", "solid")) +
  theme(text = element_text(family="", size=20),
        plot.title = element_text(hjust = 0.5, face="bold", size=20),
        axis.title.y = element_text(size=20, margin=margin(r=25)),
        axis.title.x = element_text(size=20, margin=margin(t=25)),
        axis.text = element_text(size=20, color="black"),
        legend.text = element_text(margin = margin(t = 10, b = 10, unit = "pt")),
        legend.key.size = unit(2,"line"))

Present graphic

Reproducible Example:

data <- structure(list(Curve = c("Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", 
"Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "Exp.", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", "MolDyn", 
"MolDyn", "MolDyn"), x = c(223, 225.240506329114, 227.481012658228, 
229.721518987342, 231.962025316456, 234.20253164557, 236.443037974684, 
238.683544303797, 240.924050632911, 243.164556962025, 245.405063291139, 
247.645569620253, 249.886075949367, 252.126582278481, 254.367088607595, 
256.607594936709, 258.848101265823, 261.088607594937, 263.329113924051, 
265.569620253165, 267.810126582278, 270.050632911392, 272.291139240506, 
274.53164556962, 276.772151898734, 279.012658227848, 281.253164556962, 
283.493670886076, 285.73417721519, 287.974683544304, 290.215189873418, 
292.455696202532, 294.696202531646, 296.936708860759, 299.177215189873, 
301.417721518987, 303.658227848101, 305.898734177215, 308.139240506329, 
310.379746835443, 312.620253164557, 314.860759493671, 317.101265822785, 
319.341772151899, 321.582278481013, 323.822784810127, 326.063291139241, 
328.303797468354, 330.544303797468, 332.784810126582, 335.025316455696, 
337.26582278481, 339.506329113924, 341.746835443038, 343.987341772152, 
346.227848101266, 348.46835443038, 350.708860759494, 352.949367088608, 
355.189873417722, 357.430379746835, 359.670886075949, 361.911392405063, 
364.151898734177, 366.392405063291, 368.632911392405, 370.873417721519, 
373.113924050633, 375.354430379747, 377.594936708861, 379.835443037975, 
382.075949367089, 384.316455696203, 386.556962025316, 388.79746835443, 
391.037974683544, 393.278481012658, 395.518987341772, 397.759493670886, 
400, 223, 225.240506329114, 227.481012658228, 229.721518987342, 
231.962025316456, 234.20253164557, 236.443037974684, 238.683544303797, 
240.924050632911, 243.164556962025, 245.405063291139, 247.645569620253, 
249.886075949367, 252.126582278481, 254.367088607595, 256.607594936709, 
258.848101265823, 261.088607594937, 263.329113924051, 265.569620253165, 
267.810126582278, 270.050632911392, 272.291139240506, 274.53164556962, 
276.772151898734, 279.012658227848, 281.253164556962, 283.493670886076, 
285.73417721519, 287.974683544304, 290.215189873418, 292.455696202532, 
294.696202531646, 296.936708860759, 299.177215189873, 301.417721518987, 
303.658227848101, 305.898734177215, 308.139240506329, 310.379746835443, 
312.620253164557, 314.860759493671, 317.101265822785, 319.341772151899, 
321.582278481013, 323.822784810127, 326.063291139241, 328.303797468354, 
330.544303797468, 332.784810126582, 335.025316455696, 337.26582278481, 
339.506329113924, 341.746835443038, 343.987341772152, 346.227848101266, 
348.46835443038, 350.708860759494, 352.949367088608, 355.189873417722, 
357.430379746835, 359.670886075949, 361.911392405063, 364.151898734177, 
366.392405063291, 368.632911392405, 370.873417721519, 373.113924050633, 
375.354430379747, 377.594936708861, 379.835443037975, 382.075949367089, 
384.316455696203, 386.556962025316, 388.79746835443, 391.037974683544, 
393.278481012658, 395.518987341772, 397.759493670886, 400), y = c(-7.68350483903568, 
-6.61614472856118, -5.59380218853684, -4.6165652100245, -3.68452178408595, 
-2.79775990178302, -1.9563675541775, -1.16043273233119, -0.410043427305927, 
0.294712369836497, 0.953740857737224, 1.56679954524656, 2.13397131026955, 
2.65549487174862, 3.13160894862622, 3.56255225984475, 3.94856352434666, 
4.28988146107438, 4.58674478897032, 4.83939222697694, 5.04763466843154, 
5.20722881762394, 5.31798780177217, 5.38112464965697, 5.3978523900591, 
5.3693840517593, 5.29693266353832, 5.18171125417692, 5.02493285245582, 
4.8278104871558, 4.58969168534812, 4.30264171863972, 3.96737774163971, 
3.58597508338174, 3.1605090728994, 2.69305503922635, 2.18568831139618, 
1.64048421844252, 1.05951808939902, 0.444865253299259, -0.205561649971956, 
-0.901038243936931, -1.64050642119888, -2.42230331983255, -3.24476607791261, 
-4.10623183351382, -5.00503772471085, -5.93952088957842, -6.90801846619124, 
-7.90886759262399, -8.94145568102631, -10.011930959601, -11.1209288401495, 
-12.2676518823087, -13.4513026457153, -14.6710836900061, -15.9261975748179, 
-17.2158468597874, -18.5392341045515, -19.8955618687469, -21.2849566323259, 
-22.710219553137, -24.1714730787612, -25.668557173712, -27.201311802503, 
-28.7695769296477, -30.3731925196596, -32.0119985370522, -33.6858349463391, 
-35.3945417120337, -37.1380663945532, -38.9165771494131, -40.7301362239649, 
-42.5788018140465, -44.4626321154956, -46.3816853241496, -48.3360196358465, 
-50.3256932464238, -52.3507643517192, -54.4112911475705, 31.5512317785764, 
31.2374137098356, 30.8987942750184, 30.5353249975297, 30.1469574007745, 
29.7336430081576, 29.2953333430839, 28.8319799289583, 28.3435342891858, 
27.8299479471711, 27.2911692252734, 26.7270645287271, 26.1376795171544, 
25.5231457071126, 24.8835946151589, 24.2191577578505, 23.5299666517448, 
22.8161528133988, 22.0778477593699, 21.3151830062153, 20.5280543700902, 
19.7141241075486, 18.8732892654496, 18.0062181333199, 17.1135790006862, 
16.1960401570755, 15.2542698920144, 14.2889364950296, 13.300708255648, 
12.2902534633964, 11.2572126537412, 10.1972143756774, 9.11065372858087, 
7.99867406591518, 6.86241874114379, 5.70303110773022, 4.52165451913789, 
3.31943232883031, 2.09750789027099, 0.857024556923361, -0.403167652915984, 
-1.68817892472047, -2.99742631923693, -4.32999372200323, -5.68496501855707, 
-7.06142409443628, -8.45845483517863, -9.87514112632187, -11.3105668534038, 
-12.7638159019621, -14.2345507812044, -15.7261587120945, -17.2389897597413, 
-18.7726045932309, -20.3265638816491, -21.9004282940821, -23.4937584996157, 
-25.106115167336, -26.7370589663289, -28.3861505656805, -30.0534596465301, 
-31.7405294405658, -33.4474274073871, -35.1740653792266, -36.9203551883169, 
-38.6862086668903, -40.4715376471795, -42.2762539614168, -44.1002694418348, 
-45.9434959206659, -47.8059045075618, -49.687587843828, -51.5885802231712, 
-53.5089137072123, -55.448620357572, -57.4077322358711, -59.3862814037305, 
-61.3842999227711, -63.4018198546136, -65.438873260879)), class = "data.frame", row.names = c(NA, 
-160L))

1

There are 1 answers

4
Duck On BEST ANSWER

An example and your code with proper functions can be useful. You can use ggtext package:

library(ggtext)
library(ggplot2)
#Code
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+
  geom_point()+
  scale_color_discrete(labels=c('**setosa**','versicolor','virginica'))+
  theme(legend.text = element_markdown())

Output:

enter image description here

Your code would be next, using the data shared:

#Your code
ggplot() +
  geom_smooth(data = data, aes(x=x, y=y, color=Curve, linetype=Curve),
              size=2, se=FALSE) +
  theme_classic() +
  scale_color_brewer(labels=c("**Exp.**","Molecular dynamics")) +
  ggtitle('Lysozyme') + xlab('T (K)') + ylab('∆G (kcal/mol)') +
  theme(panel.border = element_rect(colour = "black", fill=NA)) +
  scale_linetype_manual(values=c("dashed", "solid"),
                        labels=c("**Exp.**","Molecular dynamics")) +
  theme(text = element_text(family="", size=20),
        plot.title = element_text(hjust = 0.5, face="bold", size=20),
        axis.title.y = element_text(size=20, margin=margin(r=25)),
        axis.title.x = element_text(size=20, margin=margin(t=25)),
        axis.text = element_text(size=20, color="black"),
        legend.text = element_markdown(margin = margin(t = 10,
                                                       b = 10, unit = "pt")),
        legend.key.size = unit(2,"line"))

Output:

enter image description here