Home > Software engineering >  continuous legend in ggplot2 and smooth contour lines
continuous legend in ggplot2 and smooth contour lines

Time:02-03

I am handling this data:

dput(data.mi10)
structure(list(Days = c(0, 40.5555555555556, 81.1111111111111, 
121.666666666667, 162.222222222222, 202.777777777778, 243.333333333333, 
283.888888888889, 324.444444444444, 365, 0, 40.5555555555556, 
81.1111111111111, 121.666666666667, 162.222222222222, 202.777777777778, 
243.333333333333, 283.888888888889, 324.444444444444, 365, 0, 
40.5555555555556, 81.1111111111111, 121.666666666667, 162.222222222222, 
202.777777777778, 243.333333333333, 283.888888888889, 324.444444444444, 
365, 0, 40.5555555555556, 81.1111111111111, 121.666666666667, 
162.222222222222, 202.777777777778, 243.333333333333, 283.888888888889, 
324.444444444444, 365, 0, 40.5555555555556, 81.1111111111111, 
121.666666666667, 162.222222222222, 202.777777777778, 243.333333333333, 
283.888888888889, 324.444444444444, 365, 0, 40.5555555555556, 
81.1111111111111, 121.666666666667, 162.222222222222, 202.777777777778, 
243.333333333333, 283.888888888889, 324.444444444444, 365, 0, 
40.5555555555556, 81.1111111111111, 121.666666666667, 162.222222222222, 
202.777777777778, 243.333333333333, 283.888888888889, 324.444444444444, 
365, 0, 40.5555555555556, 81.1111111111111, 121.666666666667, 
162.222222222222, 202.777777777778, 243.333333333333, 283.888888888889, 
324.444444444444, 365), Shannon = c(3.60331931049121, 3.60331931049121, 
3.60331931049121, 3.60331931049121, 3.60331931049121, 3.60331931049121, 
3.60331931049121, 3.60331931049121, 3.60331931049121, 3.60331931049121, 
3.66591200730046, 3.66591200730046, 3.66591200730046, 3.66591200730046, 
3.66591200730046, 3.66591200730046, 3.66591200730046, 3.66591200730046, 
3.66591200730046, 3.66591200730046, 3.72850470410971, 3.72850470410971, 
3.72850470410971, 3.72850470410971, 3.72850470410971, 3.72850470410971, 
3.72850470410971, 3.72850470410971, 3.72850470410971, 3.72850470410971, 
3.79109740091896, 3.79109740091896, 3.79109740091896, 3.79109740091896, 
3.79109740091896, 3.79109740091896, 3.79109740091896, 3.79109740091896, 
3.79109740091896, 3.79109740091896, 3.85369009772822, 3.85369009772822, 
3.85369009772822, 3.85369009772822, 3.85369009772822, 3.85369009772822, 
3.85369009772822, 3.85369009772822, 3.85369009772822, 3.85369009772822, 
3.91628279453747, 3.91628279453747, 3.91628279453747, 3.91628279453747, 
3.91628279453747, 3.91628279453747, 3.91628279453747, 3.91628279453747, 
3.91628279453747, 3.91628279453747, 3.97887549134672, 3.97887549134672, 
3.97887549134672, 3.97887549134672, 3.97887549134672, 3.97887549134672, 
3.97887549134672, 3.97887549134672, 3.97887549134672, 3.97887549134672, 
4.04146818815597, 4.04146818815597, 4.04146818815597, 4.04146818815597, 
4.04146818815597, 4.04146818815597, 4.04146818815597, 4.04146818815597, 
4.04146818815597, 4.04146818815597), yvar = c(114.934465595745, 
120.509986463898, 124.317797972031, 126.402025856954, 127.008896350553, 
126.384635684716, 124.77547009133, 122.427625802282, 119.58732904946, 
116.500806064751, 116.682912354883, 120.8715144389, 123.666326174677, 
125.102139495472, 125.373097031997, 124.673341414962, 123.197015275081, 
121.138261243065, 118.691221949625, 116.050040025473, 118.43135911402, 
121.233042413902, 123.014854377323, 123.80225313399, 123.73729771344, 
122.962047145209, 121.618560458832, 119.848896683847, 117.795114849789, 
115.599273986195, 120.179805873158, 121.594570388904, 122.363382579969, 
122.502366772509, 122.101498394884, 121.250752875455, 120.040105642583, 
118.559532124629, 116.899007749953, 115.148507946917, 121.928252632295, 
121.956098363906, 121.711910782615, 121.202480411027, 120.465699076327, 
119.539458605701, 118.461650826334, 117.270167565411, 116.002900650118, 
114.697741907639, 123.676699391433, 122.317626338908, 121.060438985261, 
119.902594049545, 118.829899757771, 117.828164335948, 116.883196010085, 
115.980803006193, 115.106793550282, 114.246975868361, 125.42514615057, 
122.67915431391, 120.408967187907, 118.602707688064, 117.194100439215, 
116.116870066194, 115.304741193836, 114.691438446976, 114.210686450446, 
113.796209829083, 127.173592909708, 123.040682288913, 119.757495390553, 
117.302821326582, 115.558301120658, 114.40557579644, 113.726286377587, 
113.402073887758, 113.314579350611, 113.345443789805), SE = c(21.4362667151359, 
21.0345708309836, 21.3475757385305, 21.7556596991909, 21.9779072828534, 
21.9724446720235, 21.8279234351174, 21.6904629552495, 21.7073911689015, 
21.9790245548464, 21.2552064028085, 20.9564531454237, 21.2960738830559, 
21.7004183315351, 21.916973585918, 21.9149316231417, 21.7841118446432, 
21.6666239134831, 21.703739011616, 21.9904783267337, 21.1089727557892, 
20.8963128020167, 21.265009402966, 21.6721139163484, 21.8857916596887, 
21.8844949641849, 21.760571242426, 21.6550342548571, 21.7064014904793, 
22.006901071247, 20.9982933929979, 20.8543053323248, 21.2544719092067, 
21.6708520013793, 21.8844886792023, 21.8812476846385, 21.7573674257631, 
21.6557136461753, 21.7153762828176, 22.0282816748305, 20.9237325319195, 
20.830540438468, 21.2644919178816, 21.6966373051475, 21.9130699743852, 
21.9052018763622, 21.7745093714834, 21.6686609334003, 21.7306555676477, 
22.0546057184615, 20.8856770000711, 20.8250805740141, 21.2950404100913, 
21.7493736291301, 21.971418921586, 21.9562685094819, 21.8119491106176, 
21.6938541514327, 21.7522260596943, 22.0858555259451, 20.8843263590284, 
20.8379401275756, 21.3460292505318, 21.828865641412, 22.0592993072467, 
22.0342590772953, 21.8695823966922, 21.7312507098887, 21.7800690669533, 
22.1220102227175, 20.919687718143, 20.8690852338426, 21.4173124508437, 
21.9348224552894, 22.1763600529689, 22.138889039852, 21.9472501460535, 
21.7807877512229, 21.8141605712296, 22.1630458046765), df = c(55.7580784609115, 
53.1025144264156, 56.4715424609894, 60.4243872394631, 62.4216024962759, 
62.1249511584107, 60.4564452362233, 58.8586260916748, 58.7323640904346, 
61.0285366940053, 54.2458637174661, 52.4746406328148, 56.0600460729621, 
59.9739619047893, 61.9201646743347, 61.6550196668376, 60.1062851175636, 
58.6759983639765, 58.7096991532957, 61.1137432346686, 53.0346801587907, 
52.0030909373142, 55.8410732368459, 59.7862852996615, 61.7110528111446, 
61.4500809622447, 59.9527485240369, 58.6112535776037, 58.7481319789696, 
61.2482315115197, 52.1259009408581, 51.6879942030184, 55.8149635354336, 
59.8620749371686, 61.7952542270322, 61.5109889037592, 59.9962983540665, 
58.6645474511379, 58.8476814165452, 61.4319488035488, 51.5200690668976, 
51.5294740627513, 55.9821112510104, 60.2019412406328, 62.1734610155593, 
61.8383662376547, 60.237326497921, 58.8360301042944, 59.0083652403641, 
61.6648356597215, 51.2173278756729, 51.5276904410103, 56.3429707118347, 
60.8063604785635, 62.8460319933655, 62.4325815026985, 60.6761449328395, 
59.1258423102892, 59.2301982940795, 61.9468237497981, 51.2177168154089, 
51.6828607877174, 56.8980245998012, 61.6755568112587, 63.8128316457284, 
63.2936340368824, 61.3129388182665, 59.534103496183, 59.5131895009135, 
62.2778331572491, 51.5213218061654, 51.995260569728, 57.647715042043, 
62.8092944272979, 65.0729483231945, 64.4209483231072, 62.1476817968054, 
60.0608915151714, 59.8573377495659, 62.6577691285801), LCL = c(71.9883513670265, 
78.3218814764488, 81.5613435669849, 82.8905127316036, 83.0815874727086, 
82.4641018253524, 81.1199015699806, 79.0229283257209, 76.1467978246253, 
72.5514458102648, 74.0732122631882, 78.8283666021421, 81.0061721571954, 
81.6944523532065, 81.560567421742, 80.8611409147582, 79.6238885503582, 
77.7784373256389, 75.2576469076448, 72.079016030114, 76.0927678369958, 
79.3015824157915, 80.4132460615433, 80.4483833743656, 79.9841484197279, 
79.20777620066, 78.0902315818907, 76.5112590005365, 74.3568092295217, 
71.5973622682656, 78.0460698517029, 79.7413317311806, 79.782443889827, 
79.1521543757799, 78.3521454308975, 77.5038438457274, 76.5188357397666, 
75.2213641305991, 73.4442868012774, 71.1064993472294, 79.9323998280724, 
80.1474740131836, 79.1137143254093, 77.8057381367404, 76.6645272184475, 
75.7493071606306, 74.9096763786992, 73.9087487374991, 72.520088273808, 
70.6064461993771, 81.7512726466059, 80.5199256297373, 78.4070765230225, 
76.409232403511, 74.9214173108502, 73.944257392648, 73.2627995040038, 
72.573434172318, 71.5842289451783, 70.0972258571207, 83.502438347575, 
80.8586600783282, 77.6626205540342, 74.9628604933146, 73.1230945524385, 
72.0889142228647, 71.578322072254, 71.2154671991298, 70.6367307672983, 
69.5788654413683, 85.1858829505713, 81.1637082216293, 76.8805078486946, 
73.4669715525888, 71.2699929987449, 70.1836258115174, 69.8564321663107, 
69.834920064731, 69.6776223507488, 69.0513961476194), UCL = c(157.880579824464, 
162.698091451346, 167.074252377077, 169.913538982304, 170.936205228397, 
170.305169544079, 168.43103861268, 165.832323278844, 163.027860274295, 
160.450166319237, 159.292612446577, 162.914662275657, 166.326480192159, 
168.509826637738, 169.185626642251, 168.485541915166, 166.770141999804, 
164.49808516049, 162.124796991605, 160.021064020832, 160.769950391044, 
163.164502412012, 165.616462693103, 167.156122893615, 167.490447007152, 
166.716318089757, 165.146889335774, 163.186534367157, 161.233420470056, 
159.601185704124, 162.313541894613, 163.447809046627, 164.944321270111, 
165.852579169238, 165.85085135887, 164.997661905183, 163.5613755454, 
161.897700118659, 160.353728698629, 159.190516546604, 163.924105436518, 
163.764722714629, 164.310107239821, 164.599222685314, 164.266870934207, 
163.329610050772, 162.013625273969, 160.631586393323, 159.485713026427, 
158.7890376159, 165.60212613626, 164.115327048079, 163.7138014475, 
163.39595569558, 162.738382204692, 161.712071279247, 160.503592516167, 
159.388171840069, 158.629358155385, 158.396725879601, 167.347853953566, 
164.499648549493, 163.15531382178, 162.242554882813, 161.265106325991, 
160.144825909523, 159.031160315419, 158.167409694821, 157.784642133594, 
158.013554216797, 169.161302868845, 164.917656356196, 162.634482932412, 
161.138671100576, 159.846609242572, 158.627525781363, 157.596140588864, 
156.969227710784, 156.951536350472, 157.63949143199), tvar = c(0, 
40.5555555555556, 81.1111111111111, 121.666666666667, 162.222222222222, 
202.777777777778, 243.333333333333, 283.888888888889, 324.444444444444, 
365, 0, 40.5555555555556, 81.1111111111111, 121.666666666667, 
162.222222222222, 202.777777777778, 243.333333333333, 283.888888888889, 
324.444444444444, 365, 0, 40.5555555555556, 81.1111111111111, 
121.666666666667, 162.222222222222, 202.777777777778, 243.333333333333, 
283.888888888889, 324.444444444444, 365, 0, 40.5555555555556, 
81.1111111111111, 121.666666666667, 162.222222222222, 202.777777777778, 
243.333333333333, 283.888888888889, 324.444444444444, 365, 0, 
40.5555555555556, 81.1111111111111, 121.666666666667, 162.222222222222, 
202.777777777778, 243.333333333333, 283.888888888889, 324.444444444444, 
365, 0, 40.5555555555556, 81.1111111111111, 121.666666666667, 
162.222222222222, 202.777777777778, 243.333333333333, 283.888888888889, 
324.444444444444, 365, 0, 40.5555555555556, 81.1111111111111, 
121.666666666667, 162.222222222222, 202.777777777778, 243.333333333333, 
283.888888888889, 324.444444444444, 365, 0, 40.5555555555556, 
81.1111111111111, 121.666666666667, 162.222222222222, 202.777777777778, 
243.333333333333, 283.888888888889, 324.444444444444, 365), xvar = c(3.60331931049121, 
3.60331931049121, 3.60331931049121, 3.60331931049121, 3.60331931049121, 
3.60331931049121, 3.60331931049121, 3.60331931049121, 3.60331931049121, 
3.60331931049121, 3.66591200730046, 3.66591200730046, 3.66591200730046, 
3.66591200730046, 3.66591200730046, 3.66591200730046, 3.66591200730046, 
3.66591200730046, 3.66591200730046, 3.66591200730046, 3.72850470410971, 
3.72850470410971, 3.72850470410971, 3.72850470410971, 3.72850470410971, 
3.72850470410971, 3.72850470410971, 3.72850470410971, 3.72850470410971, 
3.72850470410971, 3.79109740091896, 3.79109740091896, 3.79109740091896, 
3.79109740091896, 3.79109740091896, 3.79109740091896, 3.79109740091896, 
3.79109740091896, 3.79109740091896, 3.79109740091896, 3.85369009772822, 
3.85369009772822, 3.85369009772822, 3.85369009772822, 3.85369009772822, 
3.85369009772822, 3.85369009772822, 3.85369009772822, 3.85369009772822, 
3.85369009772822, 3.91628279453747, 3.91628279453747, 3.91628279453747, 
3.91628279453747, 3.91628279453747, 3.91628279453747, 3.91628279453747, 
3.91628279453747, 3.91628279453747, 3.91628279453747, 3.97887549134672, 
3.97887549134672, 3.97887549134672, 3.97887549134672, 3.97887549134672, 
3.97887549134672, 3.97887549134672, 3.97887549134672, 3.97887549134672, 
3.97887549134672, 4.04146818815597, 4.04146818815597, 4.04146818815597, 
4.04146818815597, 4.04146818815597, 4.04146818815597, 4.04146818815597, 
4.04146818815597, 4.04146818815597, 4.04146818815597)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", 
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", 
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 
"80"))

where I am trying to plot some data using this script:

scale.breaks = scales::fullseq(range(data.mi10$yvar), size = 0.01)

ggplot(data= data.mi10 , aes(x= Days, y= Shannon, z= yvar))  
  
  geom_contour_filled(breaks = scale.breaks)  
  
  geom_contour(data= data.mi10,aes(x = Days, y = Shannon, z = yvar), color= 'black', bins= 15, alpha=0.25) 

  xlab('Time (days)')  

  ylab('Shannon Index')  
  
  theme_bw()   
  
  theme(panel.grid = element_blank(), 
        panel.border = element_blank())  

  theme(legend.position="none")

But I have some problems:

1st: I have tried to get a legend but making a continuos scale like enter image description here

This is certainly a big improvement from using the same plotting code with your unsmoothed data:

enter image description here

  • Related