ggplot2-具有特殊字符/符号和实际值的构面标签? [英] ggplot2 - facet labels with special characters/symbols and actual values?

查看:109
本文介绍了ggplot2-具有特殊字符/符号和实际值的构面标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制带有facet_gridlabeller = label_both的构面的R ggplot2图,如下所示:

I am plotting an R ggplot2 plot with facets made with facet_grid and labeller = label_both like this:

如您所见,我能够在图例中使用希腊字符.

As you can see, I was able to use greek characters in the legend.

我还想在构面标签中使用希腊符号,例如,psi: 0.1中的"psi"将是实际的希腊psi符号,同时保留:0.1"部分.我尝试了labeller = label_parsed,但没有给我:0.1".

I also want to use greek symbols in the facet labels so that, for example, the "psi" in psi: 0.1 would be the actual greek psi symbol while keeping the ": 0.1" part. I tried labeller = label_parsed but that doesn't give me the ": 0.1".

除了labeller = label_bothlabeller = label_parsed之外,还有其他方法可以实现我想要的吗?理想情况下,我希望找到一种适用于数学表达式的通用解决方案.谢谢.

Is there a way other than labeller = label_both or labeller = label_parsed to achieve what I want? Ideally I hope to find a generalised solution that applies to math expressions as well. Thank you.

P.S.这是我的ggplot通话:

P.S. Here is my ggplot call:

ggplot(data = my_data) +
    geom_point(mapping = aes(x = frame_size, y = psi_hat_mean, colour = "psi_hat")) +
    geom_point(mapping = aes(x = frame_size, y = p_hat_mean, colour = "p_hat")) +
    facet_grid(facets = psi ~ p, labeller = label_both) +
    scale_x_continuous("Frame size") +
    scale_y_continuous("Parameter estimates") +
    scale_colour_manual("Estimated parameter",
                        values = c("psi_hat" = "grey", "p_hat" = "black"),
                        breaks = c("p_hat", "psi_hat"),
                        labels = c(expression(paste(hat(p))),
                                   expression(paste(hat(psi))))) +
    theme(legend.title=element_blank())

这是数据集的dput()输出:

