wilkelab / ggridges

Ridgeline plots in ggplot2
https://wilkelab.org/ggridges
GNU General Public License v2.0
411 stars 31 forks source link

Polar Coordinates with scale_fill_gradientn #93

Open joshqsumner opened 8 months ago

joshqsumner commented 8 months ago

Hello, Thanks for developing a great package. I have a pretty niche use case and figured I'd raise it here as a bug/feature request. Basically I'm plotting model predictions from a longitudinal model of hue (circular data) and when I use polar coordinates the fill from scale_fill_gradientn acts strangely (see below with a small subset of data). Basic ggplot's geom_ribbon can also act strangely with polar coordinates, but I do not have a deep enough understanding to know if that is the same core issue. I do not have a strong appreciation for how much effort it might take to change the behavior or if this is a relic of deeper R graphics/ggplot stuff but thank you for your time in checking this issue and developing ggridges.

library(ggplot2)
library(ggridges)
test <- structure(list(time = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
                                1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
                                2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
                                3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
                                4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
                                5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
                                1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
                                2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
                                3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
                                4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
                                5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
                                1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
                                2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
                                3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
                                4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
                                5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L
), value = c(-3.1091461225831, -3.10816442110392, -3.10263526351796, 
             -3.08998889509175, -3.03611866635099, -3.0370186902774, -3.03163138154587, 
             -3.00418704597379, -2.94818930418835, 0.990355213002044, -2.95784522185552, 
             -2.93984709990298, -2.88409132524721, -2.6620177172336, 1.32935731650004, 
             -2.86701790622405, -2.82717555593076, -2.69825602771246, 0.947075137630747, 
             1.45905783193292, -2.75590484121513, -2.67551712631698, -2.26294061905437, 
             1.26005865310439, 1.54827045588378, -2.61493426808334, -2.4360518050342, 
             0.851700162179944, 1.40763803708867, 1.61540801402778, -2.40636579813402, 
             -1.67557904684545, 1.1855426044662, 1.50597909912991, 1.67146635026623, 
             -2.0216492478492, 0.731599484719909, 1.34715283524863, 1.58244341416345, 
             1.71964802090468, -0.51743599269576, 1.07943379902447, 1.45690646242902, 
             1.64940932705133, 1.76251881969511, 0.661848740534804, 1.26224954337715, 
             1.54574310305709, 1.70666062501093, 1.79999572045938, 1.00305201963862, 
             1.39219468437872, 1.62031059270124, 1.75652278109301, 1.83523306720509, 
             1.19418923238954, 1.49280884499124, 1.68305601349661, 1.80150542597926, 
             1.86700301136306, 1.32953432046727, 1.57803722463114, 1.73999897059732, 
             1.84244939735051, 1.89757791854065, 1.44019730535314, 1.64892823674035, 
             1.79006809161188, 1.87948116273007, 1.92815185224859, 1.53284583014266, 
             1.71226762844023, 1.83725054722677, 1.91544733548723, 1.95558502310188, 
             1.61032830377888, 1.7698175677993, 1.8810936570291, 1.94956340738315, 
             1.98177443537473, 1.67907432423992, 1.82083151579583, 1.92248632446263, 
             1.98370874489263, 2.00771962466572, 1.74289470946323, 1.86948901562528, 
             1.96064852311926, 2.01459197882679, 2.0331187656751, 1.80046720985407, 
             1.91506089240377, 1.99714200570396, 2.04570382405066, 2.05717272674588, 
             1.85424798759682, 1.95679347827142, 2.03228459752022, 2.07543630634599, 
             2.08092530933923, 1.90511463609383, 1.99759799105685, 2.06544238960777, 
             2.10383978680376, 2.10425641126282, 1.95257842861739, 2.03705330884793, 
             2.09930729188729, 2.13207756028345, 2.12733834613286, 1.99910083700049, 
             2.07555948282122, 2.13211245674083, 2.15978578951073, 2.14969862850102, 
             2.04207463441892, 2.11294933539243, 2.16414435094677, 2.18718746148073, 
             2.17168176068093, 2.08485900357957, 2.14896338727936, 2.19516498165383, 
             2.2133234705365, 2.19433165042145, 2.12483010263785, 2.18374683426835, 
             2.22561336820793, 2.24044815832898, 2.2163835622589, 2.16359793256981, 
             2.21808038437789, 2.25550686338696, 2.26612732004531, 2.23823204207468, 
             2.20191021887249, 2.25167674653059, 2.28570505535598, 2.29283208499065, 
             2.26031883467215, 2.24147203915613, 2.28424298850615, 2.31556852505844, 
             2.31880360911637, 2.28259323315943, 2.27947615926217, 2.31741010458157, 
             2.34518772952838, 2.34491988115975, 2.30457047917502, 2.31815686889451, 
             2.35002235602855, 2.37436505148282, 2.37058372040927, 2.32758719670909, 
             2.35517434632459, 2.38303027911387, 2.40385512050414, 2.39616066492118, 
             2.35027109381024, 2.39190597338377, 2.41592307876329, 2.43331523536976, 
             2.42277304252228, 2.37343543208707, 2.42829031108926, 2.44882487001928, 
             2.46380967092512, 2.44931353567737, 2.39697545771655, 2.46557741697036, 
             2.48180384183636, 2.49418278880775, 2.47592340194914, 2.42189335703215, 
             2.50329117577046, 2.51438880338433, 2.52485595919152, 2.50391146526691, 
             2.44719514933629, 2.54019390773405, 2.54805309199544, 2.55617607255992, 
             2.53187212014501, 2.47244911093498, 2.57632404658102, 2.58351678067375, 
             2.58878056406826, 2.56112657017001, 2.49875485416563, 2.61368658027097, 
             2.61785604819321, 2.62050905088486, 2.59223597536203, 2.52679024726896, 
             2.65221011257592, 2.65459206443348, 2.6539285147903, 2.62332313537998, 
             2.55548044675967, 2.69049354195045, 2.69091231197787, 2.68796000225647, 
             2.65585153738966, 2.58548014473444, 2.72974682463156, 2.72758683065082, 
             2.72427156591818, 2.68986366556791, 2.61728662009396, 2.77120561143297, 
             2.76551846559399, 2.76122721619547, 2.7258483071277, 2.65160944672966, 
             2.81282889180652, 2.80614691074905, 2.80094193960139, 2.76396119885707, 
             2.68893525425253, 2.85641568494748, 2.85039542656845, 2.84181623143995, 
             2.8046969461427, 2.72934998434532, 2.90237974664899, 2.89504216886721, 
             2.88637882925308, 2.84992165990544, 2.77454559807627, 2.95073100153198, 
             2.94414984853892, 2.9328682452021, 2.89917332781619, 2.8262619224823, 
             3.00142268199337, 2.99393846261913, 2.98406593115502, 2.95336126406257, 
             2.88710122766535, 3.05364280426421, 3.04854161665438, 3.04102562980719, 
             3.01752701458551, 2.96420035977733, 3.11075526966407, 3.1090114117047, 
             3.10509653747607, 3.09630683596879, 3.06757555651992), hue = c(1.8590492865237, 
                                                                            1.91529663802256, 2.23209403196109, 2.95667756895015, 6.04321431720017, 
                                                                            5.99164674475614, 6.30031679800648, 7.87276139782777, 11.0811956643966, 
                                                                            236.743173923797, 10.5279523347421, 11.5591687617843, 14.7537393330423, 
                                                                            27.4776198134647, 256.166563700289, 15.7319741849276, 18.0147727026151, 
                                                                            25.4013175663415, 234.263408268013, 263.597855835265, 22.098283858702, 
                                                                            26.7041606470664, 50.3430532394628, 252.196042761821, 268.709362666937, 
                                                                            30.1753027346938, 40.4245128963143, 228.798824703519, 260.65171860726, 
                                                                            272.556061395402, 42.125395802291, 83.9963923752026, 247.926587668861, 
                                                                            266.286246415058, 275.76796746839, 64.1680304424436, 221.917562768397, 
                                                                            257.186171818829, 270.667328949838, 278.528573845863, 150.353101450371, 
                                                                            241.847000947875, 263.474591442518, 274.504193129554, 280.984889680909, 
                                                                            217.921139508693, 252.321571527826, 268.564556016627, 277.784450874286, 
                                                                            283.132157923932, 237.470647357366, 259.766879675448, 272.836958462122, 
                                                                            280.641341975211, 285.151109173701, 248.422002955888, 265.531646437797, 
                                                                            276.432006257469, 283.218657678529, 286.971392889319, 256.176705280568, 
                                                                            270.414872885902, 279.694597372334, 285.564574434606, 288.723206029599, 
                                                                            262.517227262849, 274.476628685171, 282.563346690398, 287.686338298774, 
                                                                            290.474963394176, 267.825596711401, 278.1057085065, 285.266702264195, 
                                                                            289.747048202997, 292.046768302731, 272.265015436991, 281.403077143008, 
                                                                            287.778727416597, 291.701755136198, 293.547311093894, 276.203872267728, 
                                                                            284.325961059509, 290.150352563327, 293.658138865542, 295.033860938935, 
                                                                            279.860510987923, 287.113830441395, 292.336885483292, 295.427617827684, 
                                                                            296.489124522031, 283.159172276333, 289.724906645293, 294.427808015129, 
                                                                            297.210195251876, 297.867314971958, 286.240583859924, 292.116007683677, 
                                                                            296.441330207352, 298.913741001846, 299.228237707093, 289.155028136778, 
                                                                            294.45393405137, 298.341131751941, 300.541140555558, 300.565011378704, 
                                                                            291.874503128062, 296.714557240146, 300.28144772618, 302.159045798791, 
                                                                            301.887508829753, 294.540040781198, 298.920798494012, 302.161045218519, 
                                                                            303.746610391295, 303.16865863817, 297.002258002925, 301.063079242958, 
                                                                            303.996337566329, 305.316610546778, 304.428199332557, 299.453621784959, 
                                                                            303.126532419245, 305.773688783677, 306.814093558989, 305.725942421125, 
                                                                            301.743797063498, 305.119477128631, 307.51825282621, 308.368223690109, 
                                                                            306.989423899606, 303.96503009948, 307.086644645609, 309.231023934863, 
                                                                            309.839531277889, 308.241249581827, 306.160162408121, 309.011574403951, 
                                                                            310.961252883614, 311.369601602145, 309.506729580643, 308.426887740228, 
                                                                            310.877482623753, 312.672303659182, 312.857660322071, 310.782958605156, 
                                                                            310.604363426413, 312.777818393495, 314.369359067844, 314.354012486772, 
                                                                            312.04216204717, 312.820604836917, 314.646362761826, 316.041096473328, 
                                                                            315.824442161872, 313.360922820117, 314.941550061881, 316.537577445107, 
                                                                            317.730752965799, 317.289893135244, 314.660614386922, 317.046117267021, 
                                                                            318.422196041388, 319.41869321157, 318.814670054596, 315.987833205398, 
                                                                            319.130786257924, 320.307329818777, 321.165895667525, 320.335328298579, 
                                                                            317.336577323597, 321.267180055169, 322.196885716577, 322.906147132854, 
                                                                            321.859961329359, 318.764267788713, 323.428019263989, 324.063866488871, 
                                                                            324.663590340129, 323.463559234212, 320.213953701856, 325.542390058005, 
                                                                            325.992688146599, 326.45810065001, 325.065586751149, 321.660899117447, 
                                                                            327.612494527158, 328.024607833831, 328.326200406608, 326.741743269558, 
                                                                            323.168107181518, 329.753210019508, 329.992102934267, 330.144108791535, 
                                                                            328.524180890223, 324.774416883329, 331.960445832517, 332.096921620959, 
                                                                            332.058903026907, 330.305343956299, 326.418244227535, 334.153924760965, 
                                                                            334.177918516122, 334.008763629271, 332.169084105759, 328.137100308157, 
                                                                            336.402972190626, 336.279213651756, 336.089262974607, 334.117835502631, 
                                                                            329.959477107444, 338.77838569808, 338.452536244031, 338.206665764658, 
                                                                            336.179603591298, 331.926030214629, 341.163223992974, 340.780374679595, 
                                                                            340.482151800347, 338.363311432429, 334.064641452607, 343.660563282461, 
                                                                            343.315627885764, 342.824076213282, 340.697297827207, 336.380234917084, 
                                                                            346.294110027236, 345.873697788491, 345.377324992113, 343.288483055499, 
                                                                            338.969752836371, 349.064432866193, 348.687360575361, 348.04097231802, 
                                                                            346.110395760766, 341.932879956766, 351.968852213056, 351.540038029962, 
                                                                            350.974383643959, 349.215135808207, 345.418715372263, 354.960844824833, 
                                                                            354.668568304285, 354.237933979065, 352.891562502461, 349.836170246401, 
                                                                            358.233148049831, 358.133232348689, 357.908926578064, 357.405313778517, 
                                                                            355.759132726086), q_sym = c(1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 
                                                                                                         5, 5, 5, 5, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11, 
                                                                                                         13, 13, 13, 13, 13, 15, 15, 15, 15, 15, 17, 17, 17, 17, 17, 19, 
                                                                                                         19, 19, 19, 19, 21, 21, 21, 21, 21, 23, 23, 23, 23, 23, 25, 25, 
                                                                                                         25, 25, 25, 27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 31, 31, 31, 
                                                                                                         31, 31, 33, 33, 33, 33, 33, 35, 35, 35, 35, 35, 37, 37, 37, 37, 
                                                                                                         37, 39, 39, 39, 39, 39, 41, 41, 41, 41, 41, 43, 43, 43, 43, 43, 
                                                                                                         45, 45, 45, 45, 45, 47, 47, 47, 47, 47, 49, 49, 49, 49, 49, 49, 
                                                                                                         49, 49, 49, 49, 47, 47, 47, 47, 47, 45, 45, 45, 45, 45, 43, 43, 
                                                                                                         43, 43, 43, 41, 41, 41, 41, 41, 39, 39, 39, 39, 39, 37, 37, 37, 
                                                                                                         37, 37, 35, 35, 35, 35, 35, 33, 33, 33, 33, 33, 31, 31, 31, 31, 
                                                                                                         31, 29, 29, 29, 29, 29, 27, 27, 27, 27, 27, 25, 25, 25, 25, 25, 
                                                                                                         23, 23, 23, 23, 23, 21, 21, 21, 21, 21, 19, 19, 19, 19, 19, 17, 
                                                                                                         17, 17, 17, 17, 15, 15, 15, 15, 15, 13, 13, 13, 13, 13, 11, 11, 
                                                                                                         11, 11, 11, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 3, 3, 
                                                                                                         3, 3, 3, 1, 1, 1, 1, 1)), row.names = c(NA, -250L), class = "data.frame")

ggplot(test, aes(x=hue, y = time, height=q_sym, group = time, fill=after_stat(x)))+
  geom_density_ridges_gradient(stat="identity", gradient_lwd = 0,
                               panel_scaling=FALSE)+
  ggplot2::scale_fill_gradientn(colors = scales::hue_pal(l=50)(360))+
  labs(title="Seems right enough")+
  theme_light()

ggplot(test, aes(x=hue, y = time, height=q_sym, group = time, fill=after_stat(x) ))+
  geom_density_ridges_gradient(stat="identity", gradient_lwd = 0,
                               panel_scaling=FALSE)+
  ggplot2::scale_fill_gradientn(colors = scales::hue_pal(l=50)(360))+
  labs(fill="Hue")+
  coord_polar()+
  theme_light()+
  theme(strip.background = element_rect(fill="gray40"))+
  scale_x_continuous(breaks = c(0, 60, 120, 180, 240, 300, 360),
                     limits = c(0,360))+
  labs(title="Fill not mapping to x nearly as well")