在以下脚本中:
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)
运行此脚本会产生以下错误:
Error in f(..., self = self) : Breaks and labels are different lengths
请注意,在脚本中添加了最后一行之后,出现了此错误。
您知道如何解决此错误吗?
仅发现问题是由plot_18
的数据引起的。该图的数据:
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
))
当我移除labels = percent(0.25*0:4)
时,错误消失了。但是,我添加了它以便将图例标签显示为25%, 50%, 75%, 100%
。我应该怎么做才能解决这个问题?对我来说,显示我提到的图例标签很重要。
答案 0 :(得分:3)
我猜您的标签指示的是计数的分位数。因此,首先让我们找回没有百分比标签的原始图:
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"))
为每个图定义0,0.25 ..标签的问题是,取决于范围,您可能会有不同的中断次数。您可以看到上面的情节(plot_17_count),只有3个中断。为了正确放置百分位数,您需要访问geom_hex ..中的十六进制计数。这可能并不容易。一种解决方法是改变比例。
我们定义了所需的分位数,并进行了转换
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))
将它们放在一起:
library(gridExtra)
grid.arrange(plot_17_count,plot_17_perc)