Increase line width without stochastic bars ggplot - r
Does anyone know if it's possible to increase the line width in ggplot2 in a smooth fashion without adding random lines that stick out? Here's my original line plot and with size increased to 5:
> ggplot(curve.df, aes(x=recall, y=precision, color=cutoff)) +
> geom_line(size=1)
Ideally, the final image would look something like the following plot from the PRROC Package, but I have another problem with plotting from there in that gridlines and ablines do not correspond to the axis tickmarks.
Here I first called
> grid()
and then called
> abline(v=seq(0,1,.2), h=seq(0,1,.2))
Honestly would appreciate any way to be able to draw this curve with a wider line to see clear colors and a grid that corresponds to the axis tickmarks. Thanks!
Here's a sample of the data from cutoff .5 to .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")
Setting lineend = "round" greatly improves the plot
ggplot(curve.df, aes(x = recall, y = precision, color = cutoff)) +
geom_line(size = 5, lineend = "round")
ggplot can't plot a single line with multiple colors. The "stochastic" bits of your plot are actually the tops and bottoms of super little short lines (that are much thicker than they are long) connecting points that are close enough together in cutoff to share the same color.
Luckily, your data is so dense, a line plot is actually unnecessary. We can just plot points and all the problems go away - if we make them big enough, which seems to be what you want. (You will see the individual points if your zoom in on the data excerpt provided, but I expanded the limits to make show the data density on the size of plot you are really using. The average difference in recall between adjacent points is .00054, so on the scale of 0 to 1 your data is very dense!)
I also show a version with a loess smoother - you can of course play with the bandwidth for more or less smoothing. This may or may not be preferable.
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)
Related
Add legend based on line types and colors in ggplot
I have created the following plot using plot() function and I would like to convert it to ggplot() and add colors in the line types like: and also a legend for 'predicted' (normal line) and 'observed' values (dashed line) like: Here is my code: # Creating some data first scoregroepen <- seq(from = 1, to = 8, by = 1) s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95) s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94) s_toets_conf_low <- s_toets_observed-0.03 s_toets_conf_high <- s_toets_observed+0.045 plot(scoregroepen,s_toets_predicted, type="b", ylab = "proporties", ylim = c(0,1)) lines(scoregroepen, s_toets_observed, type="b", lty = 2 ) lines(scoregroepen, s_toets_conf_low, lty = 2 ) lines(scoregroepen, s_toets_conf_high, lty = 2 )
Try this which is close to what you expect. I have re arranged your variables in a dataframe to reshape them and then sketch the plot. Here the code: library(ggplot2) library(dplyr) library(tidyr) # Creating some data first scoregroepen <- seq(from = 1, to = 8, by = 1) s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95) s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94) s_toets_conf_low <- s_toets_observed-0.03 s_toets_conf_high <- s_toets_observed+0.045 df <- data.frame(scoregroepen,s_toets_observed,s_toets_predicted, s_toets_conf_low,s_toets_conf_high) #Plot df %>% pivot_longer(-scoregroepen) %>% ggplot(aes(x=scoregroepen,y=value,color=name,linetype=name))+ geom_line()+ geom_point(aes(shape=name))+ scale_color_manual(values=c('blue','blue','tomato','cyan3'), breaks=c('s_toets_observed','s_toets_predicted'), labels=c('Observed','Predicted'))+ scale_shape_manual(values=c(NA,NA,1,4), breaks=c('s_toets_observed','s_toets_predicted'), labels=c('Observed','Predicted'))+ scale_linetype_manual(values=c('dotted','dotted','dashed','solid'), breaks=c('s_toets_observed','s_toets_predicted'), labels=c('Observed','Predicted'))+ labs(color='var',shape='var',linetype='var') Output:
The error "Breaks and labels are different lengths" appears when using grid.arrange
In the following script: dataset_prices2017 <- read.csv("/home/nasser/Desktop/prices2017.csv") dataset_prices2018 <- read.csv("/home/nasser/Desktop/prices2018.csv") dataset_prices2019 <- read.csv("/home/nasser/Desktop/prices2019.csv") selectedDF <- dataset_prices2017 %>% select(Price, Volume) plot_17 <- ggplot(selectedDF, aes(Price, Volume)) + geom_hex(bins = 30) + theme_bw() + geom_smooth(method="lm") + labs(fill = "Count") plot_17 <- plot_17 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"), labels = percent(0.25*0:4)) selectedDF <- dataset_prices2018 %>% select(Price, Volume) plot_18 <- ggplot(selectedDF, aes(Price, Volume)) + geom_hex(bins = 30) + theme_bw() + geom_smooth(method="lm") + labs(fill = "Count") plot_18 <- plot_18 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"), labels = percent(0.25*0:4)) selectedDF <- dataset_prices2019 %>% select(Price, Volume) plot_19 <- ggplot(selectedDF, aes(Price, Volume)) + geom_hex(bins = 30) + theme_bw() + geom_smooth(method="lm") + labs(fill = "Count") plot_19 <- plot_19 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"), labels = percent(0.25*0:4)) fullPlot <- grid.arrange(plot_17, plot_18, plot_19, ncol=3, nrow = 1) Running this script generates the following error: Error in f(..., self = self) : Breaks and labels are different lengths Note that I got this error after adding the last line in the script. Do you have any idea how to solve this error? Just figured out that the problem is caused by the data of plot_18. The data for that plot: structure(list(Price = c(0.5219551282, 0, 0, 0.194543297766667, 0.46875, 0.8108108108, 0.2962962963, 0.436213991763333, 0, 0.1904761905, 0.0208333333, 0, 0, 0.630769230766667, 0.0074626866, 0, 0.7037037037, 0.70736842106, 0.597250859103333, 0.88, 0.6666666667, 0, 0.13523391815, 0.035490830643333, 0.02705627708, 0.0666666667, 0.29674796745, 0.41944444446, 0.1621621622, 0.09127725854, 0.9234567901, 0.582170542623333, 0.28962962964, 0.79312169317, 0.793380614656667, 0.875187969906667, 0.03595113442, 0.113242009126667, 0.0293040293, 0.381194511696667, 0.0344827586, 0.09544715448, 0.161734693876667, 0.123095238113333, 0.7075949367, 0, 0.8125, 0.8571428571, 0.9, 0.9333333333, 0.79920634921, 0.0085106383, 0.713333333323333, 0.62150537635, 0.232905982933333, 0.868, 0.851580459763333, 0.743589743583333, 0.744298245626667, 0, 0.434379671133333, 0.797916666666667, 0.2222222222, 0.612962962953333, 0.591333333333333, 0.516666666666667, 0.53809523808, 0.611515151526667, 0.64246575339, 0.7101449275, 0.122445561153333, 0.65848765432, 0.2558526441, 0.641025641, 0.347892720293333, 0.0769230769, 0.353939393936667, 0.978333333333333, 0.755238095256667, 0.863333333333333, 0.6751243781, 0.67297297298, 0.5344017094, 0.0057142857, 0.921568627463333, 0.52644927536, 0.128333333333333, 0, 0.1904761905, 0.471929824536667, 0.15972222225, 0, 0, 0, 0.446258503403333, 0.44210526318, 0.82189054726, 0.628169014093333, 0, 0.2307692308, 0.191304347823333, 0.4004385965, 0.83441734419, 0.20449172577, 0.746598639443333, 0.211494252846667, 0.8695652174, 0.519999999993333, 0.113333333333333, 0.637142857133333, 0.699420289853333, 0.7391304348, 0.457575757543333, 0.461904761866667, 0.788888888883333, 0.503065134103333, 0.499616858236667, 0.793888888883333, 0.70901542112, 0.591358024693333, 0.47769028871, 0.456692913383333, 0.40625, 0, 0.7678571429, 0.796969696963333, 0.914886731406667, 0.102424242446667, 0.487804878, 0.1923076923, 0, 0.3658536585, 0.634782608673333, 0.385028248613333, 0.69864864864, 0.60404040406, 0.814880952386667, 0.533582089543333, 0.8695652174, 0, 0.715942029016667, 0.74987277353, 0.864455782326667, 0.624999999986667, 0.3333333333, 0.4642857143, 0.10434782606, 0.078095238096667, 0.0119047619, 0.427472527466667, 0.0909090909, 0.6071428571, 0.6071428571, 0.064516129, 0.500266666666667, 0.5263157895, 0, 0.75256410254, 0.691836734676667, 0.2083333333, 0.5, 0.85625, 0.6666666667, 0.60065359476, 0.5, 0.279012345676667, 0.1176470588, 0.5185185185, 0.8085106383, 0.5365853659, 0.9090909091, 0.82589928058, 0, 0, 0.03175438598, 0.511764705856667, 0.0196078431, 0.693650793643333, 0.18378378382, 0.10596205961, 0.022916666666667, 0.288888888903333, 0.306666666673333, 0, 0.68078703703, 0.744217687073333, 0.6428571429, 0.828462709286667, 0.887265917566667, 0.713710691823333, 0.76077643908, 0.72816091955, 0.648370927326667, 0.76565656568, 0.315458937203333, 0.29583333335, 0.714765100693333, 0.812159329126667, 0.05, 0.713681592026667, 0.10976645433, 0.75625, 0, 0.42777777777, 0.6842105263, 0.79678362574, 0.71025641027, 0.88205128204, 0.84959349595, 0.650104821806667, 0.9032258065, 0.895652173936667, 0.374390243886667, 0.531182795713333, 0.8429378531, 0.08, 0.030075188, 0, 0.0134228188, 0.93787878788, 0.7804878049, 0.556666666676667, 0.506630824373333, 0, 0.368527918786667, 0.7439716312, 0.21818181816, 0.7647058824, 0.90188034188, 0.792857142846667, 0.157142857183333, 0.19814814816, 0.799047619056667, 0.8947368421, 0, 0.318562091513333, 0.40884353743, 0.085815602833333, 0.14280701756, 0.804624277473333, 0.5714285714, 0.867759562826667, 0.624074074063333, 0.24139784949, 0.42, 0.716269841283333, 0.692539109493333, 0.398290598286667, 0.474193548386667, 0.5384615385, 0.225925925923333, 0.1666666667, 0.156540084396667, 0.6666666667, 0.82459546925, 0.683681592026667, 0.70945945945, 0.677494692146667, 0.65738396624, 0.83459119496, 0.794179894193333, 0.533630952366667, 0.639999999996667, 0.620648967546667, 0.301428571416667, 0.10307017545, 0.72935323382, 0.43850129199, 0.43074712644, 0.432835820883333, 0.1666666667, 0.668965517236667, 0.062745098026667, 0.625, 0.0425531915, 0.617567567546667, 0.069005847956667, 0.081836327346667, 0.6956521739, 0.9818181818, 0.5, 0.5, 0.4324324324, 0.2666666667, 0.2857142857, 0, 0.4027777778, 0.71195402298, 0.64342105263, 0.7741935484, 0.186419753086667, 0.822710622716667, 0, 0, 0.021875, 0.091484184913333, 0, 0, 0, 0, 0.6666666667, 0.737435897473333, 0.759420289843333, 0.6666666667, 0.295928753186667, 0.75420054203, 0.039602925803333, 0.6666666667, 0.183333333326667, 0.74521072797, 0.43983739835, 0.653645833333333, 0, 0.1935483871, 0.22883895131, 0.72955465589, 0.380459770086667, 0.844061302676667, 0.952873563223333, 0.3325), Volume = c(0.002414102564097, 0.001174398625467, 0.0010256410256, 0.001051838671393, 0.002878125, 0.004077477477493, 0.00177037037037, 0.003053909465023, 0.00206551724138, 0.002257142857167, 0.002033333333333, 0.001, 0.001072380952367, 0.0029717948718, 0.002232587064677, 0.00201036414567, 0.0021, 0.00292771929824, 0.003005841924393, 0.008772666666667, 0.002482539682533, 0.0016028328612, 0.001884795321637, 0.001899568500543, 0.001675757575777, 0.0012, 0.002775880758787, 0.002668333333327, 0.00151171171169, 0.001388785046723, 0.00607530864197, 0.00415348837209, 0.002834814814827, 0.008986243386227, 0.0020964539007, 0.005417543859647, 0.00207905759162, 0.002877168949767, 0.001929792429827, 0.003026553672313, 0.00150344827588, 0.001880108401087, 0.001844047619057, 0.002190952380963, 0.00348396624472, 0.0010344827586, 0.006220833333333, 0.00308730158726, 0.002998333333333, 0.00227777777778, 0.002014484126987, 0.00126765957446, 0.003718888888883, 0.005132616487453, 0.002628205128203, 0.004248, 0.007026293103447, 0.002628205128207, 0.003443421052633, 0.001, 0.002645739910313, 0.008983333333333, 0.0017777777778, 0.002870370370367, 0.003363333333333, 0.00210256410256, 0.003542857142857, 0.002534848484847, 0.00231506849313, 0.002258454106293, 0.001163484087093, 0.005761111111113, 0.00211039809863, 0.002683760683757, 0.016383908045983, 0.002228205128207, 0.00215212121213, 0.004805, 0.010981904761907, 0.00828875, 0.00274427860695, 0.002558108108107, 0.002019444444443, 0.0015914285714, 0.004362745098053, 0.002043840579703, 0.002366666666667, 0.001103703703693, 0.001742857142873, 0.004219298245603, 0.00166712962963, 0.001, 0.0013430107527, 0.0015434782609, 0.002538775510207, 0.003559899749367, 0.0025592039801, 0.00343615023474, 0.001239955849933, 0.002176923076903, 0.001110144927567, 0.00341403508773, 0.003459349593493, 0.002460283687937, 0.00319863945578, 0.00226666666666, 0.018426086956527, 0.003018888888873, 0.002331666666667, 0.003414285714283, 0.002526956521737, 0.00519710144927, 0.002732034632027, 0.002861904761897, 0.003106666666673, 0.002896934865907, 0.00257969348659, 0.003066111111117, 0.00349881376038, 0.00244814814815, 0.00230262467191, 0.002416272965877, 0.00248125, 0.001801666666667, 0.007639880952377, 0.002253030303033, 0.009329126213587, 0.00166909090911, 0.00293821138211, 0.004464102564097, 0.002315942029033, 0.003039024390237, 0.015501449275363, 0.008321468926553, 0.009915315315317, 0.004964646464643, 0.00526488095237, 0.004199004975113, 0.00949130434782, 0.001162745098033, 0.00265942028985, 0.002461323155213, 0.013436054421777, 0.003893253968247, 0.001716666666667, 0.00195595238097, 0.0019130434783, 0.001076190476233, 0.001866269841277, 0.003128937728933, 0.0021, 0.00230238095237, 0.00248333333332, 0.00281397849465, 0.002320533333333, 0.002299122807017, 0.002, 0.00275, 0.002816326530617, 0.002254166666667, 0.00369761904762, 0.00381875, 0.003125925925927, 0.005600000000007, 0.00369861111111, 0.002507407407373, 0.0014117647059, 0.003214814814817, 0.002590070921973, 0.00297479674797, 0.01804242424242, 0.00463980815348, 0.001, 0.0010222222222, 0.001853947368427, 0.0017058823529, 0.0011568627451, 0.003668253968263, 0.00215675675675, 0.001922222222237, 0.002383333333333, 0.00296495726495, 0.04700512820514, 0.001080952380933, 0.007685648148143, 0.0026112244898, 0.014720238095237, 0.00248949771689, 0.00493520599251, 0.00640553459119, 0.004878313253013, 0.00365545977011, 0.004275689223057, 0.01131245791246, 0.003004106280193, 0.00678472222223, 0.005784004474273, 0.004531446540867, 0.001835, 0.00332611940299, 0.001566454352443, 0.01443125, 0.001049275362333, 0.007142424242427, 0.0240552631579, 0.007115204678353, 0.003953846153853, 0.00365705128204, 0.005986720867213, 0.002818448637317, 0.00557311827956, 0.00247934782609, 0.002582926829263, 0.003732258064523, 0.00645706214689, 0.004855999999993, 0.001249122807027, 0.0010241545894, 0.00103154362418, 0.013604545454543, 0.005505691056917, 0.002934444444437, 0.002545340501793, 0.0010238095238, 0.00221370558375, 0.00335000000001, 0.001788552188527, 0.00236274509804, 0.013679487179487, 0.005909523809527, 0.003596190476187, 0.002676851851847, 0.00611942857143, 0.005988596491227, 0.002, 0.002481045751637, 0.00392789115644, 0.00167568389058, 0.002203684210527, 0.003684778420043, 0.0097238095238, 0.00281311475409, 0.003364074074077, 0.002609677419357, 0.002558, 0.002856746031737, 0.00735330926595, 0.001780128205137, 0.00261075268818, 0.002848717948717, 0.00189058641975, 0.001945555555547, 0.001983966244727, 0.005273333333327, 0.004203883495147, 0.003079104477617, 0.003092342342337, 0.003043312101907, 0.00304556962024, 0.003135220125783, 0.004403703703707, 0.002158333333327, 0.004803902439023, 0.00234247787612, 0.0019919047619, 0.001954385964913, 0.003713432835827, 0.002959173126613, 0.00378965517242, 0.00259552238807, 0.002186111111113, 0.001156704980857, 0.002354901960793, 0.01525625, 0.0015957446809, 0.00489594594594, 0.001830409356733, 0.00169753825682, 0.00253671497585, 0.01430363636364, 0.0025, 0.016829166666667, 0.002627027027023, 0.002300000000033, 0.0022857142857, 0.002, 0.001732870370397, 0.002788850574713, 0.00302105263158, 0.008280645161293, 0.002154938271587, 0.003345421245423, 0.001345662100463, 0.001486666666667, 0.00210078125, 0.002824574209243, 0.001466666666667, 0.002, 0.002, 0.001147619047633, 0.00367654320988, 0.00385538461539, 0.003284541062797, 0.00693765432099, 0.001989058524173, 0.004204607046073, 0.002009299895507, 0.004667777777773, 0.00320292397661, 0.003397318007663, 0.00231788617884, 0.005576388888893, 0.001764102564133, 0.002895698924767, 0.001935205992517, 0.002502699055333, 0.002919540229867, 0.00350996168583, 0.003030459770113, 0.002411666666667)), .Names = c("Price", "Volume"), class = "data.frame", row.names = c(NA, -320L )) When I remove the labels = percent(0.25*0:4), the error is gone. However, I added this in order to show the legend labels as 25%, 50%, 75%, 100%. What should I do to solve this issue? It's important for me to show the legend labels as I mentioned.
I am guessing what your labels indicate are the quantiles of counts. So first let's get back what your original plot is without percentage labels: library(scales) library(ggplot2) library(hexbin) library(RColorBrewer) plot_17 <- ggplot(selectedDF, aes(Price, Volume)) + geom_hex(bins = 30) + theme_bw() + geom_smooth(method="lm") + labs(fill = "Count") plot_17_count <- plot_17 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2")) The problem with defining a 0,0.25.. labels for every plot is, you might have different number of breaks depending on the range. You can see for the plot above (plot_17_count), it has only 3 breaks. For you to place the percentiles correctly, you need to access the hexbin count inside geom_hex.. which might not be so easy. One workaround is to transform the scale. We define the quantiles you need, and a transformation BR = percent(0.25*0:4) # unfortunately no inverse, I just put in identity so the function works min_max_trans = trans_new("min_max",function(i)i/(max(i)-min(i)),inverse=identity) plot_17_perc <- plot_17 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"), trans=min_max_trans,labels=percent(0.25*0:4)) Put them together: library(gridExtra) grid.arrange(plot_17_count,plot_17_perc)
Remove white space between two plots resulting from grid.arrange function in R
I'd like to eliminate the white space between my two forest plots that I plotted side-by-side using grid.arrange(). Before you vote down or redirect - Before asking this question, I have spent hours attempting every solution posed in each of the responses I've seen here for similar questions without achieving my desired result. First, here is my dataset and code: library(meta) library(grid) library(gridExtra) df <- structure(list(study = 1:7, sens = c(0.88, 0.86, 0.75, 0.9, 0.91, 0.93, 0.98), sens.se = c(0.13, 0.08, 0.2, 0.06, 0.13, 0.15, 0.66), sens2 = c(0.76, 0.68, 0.9, 0.82, 0.76, 0.85, 0.76), sens.se2 = c(0.14, 0.08, 0.2, 0.06, 0.14, 0.15, 0.66)), class = "data.frame", row.names = c(NA, -7L)) ## setting up meta-analysis model using library(meta) res1 <- metagen(TE=sens, seTE=sens.se, data=df, studlab=study) res2 <- metagen(TE=sens2, seTE=sens.se2, data=df, studlab=study) ## changing plots to grid graphical objects to use grid.arrange fp1 <- grid.grabExpr(forest(res1, data=df, method.tau="REML", comb.random=TRUE, leftcols="studlab", rightcols=c("effect", "ci"))) fp2 <- grid.grabExpr(forest(res2, data=df, method.tau="REML", comb.random=TRUE, leftcols="studlab", rightcols=c("effect", "ci"))) ## arranging plots side by side: grid.arrange(fp1, fp2, ncol = 2) When I have attempted to use code suggested in responses to similar questions, I get the "only grobs allowed in gList" error code, even though R recognizes the plots as "gTrees" because I used the grid.grabExpr function. I've tried coercing the gTrees into grobs via: p1 <- as.grob(fp1) p2 <- as.grob(fp2) , which only creates null values in the global environment. I would greatly appreciate some help with this!
Perhaps this does what you are looking for: grid.grabExpr( forest( res1, data=df, method.tau="REML", comb.random=TRUE, leftcols="studlab", rightcols=c("effect", "ci") ), height = 1, width = 2 ) -> fp1 grid.grabExpr( forest( res2, data=df, method.tau="REML", comb.random=TRUE, leftcols="studlab", rightcols=c("effect", "ci") ), height = 1, width = 2 ) -> fp2 grid.arrange(fp1, fp2, ncol = 2, vp=viewport(width=1, height=1, clip = TRUE))
Create A Stat_Density_2D Plot By Binning Fill Variable
I have been trying to create a density plot in R that looks similar to the picture below. In my code below, I have created a stat_density_2D plot that successfully plots my data, however, it fails to recognize my fill variable (in this case exitspeed) and only plots one color. Upon further research, I believe the reason for this is because stat_density_2d bins the fill into levels. The problem I am having is that my fill variable has multiple values for the points within a particular level ultimately resulting in a density plot that only displays one color. Does anyone know how to bin my data so that my density plot can recognize the fill variable (exitspeed)? Please see below for the dataset and R code. Thanks in advance! Data: structure(list(platelocheight = c(2.594, 3.803, 3.254, 3.599, 3.617, 3.297, 2.093, 3.611, 2.842, 3.316, 2.872, 3.228, 3.633, 4.28, 3.309, 2.8, 2.632, 3.754, 2.207, 3.604, 3.443, 2.188, 3.452, 2.553, 3.382, 3.067, 2.986, 2.785, 2.567, 3.804), platelocside = c(0.059, -1.596, -0.65, -0.782, -0.301, -0.104, 0.057, -0.807, 0.003, 1.661, 0.088, -0.32, -1.115, -0.146, -0.364, -0.952, 0.254, 0.109, -0.671, -0.803, -0.212, -0.069, -0.09, -0.472, 0.434, 0.337, 0.723, 0.508, -0.197, -0.635), exitspeed = c(69.891, 73.352, 83.942, 85.67, 79.454, 85.277, 81.078, 73.573, 77.272, 59.263, 97.343, 91.436, 76.264, 83.479, 47.576, 84.13, 60.475, 61.093, 84.54, 69.959, 88.729, 88.019, 82.18, 83.684, 86.296, 90.605, 79.945, 59.899, 62.522, 77.75)), .Names = c("platelocheight", "platelocside", "exitspeed"), row.names = c(NA, 30L), class = "data.frame") R-Code: library(RODBC) library(ggplot2) con=odbcConnect('username',uid='userid', pwd = 'password') df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from tm_sample where pitchcall='InPlay' and exitspeed is not null") topKzone <- 3.5 botKzone <- 1.6 inKzone <- -0.95 outKzone <- 0.95 kZone <- data.frame( x=c(inKzone, inKzone, outKzone, outKzone, inKzone), y=c(botKzone, topKzone, topKzone, botKzone, botKzone) ) df$h <- round(df$platelocheight) df$s <- round(df$platelocside) df$es<- round(df$exitspeed) ggplot(kZone, aes(x,y)) + stat_density_2d(data=df, aes(x=s, y=h, fill=es),geom="polygon") + scale_fill_distiller(palette = "Spectral") + geom_path(lwd=1.5, col="black") + coord_fixed()
Circular plot with related temperature
Hello guys and thanks in advance for your time. I'm trying to create a circular plot with a color map that should be related with different temperature values acquired on a disk in order to verify the homogeneity of the heating, but despite my numerous trial and efforts it just wont work as i'd like it to be (i'm a newbie in programming). Any ideas? Thanks for your attention and have a nice day! import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap x = np.arange(-2, 2, 0.01) y = np.arange(-2, 2, 0.01) X, Y = np.meshgrid(x, y) Z = X**2 + Y**2 + 1 #np.sin(X) * np.cos(Y) * 2 R = 4 Z[Z>R] = 0 colors = [(1,1,1), (0.99, 0.90, 0.68), (1, 0.87, 0.58), (0.93, 0.79, 0.53), (0.97, 0.71, 0.35), (0.84, 0.71, 0.27), (0.74, 0.48, 0.23), (0.65, 0.44, 0.24), (0.56, 0.39, 0.23), (0.48, 0.32, 0.23),( 0.84, 0.53, 0.20), (0.21, 0.2, 0.17)] # R -> G -> B n_bins = [100] cmap_name = 'my_list' fig, axs = plt.subplots(2, figsize=(6, 9)) fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05) for n_bin, ax in zip(n_bins, axs.ravel()): cm = LinearSegmentedColormap.from_list( cmap_name, colors, N=n_bin) im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm) ax.set_title("N bins: %s" % n_bin) fig.colorbar(im, ax=ax) This is a code that i'm trying to modify in order fit my purpose (temperature values correlation is still missing, i know...).