更改ggplot2图例中单个标签的字体(变为粗体)

Posted

技术标签:

【中文标题】更改ggplot2图例中单个标签的字体(变为粗体)【英文标题】:Change font face (to bold) of a single label in ggplot2 legend 【发布时间】:2021-03-19 01:00:09 【问题描述】:

我在 R 4.0.2 中有一个包含两个连续变量的数据集,分为两组(变量曲线:“Exp.”或“Molecular\ndynamics”)。我用 geom_smooth 图层(ggplot2 3.3.2)绘制它们并按组曲线为它们着色,并且我还设置了该组为它们赋予不同的线型,但然后我使用 scale_linetype_manual 对其进行修改以分别为它们提供虚线和实线线型.

我想做“Exp”。图例的标签粗体。我尝试了很多方法(即,其他人的问题的许多答案)来尝试手动重命名图例标签(在 scale_linetype_manual 中添加一个 labels=;在 guides()... 中尝试了一个 override.aes),但最好的我设法做的是获得具有不同标签的重复图例,既不保留颜色也不保留线型,也不显示“Exp”。粗体字。 (我认为问题在于我已经使用 scale_xxx 手动调整线型,所以我测试过的关于如何编辑标签的其余答案与我的代码不兼容,我不知道如何弄清楚问题)。

当前代码:

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

可重现的例子:

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】:

具有适当功能的示例和代码可能很有用。你可以使用ggtext包:

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

输出:

接下来是您的代码,使用共享的数据:

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

输出:

【讨论】:

嗨,非常感谢!似乎在我的许多尝试中(ggtext 也包含在其中,哈哈)我缺少的只是在 scale_color 中添加标签 =。干杯 @FranNerínFonz 总是乐于助人!

以上是关于更改ggplot2图例中单个标签的字体(变为粗体)的主要内容,如果未能解决你的问题,请参考以下文章

R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置y轴的标签文本使用粗体字体)

R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,使x轴和Y轴的标签文本都使用粗体字体)注意是轴标签而非轴标题

无法在 R 中使用 ggplot2 更改图例标题和标签

更改 matplotlib 图例中各个标签的字体大小

如何更改 direct.label 中的字体大小?

增加图例字体大小ggplot2