structure(list(frame_size = c(1L, 2L, 3L, 4L, 5L, 6L, 10L, 12L, 
15L, 20L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 10L, 12L, 15L, 20L, 30L, 
1L, 2L, 3L, 4L, 5L,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.05, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.1, 
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 
0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2), psi_hat_mean = c(0.134356048382177, 
0.134486773572389, 0.13873620166692, 0.138805987584267, 0.13870935995785, 
0.138650800195541, 0.143796766183063, 0.13916560213237, 0.143315740756538, 
0.169921216208113, 0.197659424623347, 0.100613069549282, 0.100635588260763, 
0.100597940900849, 0.100694725369573, 0.100675516255325, 0.100745841907948, 
0.100815988120923, 0.100979695039558, 0.101142533641973, 0.101443977996904, 
0.115373528295039, 0.0999755153064313, 0.0999979752698177, 0.0998962157963402, 
0.100005898203748, 0.100003047766876, 0.100019378162055, 0.100011289299875, 
0.100012663699438, 0.100017130290604, 0.100019688944185, 0.100002352877339, 
0.250280871706711, 0.20814054675288, 0.207769046277594, 0.208090970914499, 
0.208532916287244, 0.208859991544016, 0.209252480710842, 0.210811715962647, 
0.211537486757659, 0.212108292997122, 0.221164186075537, 0.200847254811565, 
0.200882290085097, 0.200895194559799, 0.200963134246294, 0.200935377663199, 
0.200986349050379, 0.201026021866445, 0.20133815021031, 0.201285819726138, 
0.201771848718818, 0.202132030705017, 0.200000863248174, 0.200013219984772, 
0.20001223270007, 0.199994520753463, 0.200008287620153, 0.200004330046502, 
0.199998072041364, 0.20000498181904, 0.199884681785292, 0.199925907752792, 
0.200030218291904, 0.46909003266665, 0.404543079367836, 0.405542037009819, 
0.405473359663344, 0.406450221494248, 0.407016892401095, 0.407804741068077, 
0.408998802872545, 0.409059879710074, 0.412281980391505, 0.417686374689605, 
0.40047111166792, 0.400465155003593, 0.4004738934807, 0.400494312554866, 
0.400470923112942, 0.400528527625076, 0.400573788345039, 0.400647311390814, 
0.400818156817758, 0.400819981298249, 0.401099396871977, 0.399988878019095, 
0.400032636565743, 0.400001592936094, 0.400001283207262, 0.40000165174745, 
0.399995790514786, 0.400004582819455, 0.400021723478689, 0.400009633788358, 
0.400003950520113, 0.400026826367252), psi_hat_SD = c(0.153587439714877, 
0.153071750905185, 0.16430223183556, 0.164551851214726, 0.164650834475015, 
0.164191178124105, 0.174995748335211, 0.163948969973203, 0.174791156358891, 
0.227232415724057, 0.272815709195867, 0.00416873072294715, 0.00418449905149223, 
0.00408281532783013, 0.00413066011578574, 0.00411030244674003, 
0.00415794082443098, 0.00428705188869137, 0.00447385303686605, 
0.00512309080419838, 0.00538878394819733, 0.109059513881393, 
8.10448978287042e-05, 3.25412047710771e-05, 0.000229019038524637, 
1.32554791740984e-05, 3.28644816689232e-05, 2.89433268844505e-05, 
2.95023799151163e-05, 4.38564452461257e-05, 6.39815108451877e-05, 
9.1881375010522e-05, 4.78209458970029e-13, 0.183002870467444, 
0.0293818799338775, 0.0288402656743286, 0.0288165177620824, 0.0297634852739387, 
0.0299527800584211, 0.0347846993392747, 0.0372990730196757, 0.035669047956326, 
0.0368368101376486, 0.0705751689361306, 0.00203121177367882, 
0.00192575329570197, 0.00184269195613544, 0.00240720598274821, 
0.00214770293122925, 0.00226807525752776, 0.00188169304328542, 
0.00340586369727475, 0.00345333835319253, 0.00394664917443553, 
0.00872196151289955, 8.6094454437387e-06, 1.71106169362863e-05, 
3.23287049318278e-05, 3.53049297358841e-05, 2.1492246131839e-05, 
6.2288439810166e-06, 1.44667033948848e-05, 1.2515352244187e-05, 
0.00012029349894011, 0.000171331269685535, 0.000293960412588456, 
0.195750328230943, 0.0432980747152544, 0.0448046192056449, 0.0440846647061932, 
0.0485042955171366, 0.0482979217319834, 0.0522379989209262, 0.0579890416481406, 
0.0531009498688177, 0.0630117285310315, 0.0713821778815143, 0.00623289291854068, 
0.006221899243366, 0.00624538209117939, 0.00628328531688065, 
0.00627909851780257, 0.00634871536458593, 0.00637069626931915, 
0.00644027718251307, 0.00670664538203358, 0.00674945522402437, 
0.00672507992540975, 1.28897443961884e-05, 4.4027910050059e-05, 
2.80088266440853e-06, 4.4937052942413e-06, 3.89369731696281e-06, 
1.90379259756568e-05, 1.15262840907433e-05, 3.12553218039823e-05, 
2.25050991263921e-05, 5.46276800711688e-05, 0.00028230060200018
), p_hat_mean = c(0.0493805572043857, 0.0968923979425734, 0.141501343959094, 
0.184624723464391, 0.225280979633726, 0.262492758502991, 0.39490851538299, 
0.45154142965489, 0.525853334795089, 0.622498582290085, 0.744750596464598, 
0.0951102054450773, 0.181157836797368, 0.258547631613774, 0.329852164417611, 
0.392734430296655, 0.452845308468353, 0.634656661079906, 0.700411111167911, 
0.775401595177942, 0.852473557888133, 0.927365458017404, 0.201075086977168, 
0.361245627511475, 0.490362348231505, 0.593823984188126, 0.675404456152514, 
0.741475393843094, 0.896180595647868, 0.930911156580922, 0.969228130789834, 
0.993980061494683, 0.999933401786537, 0.0476141340798173, 0.0949703678299418, 
0.139111305167333, 0.179581265249894, 0.218955133012378, 0.255643703659721, 
0.390317309763656, 0.442577946011462, 0.517324051736444, 0.616089655559368, 
0.742460394002412, 0.0991474592154088, 0.188345493237174, 0.268690852765111, 
0.340630181377103, 0.404700993857244, 0.466171060122403, 0.646753481555216, 
0.710048127892561, 0.790236156784217, 0.86402129317977, 0.953873990924048, 
0.202356758942191, 0.363873473395577, 0.493936650336295, 0.597901898041952, 
0.680296139765208, 0.747366703193019, 0.903322005155783, 0.939981567846486, 
0.966801836293303, 0.988586306636356, 0.999243451951223, 0.0463364925613695, 
0.096186827643031, 0.13997233747503, 0.181749771931483, 0.222523649736396, 
0.259499892174649, 0.394045900835822, 0.45083186052406, 0.527845241576527, 
0.625777508556727, 0.765427947503516, 0.0993534376856661, 0.189237207621871, 
0.269758186956382, 0.342693314139248, 0.409295462208863, 0.467030278808092, 
0.651622635973668, 0.718795315510802, 0.793143513356861, 0.879655463405415, 
0.957032974716723, 0.201367021664948, 0.362466628523926, 0.490623284961033, 
0.595997765341907, 0.676872347808209, 0.73980714214723, 0.892075883852205, 
0.930114063779856, 0.963579844299096, 0.985988067825672, 0.99830794696651
), p_hat_SD = c(0.020671073048708, 0.0405971695814201, 0.0595100648300367, 
0.077240162880486, 0.0923487236898059, 0.106526196932577, 0.159729823310882, 
0.176228877737896, 0.200751656004054, 0.254670828390244, 0.287381559859864, 
0.0253741193781226, 0.0471638768455461, 0.0655493558111445, 0.0839136353773245, 
0.100142912148987, 0.110911181108964, 0.140636755877925, 0.15125797466698, 
0.1555013613506, 0.149459455618193, 0.166850371924508, 0.0344233871624597, 
0.0575241859626709, 0.0765279598077096, 0.088251660637436, 0.0907468071979005, 
0.09555580032782, 0.0910651633810324, 0.0787075972576783, 0.0610760339901177, 
0.0314642360500455, 1.07277201318732e-11, 0.0174068050890887, 
0.0296542146793441, 0.0420243108930666, 0.0538557925028335, 0.0650751964538859, 
0.0748944574050911, 0.107149795097664, 0.126045003227699, 0.145148214248163, 
0.161268452161263, 0.192544810714351, 0.0189081918124477, 0.0358817044204188, 
0.0483995702453099, 0.0615944579033814, 0.0706741412337433, 0.0806577966830984, 
0.101766144853803, 0.111253971769697, 0.108010267370669, 0.109406145445318, 
0.0844663953398124, 0.0237717398485985, 0.0415592337691319, 0.0508739981443487, 
0.055065018088554, 0.0607402227151579, 0.0644644110987824, 0.0599412871159953, 
0.0537879467634287, 0.0473614192478991, 0.0335711724058262, 0.0100985164006129, 
0.0148757945028784, 0.0223727872205404, 0.0328124061087913, 0.0416862513830743, 
0.051522313637991, 0.0599641576237172, 0.0870524394338348, 0.100814653541668, 
0.115655991730271, 0.131950868996117, 0.159736280003032, 0.0152671641785822, 
0.0282221543538266, 0.0388769807365557, 0.0478383382638392, 0.0552111224929799, 
0.0611073915392696, 0.0758076715431021, 0.0818244295408088, 0.0820682293454554, 
0.0729034999789475, 0.0524286593814453, 0.0175508745752872, 0.0293973130349527, 
0.0405068273531901, 0.0454857976255542, 0.0469496795934104, 0.0509564122496874, 
0.0469363426205197, 0.0448122577291855, 0.0334480891840883, 0.0234468477023789, 
0.0104300159826976)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -99L))

推荐答案

您可以为此使用label_bquote:

ggplot(data = my_data) +
  geom_point(mapping = aes(x = frame_size, y = psi_hat_mean, colour = "psi_hat")) +
  geom_point(mapping = aes(x = frame_size, y = p_hat_mean, colour = "p_hat")) +
  facet_grid(facets = psi ~ p, 
             labeller = label_bquote(cols = p: .(p), rows = psi: .(psi))) +
  scale_x_continuous("Frame size") +
  scale_y_continuous("Parameter estimates") +
  scale_colour_manual("Estimated parameter",
                      values = c("psi_hat" = "grey", "p_hat" = "black"),
                      breaks = c("p_hat", "psi_hat"),
                      labels = c(expression(hat(p)), expression(hat(psi)))) +
  theme(legend.title=element_blank())

有关更多信息,另请参见 https://ggplot2.tidyverse.org/reference/label_bquote. html

For more information see also https://ggplot2.tidyverse.org/reference/label_bquote.html

顺便说一句:您不需要在expression调用内的paste此处输入图例标签.

By the way: You don't need the paste inside the expression-call here for your legend-labels.

这篇关于ggplot2-具有特殊字符/符号和实际值的构面标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