没有随机条ggplot增加线宽

时间:2016-08-09 22:54:51

标签: r plot ggplot2 roc

有没有人知道是否可以以平滑的方式增加ggplot2中的线宽而不添加突出的随机线?这是我的原始线图,大小增加到5:

> ggplot(curve.df, aes(x=recall, y=precision, color=cutoff)) +
>   geom_line(size=1)

image 1 image 2

理想情况下,最终图像看起来类似于PRROC Package中的下图,但是我还有另外一个问题,那就是在那里绘制网格线并且底线与轴标记不对应。

这里我第一次打电话给

> grid()

然后调用

> abline(v=seq(0,1,.2), h=seq(0,1,.2))

enter image description here enter image description here

老实说,欣赏任何能够用更宽的线条绘制此曲线以查看清晰颜色和与轴标记对应的网格的方法。谢谢!

以下是截止.5到.7:

的数据样本
> dput(output)
structure(list(recall = c(0.0237648530331457, 0.024390243902439, 
0.0250156347717323, 0.0256410256410256, 0.0256410256410256, 0.0268918073796123, 
0.0275171982489056, 0.0281425891181989, 0.0293933708567855, 0.0300187617260788, 
0.0300187617260788, 0.0300187617260788, 0.0306441525953721, 0.0312695434646654, 
0.0312695434646654, 0.0312695434646654, 0.0318949343339587, 0.0318949343339587, 
0.0318949343339587, 0.032520325203252, 0.0331457160725453, 0.0331457160725453, 
0.0337711069418387, 0.034396497811132, 0.034396497811132, 0.0350218886804253, 
0.0356472795497186, 0.0356472795497186, 0.0362726704190119, 0.0362726704190119, 
0.0362726704190119, 0.0387742338961851, 0.0387742338961851, 0.0387742338961851, 
0.0393996247654784, 0.0400250156347717, 0.0400250156347717, 0.040650406504065, 
0.040650406504065, 0.040650406504065, 0.0412757973733583, 0.0419011882426517, 
0.042526579111945, 0.0431519699812383, 0.0431519699812383, 0.0437773608505316, 
0.0444027517198249, 0.0450281425891182, 0.0456535334584115, 0.0456535334584115, 
0.0462789243277048, 0.0469043151969981, 0.0469043151969981, 0.0469043151969981, 
0.0469043151969981, 0.0475297060662914, 0.0481550969355847, 0.0481550969355847, 
0.0494058786741714, 0.0494058786741714, 0.0494058786741714, 0.0494058786741714, 
0.0512820512820513, 0.0512820512820513, 0.0531582238899312, 0.0537836147592245, 
0.0537836147592245, 0.0537836147592245, 0.0550343964978111, 0.0556597873671044, 
0.0556597873671044, 0.0562851782363977, 0.0569105691056911, 0.0575359599749844, 
0.0581613508442777, 0.058786741713571, 0.0594121325828643, 0.0594121325828643, 
0.0600375234521576, 0.0606629143214509, 0.0612883051907442, 0.0625390869293308, 
0.0631644777986241, 0.0637898686679174, 0.0644152595372108, 0.0644152595372108, 
0.0644152595372108, 0.0650406504065041, 0.0650406504065041, 0.0656660412757974, 
0.0656660412757974, 0.0662914321450907, 0.066916823014384, 0.0687929956222639, 
0.0694183864915572, 0.0700437773608505, 0.0700437773608505, 0.0706691682301438, 
0.0712945590994371, 0.0712945590994371, 0.0712945590994371, 0.0712945590994371, 
0.0712945590994371, 0.0712945590994371, 0.0719199499687305, 0.0725453408380238, 
0.0725453408380238, 0.0731707317073171, 0.075046904315197, 0.075046904315197, 
0.0756722951844903, 0.0762976860537836, 0.0769230769230769, 0.0775484677923702, 
0.0775484677923702, 0.0787992495309568, 0.0794246404002502, 0.0794246404002502, 
0.0794246404002502, 0.0800500312695435, 0.0800500312695435, 0.0806754221388368, 
0.0813008130081301, 0.0813008130081301, 0.0819262038774234, 0.0825515947467167, 
0.08317698561601, 0.08317698561601, 0.0850531582238899, 0.0863039399624766, 
0.0863039399624766, 0.0869293308317699, 0.0881801125703565, 0.0888055034396498, 
0.0888055034396498, 0.0900562851782364, 0.0919324577861163, 0.0919324577861163, 
0.0925578486554096, 0.0931832395247029, 0.0931832395247029, 0.0931832395247029, 
0.0938086303939962, 0.0944340212632895, 0.0944340212632895, 0.0956848030018762, 
0.0956848030018762, 0.0963101938711695, 0.0963101938711695, 0.0963101938711695, 
0.0963101938711695, 0.0975609756097561, 0.0981863664790494, 0.0988117573483427, 
0.0988117573483427, 0.099437148217636, 0.099437148217636, 0.100062539086929, 
0.100687929956223, 0.101313320825516, 0.103189493433396, 0.103814884302689, 
0.103814884302689, 0.103814884302689, 0.105065666041276, 0.105691056910569, 
0.106316447779862, 0.106316447779862, 0.106941838649156, 0.107567229518449, 
0.107567229518449, 0.107567229518449, 0.108192620387742, 0.108818011257036, 
0.109443402126329, 0.110694183864916, 0.110694183864916, 0.111319574734209, 
0.111319574734209, 0.111319574734209, 0.112570356472795, 0.114446529080675, 
0.114446529080675, 0.114446529080675, 0.114446529080675, 0.115071919949969, 
0.115697310819262, 0.118198874296435, 0.119449656035022, 0.119449656035022, 
0.119449656035022, 0.120700437773609, 0.120700437773609, 0.121325828642902, 
0.121951219512195, 0.121951219512195, 0.122576610381488, 0.122576610381488, 
0.122576610381488, 0.122576610381488, 0.123202001250782, 0.123827392120075, 
0.125703564727955, 0.127579737335835, 0.127579737335835, 0.127579737335835, 
0.127579737335835, 0.127579737335835, 0.128830519074422, 0.128830519074422, 
0.129455909943715, 0.129455909943715, 0.130706691682301, 0.131957473420888, 
0.132582864290181, 0.132582864290181, 0.134459036898061, 0.135084427767355, 
0.136335209505941, 0.136960600375235, 0.136960600375235, 0.136960600375235, 
0.137585991244528, 0.138211382113821, 0.138211382113821, 0.138836772983114, 
0.140712945590994, 0.140712945590994, 0.141338336460288, 0.141338336460288, 
0.141963727329581, 0.141963727329581, 0.149468417761101), precision = c(0.584615384615385, 
0.590909090909091, 0.597014925373134, 0.602941176470588, 0.594202898550725, 
0.597222222222222, 0.594594594594595, 0.6, 0.602564102564103, 
0.607594936708861, 0.6, 0.592592592592593, 0.597560975609756, 
0.595238095238095, 0.588235294117647, 0.581395348837209, 0.579545454545455, 
0.573033707865168, 0.566666666666667, 0.571428571428571, 0.56989247311828, 
0.563829787234043, 0.568421052631579, 0.572916666666667, 0.56701030927835, 
0.571428571428571, 0.575757575757576, 0.57, 0.568627450980392, 
0.563106796116505, 0.557692307692308, 0.553571428571429, 0.548672566371681, 
0.543859649122807, 0.538461538461538, 0.542372881355932, 0.53781512605042, 
0.541666666666667, 0.537190082644628, 0.532786885245902, 0.536585365853659, 
0.540322580645161, 0.544, 0.543307086614173, 0.5390625, 0.538461538461538, 
0.537878787878788, 0.537313432835821, 0.540740740740741, 0.536764705882353, 
0.536231884057971, 0.531914893617021, 0.528169014084507, 0.524475524475524, 
0.520833333333333, 0.524137931034483, 0.523809523809524, 0.52027027027027, 
0.526666666666667, 0.52317880794702, 0.516339869281046, 0.512987012987013, 
0.522292993630573, 0.518987341772152, 0.527950310559006, 0.52760736196319, 
0.524390243902439, 0.521212121212121, 0.526946107784431, 0.529761904761905, 
0.526627218934911, 0.526315789473684, 0.526011560693642, 0.528735632183908, 
0.531428571428571, 0.531073446327684, 0.527777777777778, 0.524861878453039, 
0.524590163934426, 0.527173913043478, 0.52972972972973, 0.529100529100529, 
0.528795811518325, 0.525773195876289, 0.528205128205128, 0.525510204081633, 
0.517587939698492, 0.52, 0.517412935323383, 0.51980198019802, 
0.51219512195122, 0.514563106796116, 0.514423076923077, 0.52132701421801, 
0.523584905660377, 0.523364485981308, 0.52093023255814, 0.518348623853211, 
0.520547945205479, 0.518181818181818, 0.515837104072398, 0.511210762331839, 
0.508928571428571, 0.506666666666667, 0.508849557522124, 0.5, 
0.497854077253219, 0.497872340425532, 0.504201680672269, 0.502092050209205, 
0.504166666666667, 0.506224066390041, 0.506172839506173, 0.508196721311475, 
0.506122448979592, 0.510121457489879, 0.51004016064257, 0.508, 
0.503968253968254, 0.498054474708171, 0.496124031007752, 0.494252873563218, 
0.492424242424242, 0.490566037735849, 0.488805970149254, 0.488888888888889, 
0.488970588235294, 0.487179487179487, 0.489208633093525, 0.48936170212766, 
0.487632508833922, 0.48943661971831, 0.493006993006993, 0.491349480968858, 
0.487972508591065, 0.491467576791809, 0.496621621621622, 0.493288590604027, 
0.486842105263158, 0.486928104575163, 0.485342019543974, 0.482200647249191, 
0.482315112540193, 0.482428115015974, 0.480891719745223, 0.481132075471698, 
0.479623824451411, 0.48125, 0.479750778816199, 0.478260869565217, 
0.476780185758514, 0.478527607361963, 0.480122324159021, 0.480243161094225, 
0.478787878787879, 0.478915662650602, 0.477477477477477, 0.479041916167665, 
0.476331360946746, 0.47787610619469, 0.478260869565217, 0.479768786127168, 
0.478386167146974, 0.474285714285714, 0.473239436619718, 0.472067039106145, 
0.472222222222222, 0.470914127423823, 0.472375690607735, 0.471232876712329, 
0.464864864864865, 0.463611859838275, 0.46505376344086, 0.466487935656836, 
0.467914438502674, 0.468253968253968, 0.467018469656992, 0.467191601049869, 
0.464751958224543, 0.463541666666667, 0.465116279069767, 0.465648854961832, 
0.464467005076142, 0.462121212121212, 0.46095717884131, 0.462311557788945, 
0.461346633416459, 0.466666666666667, 0.466992665036675, 0.465853658536585, 
0.463592233009709, 0.463942307692308, 0.462829736211031, 0.463007159904535, 
0.464285714285714, 0.463182897862233, 0.462264150943396, 0.460093896713615, 
0.456876456876457, 0.455813953488372, 0.454965357967667, 0.456221198156682, 
0.457858769931663, 0.461538461538462, 0.460496613995485, 0.458426966292135, 
0.453333333333333, 0.452328159645233, 0.45374449339207, 0.452747252747253, 
0.453947368421053, 0.451965065502183, 0.453362255965293, 0.455723542116631, 
0.456896551724138, 0.455913978494624, 0.459401709401709, 0.460554371002132, 
0.461864406779661, 0.463002114164905, 0.461052631578947, 0.460084033613445, 
0.460251046025105, 0.459459459459459, 0.457556935817805, 0.457731958762887, 
0.458248472505092, 0.456389452332657, 0.456565656565657, 0.455645161290323, 
0.455823293172691, 0.454909819639279, 0.449248120300752), cutoff = c(0.7, 
0.695652173913043, 0.694444444444444, 0.694117647058824, 0.693333333333333, 
0.692307692307692, 0.691358024691358, 0.691176470588235, 0.690140845070423, 
0.689655172413793, 0.688888888888889, 0.688311688311688, 0.6875, 
0.686746987951807, 0.686567164179104, 0.686046511627907, 0.685714285714286, 
0.684210526315789, 0.683544303797468, 0.683333333333333, 0.680555555555556, 
0.68, 0.67948717948718, 0.67741935483871, 0.676923076923077, 
0.676056338028169, 0.675675675675676, 0.675324675324675, 0.671641791044776, 
0.671428571428571, 0.671052631578947, 0.666666666666667, 0.662650602409639, 
0.662162162162162, 0.661764705882353, 0.661538461538462, 0.658536585365854, 
0.657894736842105, 0.657534246575342, 0.657142857142857, 0.65625, 
0.653846153846154, 0.653333333333333, 0.652777777777778, 0.652173913043478, 
0.650602409638554, 0.65, 0.648648648648649, 0.647887323943662, 
0.647058823529412, 0.645569620253165, 0.643835616438356, 0.64367816091954, 
0.642857142857143, 0.641975308641975, 0.640625, 0.64, 0.639344262295082, 
0.638888888888889, 0.63855421686747, 0.63768115942029, 0.6375, 
0.636363636363636, 0.635135135135135, 0.633802816901408, 0.631578947368421, 
0.63013698630137, 0.62962962962963, 0.628571428571429, 0.627118644067797, 
0.626865671641791, 0.625, 0.623529411764706, 0.622950819672131, 
0.622222222222222, 0.621951219512195, 0.621621621621622, 0.621212121212121, 
0.619718309859155, 0.619047619047619, 0.618421052631579, 0.617647058823529, 
0.617283950617284, 0.616438356164384, 0.615384615384615, 0.614035087719298, 
0.613333333333333, 0.6125, 0.611940298507463, 0.611764705882353, 
0.61038961038961, 0.609375, 0.609195402298851, 0.608695652173913, 
0.608108108108108, 0.607594936708861, 0.606060606060606, 0.605633802816901, 
0.605263157894737, 0.604938271604938, 0.604651162790698, 0.602941176470588, 
0.602739726027397, 0.602564102564103, 0.602272727272727, 0.6, 
0.597826086956522, 0.597402597402597, 0.597222222222222, 0.597014925373134, 
0.596774193548387, 0.594936708860759, 0.594202898550725, 0.593220338983051, 
0.592592592592593, 0.591549295774648, 0.590909090909091, 0.590361445783133, 
0.588235294117647, 0.586666666666667, 0.585365853658537, 0.584615384615385, 
0.583333333333333, 0.582278481012658, 0.582089552238806, 0.581081081081081, 
0.580645161290323, 0.580246913580247, 0.579710144927536, 0.578947368421053, 
0.578313253012048, 0.578125, 0.577464788732394, 0.576923076923077, 
0.575342465753425, 0.575, 0.573333333333333, 0.573170731707317, 
0.571428571428571, 0.569620253164557, 0.569230769230769, 0.568965517241379, 
0.567901234567901, 0.567567567567568, 0.567164179104478, 0.565789473684211, 
0.565217391304348, 0.564705882352941, 0.563380281690141, 0.5625, 
0.561797752808989, 0.561643835616438, 0.560975609756098, 0.560606060606061, 
0.55952380952381, 0.558823529411765, 0.558441558441558, 0.557377049180328, 
0.557142857142857, 0.556962025316456, 0.555555555555556, 0.55421686746988, 
0.554054054054054, 0.552631578947368, 0.552238805970149, 0.551282051282051, 
0.550724637681159, 0.55, 0.549295774647887, 0.547945205479452, 
0.546666666666667, 0.546511627906977, 0.544117647058823, 0.54320987654321, 
0.542168674698795, 0.541666666666667, 0.541176470588235, 0.540540540540541, 
0.53968253968254, 0.539473684210526, 0.538461538461538, 0.537313432835821, 
0.536585365853659, 0.536231884057971, 0.535714285714286, 0.535211267605634, 
0.534246575342466, 0.533333333333333, 0.532467532467532, 0.531645569620253, 
0.53125, 0.529411764705882, 0.528571428571429, 0.527777777777778, 
0.527272727272727, 0.527027027027027, 0.526315789473684, 0.525641025641026, 
0.525, 0.524590163934426, 0.524390243902439, 0.523255813953488, 
0.523076923076923, 0.522388059701492, 0.521739130434783, 0.52112676056338, 
0.520547945205479, 0.52, 0.519480519480519, 0.518987341772152, 
0.518518518518518, 0.518072289156627, 0.517647058823529, 0.515151515151515, 
0.514285714285714, 0.513888888888889, 0.513513513513513, 0.513157894736842, 
0.512820512820513, 0.5125, 0.51219512195122, 0.511627906976744, 
0.508196721311475, 0.507692307692308, 0.507462686567164, 0.507246376811594, 
0.507042253521127, 0.506849315068493, 0.506666666666667, 0.506493506493506, 
0.506329113924051, 0.505747126436782, 0.5)), .Names = c("recall", 
"precision", "cutoff"), row.names = 55:287, class = "data.frame")

