如何在ggplot中编辑当前图例标签

时间:2019-12-13 15:23:10

标签: r ggplot2

以下脚本生成六边形图:

library(ggplot2)
d <- ggplot(df, aes(Volume, Price)) + geom_hex(bins = 30) + theme_bw() +
    geom_smooth(method="lm") 
d + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"))

数据:

structure(list(Volume = c(0.5, 0.191304347826087, 0.887265917602996, 
0.347892720306513, 0.0119047619047619, 0.42, 0.743589743589744, 
0, 0.400438596491228, 0.5, 0.653645833333333, 0, 0.755238095238095, 
0.477690288713911, 0.743971631205674, 0.0134228187919463, 0, 
0.71025641025641, 0.698648648648649, 0.760776439089692, 0, 0.737435897435898, 
0.534401709401709, 0.322630385487528, 0.442105263157895, 0.518518518518518, 
0.117647058823529, 0.981818181818182, 0.0769230769230769, 0.625, 
0.365853658536585, 0.128333333333333, 0, 0.0425531914893617, 
0.0954471544715447, 0.691836734693877, 0.749872773536896, 0.675124378109453, 
0.446258503401361, 0.680787037037037, 0.828462709284627, 0.0690058479532163, 
0.666666666666667, 0.903225806451613, 0.0666666666666667, 0, 
0.0270562770562771, 0, 0.295928753180662, 0.767857142857143, 
0.183783783783784, 0.432835820895522, 0.792857142857143, 0, 0.624074074074074, 
0.102424242424242, 0.419444444444445, 0.637142857142857, 0.937878787878788, 
0.05, 0.296296296296296, 0.695652173913043, 0.612962962962963, 
0.14280701754386, 0.315458937198068, 0.198148148148148, 0.516666666666667, 
0.538461538461538, 0.607142857142857, 0.882051282051282, 0.657383966244726, 
0.434379671150972, 0.709015421115065, 0.75625, 0.218181818181818, 
0.869565217391304, 0.894736842105263, 0.318562091503268, 0.471929824561404, 
0.75420054200542, 0.438501291989664, 0.715942028985507, 0.3325, 
0.869565217391304, 0.0293040293040293, 0.0354908306364617, 0.353939393939394, 
0.642857142857143, 0.804624277456647, 0.92156862745098, 0.0229166666666667, 
0.666666666666667, 0.296747967479674, 0.641025641025641, 0.230769230769231, 
0.511764705882353, 0.677494692144374, 0.531182795698925, 0.707368421052632, 
0.863333333333333, 0.643421052631579, 0.895652173913044, 0.591358024691358, 
0.710144927536232, 0.0344827586206897, 0.113333333333333, 0.713710691823899, 
0.752564102564103, 0.499616858237548, 0.74421768707483, 0.241397849462366, 
0.774193548387097, 0.323786209710322, 0.764705882352941, 0.692539109506618, 
0.666666666666667, 0.306666666666667, 0.901880341880342, 0.604040404040404, 
0, 0.183333333333333, 0.48780487804878, 0.19047619047619, 0.607142857142857, 
0, 0.0208333333333333, 0.766862745098039, 0.533630952380953, 
0.135233918128655, 0, 0.683681592039801, 0.799047619047619, 0.122445561139028, 
0.923456790123457, 0.709459459459459, 0.797916666666667, 0.793888888888889, 
0.00851063829787234, 0.571428571428571, 0.745210727969349, 0.474193548387097, 
0.432432432432432, 0.380459770114942, 0.85625, 0.684210526315789, 
0.658487654320988, 0.166666666666667, 0.868, 0.0912772585669781, 
0, 0.0623134328358209, 0, 0.625, 0, 0.266666666666667, 0.52, 
0.79920634920635, 0.279012345679012, 0.363525091799266, 0.711954022988506, 
0.933333333333333, 0.591333333333333, 0.208333333333333, 0.192307692307692, 
0.225925925925926, 0, 0.157142857142857, 0.812159329140461, 0.427472527472528, 
0.822710622710623, 0.842937853107345, 0.79338061465721, 0.64, 
0.5, 0.611515151515151, 0.301428571428571, 0.556666666666667, 
0.538095238095238, 0.50663082437276, 0.72816091954023, 0.541115434500648, 
0.38502824858757, 0.875187969924812, 0.40625, 0.849593495934959, 
0.9, 0.103070175438597, 0.156540084388186, 0.666666666666667, 
0.461904761904762, 0, 0.824595469255664, 0, 0.374390243902439, 
0.381194511702986, 0.713333333333333, 0.526315789473684, 0.166666666666667, 
0, 0.668965517241379), Price = c(0.792135141054365, 0.937888752591144, 
0.855778931380096, 0.747334497789751, 0.883152256576581, 0.789259638590895, 
0.870096127149947, 0.996337891880306, 0.814879583933719, 0.901490595561084, 
0.831880524327038, 0.644017135881595, 0.871950913227784, 0.882912189801298, 
0.873566212440884, 0.996864572337506, 0.987043233834958, 0.871766101237158, 
0.885205380235904, 0.939355667437049, 0.393303123065151, 0.946302359969951, 
0.902244197575864, 0.834500136366311, 0.905276184897953, 0.822698294316994, 
0.943873960757459, 0.864079525710874, 0.848401195479997, 0.878145437996109, 
0.878189027935294, 0.861698869472603, 0.983667238077781, 0.959660634076345, 
0.651789775666478, 0.865101309691293, 0.795357373359823, 0.932410593456246, 
0.853495027434347, 0.887308093062124, 0.884542642634386, 0.917836552037272, 
0.873845815527948, 0.807166757286395, 0.951794394193276, 0.997458634985182, 
0.728851736717989, 0.865664458393566, 0.86324348854792, 0.736136413458874, 
0.909690837980405, 0.782440348333119, 0.86223595573823, 0.915711295232019, 
0.796813460570667, 0.774814902651245, 0.846133250446692, 0.888023290720753, 
0.948778843831823, 0.819751113761411, 0.841660156074548, 0.902128364606873, 
0.856094497305008, 0.941885398319796, 0.849642781758329, 0.750425285112076, 
0.868498070397065, 0.846093489021016, 0.915929754831932, 0.891889009416148, 
0.877141935475535, 0.879518996298533, 0.838968314295522, 0.863049339141498, 
0.848111974094123, 0.855565630338174, 0.894303366672644, 0.853253724513418, 
0.69076686337143, 0.88792353694477, 0.886723642629289, 0.893688354716787, 
0.870247111805308, 0.854693511570169, 0.632573532833423, 0.679619775362861, 
0.876409107849085, 0.862108357710104, 0.899204727254876, 0.809948270674638, 
0.804185291725837, 0.87036059371433, 0.796654959892513, 0.852532618779132, 
0.835191072475417, 0.787487767125462, 0.812492815203243, 0.81912255349386, 
0.900375440947856, 0.854316604016639, 0.726946109393128, 0.753516836131138, 
0.857253018373696, 0.940702096565525, 0.714591141035648, 0.821148156989907, 
0.928164484701284, 0.876639804485154, 0.893141793718152, 0.86469273603586, 
0.529871229804895, 0.86438237983567, 0.806451903919243, 0.836181475132432, 
0.905549930610653, 0.870695393272322, 0.711314236933727, 0.924732704091675, 
0.791476850860223, 0.848267466391695, 0.787912019679365, 0.779707186810304, 
0.879282230577452, 0.912976866848287, 0.474185130108644, 0.657354363193108, 
0.854357160780713, 0.865553058451435, 0.941048042796854, 0.935734410351538, 
0.813233115666515, 0.881776963437419, 0.991117852002588, 0.875415843792012, 
0.804506879395258, 0.88677995787238, 0.869789455245567, 0.882387872449206, 
0.920600218781275, 0.802933421708709, 0.829035443153687, 0.873839469430045, 
0.886555949395015, 0.828157301241112, 0.832288690195035, 0.879283726703994, 
0.837650591207202, 0.881666566098315, 0.941566987353403, 1, 0.931713647000955, 
0.794581863731762, 0.861022706614273, 0.799291217175045, 0.11038987336682, 
0.727463768584832, 0.908459831173911, 0.766258873319923, 0.781911859095871, 
0.805737327876253, 0.918199301649904, 0.856071857504223, 0.777145847648749, 
0.741629568641077, 0.726439089973248, 1, 0.845716848707734, 0.898277797514682, 
0.944127849493313, 0.985620018209899, 0.77254196112121, 0.851235022502933, 
0.925732755540251, 0.909733251974376, 0.864757897587371, 0.849510421572219, 
0.589569204238877, 0.821042259307134, 0.973357829777803, 0.933020576836232, 
0.79441693983753, 0.834319648636115, 0.880933734139818, 0.893479230672201, 
0.912196800595273, 0.724455744188118, 0.873816567090615, 0.756904281157693, 
0.908707557672312, 0.840228254605193, 0.801935978324457, 0.819047063180147, 
0.754326659670962, 0.751013460355367, 0.762689335286665, 0.937238883600678, 
0.909919126845949, 0.762053957261507, 1, 0.97477402827307)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -200L), .Names = c("Volume", 
"Price"))

这将生成一个图,例如:

enter image description here

如何更改计数的标签以显示为100(例如4应该是400)?我宁愿不手动执行此操作。

1 个答案:

答案 0 :(得分:2)

您可以使用geom_hexstat(count)进行期望的测量,此外,您还可以修改其结果(例如乘以一个系数):

library(ggplot2)
ggplot(df, aes(Volume, Price)) +
  geom_hex(aes(fill = stat(count) * 1e2)) +
  geom_smooth(method = "lm") +
  labs(
    fill = "Measurement"
  )

enter image description here