2 个答案:

答案 0 :(得分:4)

设置lineend = "round"大大改善了情节

ggplot(curve.df, aes(x = recall, y = precision, color = cutoff)) +
   geom_line(size = 5, lineend = "round") 

答案 1 :(得分:3)

ggplot无法使用多种颜色绘制单行。你的情节中的“随机”位实际上是超短小线(比它们长得多)的顶部和底部连接在cutoff中足够接近的点以共享相同的颜色。

幸运的是,您的数据非常密集,实际上不需要线图。我们可以只绘制积分,所有问题都消失了 - 如果我们把它们做得足够大,这似乎就是你想要的。 (如果放大了数据摘录,您将看到各个点,但我扩展了限制以显示您实际使用的绘图大小的数据密度。相邻点之间recall的平均差异是.00054,因此在0到1的范围内,您的数据非常密集!)

我还展示了一个黄土更平滑的版本 - 你当然可以使用带宽来进行更多或更少的平滑处理。这可能是也可能不是优选的。

raw_plot = ggplot(df, aes(recall, precision, color = cutoff)) + 
    geom_point(size = 3) + 
    coord_fixed(xlim = c(0, 1), ylim = c(0, 1)) +
    labs(title = "Raw")

df$smooth = predict(loess(precision ~ recall, data = df))
smooth_plot = ggplot(df, aes(recall, smooth, color = cutoff)) +
    geom_point(size = 3) +
    coord_fixed(xlim = c(0, 1), ylim = c(0, 1)) + 
    labs(title = "Smooth")

gridExtra::grid.arrange(raw_plot, smooth_plot, nrow = 1)

enter image description here