How do I visualise multiple routes using Leaflet in R - r

I like Leaflet package it is interactive, with good basemap and high zooming power. Thus, I want to use leaflet package to produce a travel route. I know map and ggmap packages can be used to produce a map such as this:Example of map to produce
In ggmap, there is gcintermediate function that connects draws multiple lines simultaneously, but I am not sure if similar function is in leaflet package. I tried using leaflet package, but could only use it to connect two points. So, my question is that can leaflet package be used to visualize multiple routes simultaneously?
Below is my attempt:
library(leaflet)
p1=leaflet()%>%
addTiles() %>%
mydf2=data.frame(inter)
#mydf2 is below
lon lat
1 -157.8583 21.3069444
2 -158.1350 20.7322775
3 -158.4096 20.1571716
4 -158.6822 19.5816431
5 -158.9528 19.0057081
6 -159.2216 18.4293823
7 -159.4885 17.8526808
8 -159.7538 17.2756187
9 -160.0173 16.6982106
10 -160.2793 16.1204708
11 -160.5398 15.5424133
12 -160.7988 14.9640518
13 -161.0564 14.3853998
14 -161.3127 13.8064705
15 -161.5677 13.2272769
16 -161.8215 12.6478316
17 -162.0742 12.0681472
18 -162.3258 11.4882361
19 -162.5763 10.9081102
20 -162.8259 10.3277815
21 -163.0745 9.7472619
22 -163.3223 9.1665628
23 -163.5692 8.5856958
24 -163.8154 8.0046721
25 -164.0609 7.4235030
26 -164.3058 6.8421994
27 -164.5501 6.2607724
28 -164.7938 5.6792327
29 -165.0370 5.0975911
30 -165.2798 4.5158583
31 -165.5222 3.9340449
32 -165.7643 3.3521614
33 -166.0060 2.7702183
34 -166.2476 2.1882260
35 -166.4889 1.6061948
36 -166.7301 1.0241352
37 -166.9712 0.4420575
38 -167.2123 -0.1400281
39 -167.4534 -0.7221112
40 -167.6946 -1.3041815
41 -167.9359 -1.8862287
42 -168.1773 -2.4682425
43 -168.4189 -3.0502124
44 -168.6608 -3.6321282
45 -168.9030 -4.2139792
46 -169.1456 -4.7957552
47 -169.3886 -5.3774454
48 -169.6321 -5.9590393
49 -169.8761 -6.5405261
50 -170.1206 -7.1218951
51 -170.3658 -7.7031354
52 -170.6116 -8.2842359
53 -170.8582 -8.8651856
54 -171.1055 -9.4459733
55 -171.3537 -10.0265874
56 -171.6028 -10.6070167
57 -171.8528 -11.1872492
58 -172.1038 -11.7672733
59 -172.3559 -12.3470769
60 -172.6091 -12.9266478
61 -172.8635 -13.5059736
62 -173.1191 -14.0850416
63 -173.3760 -14.6638391
64 -173.6343 -15.2423530
65 -173.8940 -15.8205699
66 -174.1552 -16.3984763
67 -174.4179 -16.9760583
68 -174.6823 -17.5533017
69 -174.9484 -18.1301922
70 -175.2162 -18.7067148
71 -175.4858 -19.2828545
72 -175.7574 -19.8585959
73 -176.0309 -20.4339231
74 -176.3065 -21.0088199
75 -176.5842 -21.5832696
76 -176.8641 -22.1572552
77 -177.1464 -22.7307591
78 -177.4310 -23.3037635
79 -177.7180 -23.8762498
80 -178.0076 -24.4481991
81 -178.2999 -25.0195918
82 -178.5948 -25.5904080
83 -178.8927 -26.1606268
84 -179.1934 -26.7302271
85 -179.4971 -27.2991869
86 -179.8040 -27.8674837
87 179.8859 -28.4350940
88 179.5724 -29.0019940
89 179.2555 -29.5681588
90 178.9350 -30.1335627
91 178.6108 -30.6981794
92 178.2828 -31.2619815
93 177.9509 -31.8249406
94 177.6149 -32.3870276
95 177.2747 -32.9482123
96 176.9302 -33.5084632
97 176.5811 -34.0677479
98 176.2275 -34.6260330
99 175.8690 -35.1832834
100 175.5056 -35.7394632
101 175.1371 -36.2945349
102 174.7633 -36.8484597
addPolylines(data = mydf2, lng = ~lon, lat = ~lat)
This is what I got from my attempt.
my attempt

The example you provided uses Sources and Targets.
You need some sort of 'Source' as lat1 and then 'Target' as lat2.
Then the same for longitude 'Source' as lon1 and 'Target' as lon2.
c(lat1, lon1)
c(lat2, lon2)
There is an example here: http://kateto.net/network-visualization
The flight path map is the last graph on the website, so scroll to the bottom immediately and then work your way up.
Re-frame your data set into Sources and Targets and try and replot.
Here is a similar question with an answer, which explains how to pass gcintermediate into Leaflet:
Adding Curved Flight path using R's Leaflet Package

Related

How to visualize a layer onto a map with R?

I am trying to produce a map which would visualize the lu_effic on my map. With the current codes I can visualize the map and the lu_effic category, but they are not layered on each other. I would like to fill the map with lu_effic colors. I tried with aes to add fill in geom_sf(data = shp_dt) but didn't work. The geom_polygon() also showed error, as the shape file data is a sf format. It has geometry column instead of longitude and latitude columns separately. Hence, failed to run aes (x= long, y=lat). I need to fill the map with lu_effic colors and other necessary visualization elements. Would anybody kindly assist me to solve this? Thanks in advance.
The data frame urb_dtf is here: https://docs.google.com/spreadsheets/d/1mZOkMTLx830oPaqtg9TVpg0OTP5fGzcR/edit?usp=sharing&ouid=114933701557583416369&rtpof=true&sd=true
urb_dtf
dtafil.GCPNT_LAT dtafil.GCPNT_LON lu_effic dtafil.UC_NM_MN
1 26.23825 88.40822 2.49722588 Atwari
2 26.03490 88.44599 1.30779927 Thakurgaon
3 26.33849 88.55293 4.41597618 Panchagarh
4 25.86100 88.36024 0.60699713 Pirganj
5 24.94617 88.21594 0.15688455 Asia
6 26.39944 88.96166 1.70447128 Asia
7 25.85807 88.65385 4.35869046 N/A
8 24.61404 88.23778 0.69757530 Chapainawabganj
9 25.62459 88.62559 0.70376589 Dinajpur
10 26.34469 89.01786 -0.78882643 Patgram
11 25.93154 88.85591 1.17247204 Nilphamari
12 25.79222 88.89938 0.53713622 Saidpur
13 24.44356 88.34488 21.83406438 Godagari
14 25.67936 89.05180 0.94306324 Bodorganj
15 24.41426 88.61983 0.67571401 Rajshahi
16 25.10153 89.02355 9.89191877 Joypurhat
17 24.80587 88.95890 1.26512380 Naogaon
18 24.37298 88.76614 0.43754723 Baneshwar
19 23.78359 88.63357 0.74975530 Meherpur
20 24.41677 88.99007 0.82625124 নাটোর
21 25.69513 89.60984 1.43875426 Ulipur
22 24.94282 89.37863 1.15731210 Bogura
23 25.32976 89.53690 1.28528943 Gaibandha
24 23.63560 88.83938 0.88394331 Chuadanga
25 24.08493 89.09479 0.36944300 Ishurdi
26 23.75310 88.94598 0.52944470 Alamdanga
27 23.90058 89.10053 0.68007198 Barkhada
28 24.65859 89.44197 1.51617122 Bogra
29 24.01567 89.26079 0.71299373 Pabna
30 23.86184 89.15752 0.82472321 Alampur
31 24.41254 89.64289 0.95057900 Sirajganj
32 23.40571 89.01174 0.69460150 Kotchadpur
33 23.04288 88.89597 0.75806861 Asia
34 23.54037 89.17593 0.61603886 Jhenaidah
35 23.40988 89.13477 1.10069949 কালীগঞà§\u008dজ
36 24.96816 89.97172 0.46869564 Jamalpur
37 23.16668 89.20843 0.52640165 Jessore
38 23.48845 89.42102 1.09687780 Magura
39 24.32668 89.94591 0.82200687 Tangail
40 24.76605 90.25557 0.56361005 Muktagacha
41 22.84015 89.55533 -1.26805716 Khulna
42 23.58577 89.83287 1.35248758 Faridpur
43 24.74904 90.38985 0.17224254 Mymensingh
44 23.84481 90.00978 0.95263604 Manikgonj
45 24.53342 90.34734 0.46884662 Trishal
46 23.80705 90.40166 0.17146435 Dhaka
47 23.91039 90.06940 0.25197463 Jaigir
48 24.54544 90.42188 0.26382334 Rampur
49 24.42903 90.38683 0.77181766 Bhaluka
50 24.49606 90.49881 0.32848142 Gafargaon
51 23.01082 89.82701 -2.48743996 Gopalganj
52 22.66161 89.77432 -0.26417887 Bagerhat
53 24.87865 90.72984 0.02135679 Netrokona
54 23.10274 90.17751 -2.59794615 Madaripur
55 24.41027 90.81461 0.29627310 Kishoreganj
56 24.57433 90.89773 0.40133470 Tarail
57 23.21486 90.33779 1.49758668 Shariatpur
58 23.55846 90.60338 0.84538204 N/A
59 24.05720 90.98408 0.15528478 Brahmanbaria
60 23.88657 90.73217 0.33951829 N/A
61 23.55013 90.66072 1.08523717 Gazaria
62 23.93776 90.86446 0.26901075 N/A
63 23.69415 90.83184 0.35016879 Homna
64 22.70696 90.35247 -0.89130028 Barisal
65 23.14673 90.80557 0.57463630 Chandpur
66 25.06408 91.40035 0.13625843 Sunamganj District
67 23.46517 90.78352 0.75471736 N/A
68 23.80230 90.97777 0.18687093 Nabinagar
69 23.89096 91.03594 0.05931816 Shibpur
70 24.06237 91.17975 0.09915729 Shahbazpur Town
71 22.99137 90.86482 0.19705905 Lakshmipur
72 22.67565 90.65828 1.14616972 Bhola
73 24.38139 91.41250 0.16456846 Habiganj
74 24.87859 91.90338 0.11481848 Sylhet
75 24.49782 91.78539 0.13333066 Moulvibazar
76 24.33299 91.73140 0.20055411 Sreemangal
77 24.82160 92.16267 0.18369562 Beanibazar
78 23.00552 91.40018 0.53240186 Feni
79 22.43880 91.82426 0.05366313 Chattogram
80 23.10985 91.98522 0.13909249 Khagrachhari
81 22.52012 91.92949 0.05425749 Raujan
82 21.75023 92.05997 0.11659127 Chakaria
83 22.20431 92.21605 0.08462762 Bandarban
84 21.43620 92.02313 0.07814110 Ramu
85 20.86985 92.29864 0.17803279 Teknaf
The shape file link: https://biogeo.ucdavis.edu/data/diva/adm/BGD_adm.zip
The Zip file is available at DIVA-GIS (a popular platform to download shape file). DIVA-GIS-> free spatial data -> country level data
The codes I ran are as follows:
library(sf)
library(ggplot2)
library(tidyverse)
urb_dt= read.csv("GHS_STAT_V1_2_BGD.csv")
urb_dtf= data.frame(dtafil$GCPNT_LAT, dtafil$GCPNT_LON, lu_effic, dtafil$UC_NM_MN)
dput(urb_dtf)
shp_dt= st_read("bgd_admbnda_adm3_bbs_20201113.shp")
str(shp_dt)
dput(shp_dt)
ggplot()+ geom_sf(data = shp_dt)+
geom_polygon(data = urb_dtf, aes(dtafil.GCPNT_LON, dtafil.GCPNT_LAT, group = lu_effic, fill = lu_effic))
Finally, I get the following image. But I would like to fill the map with the colors of lu_effic.
1

How to isolate certain letters inside a string in R?

I need to find an index for all the values that present Q2 or Q4.
So basically skipping the first element that is a title. I need to save: 3,5,7,9..... till the end of the object.
How can I do this analyzing a string?
This is the code:
line = c("ISSUE_CUR", "1993-Q1", "1993-Q2", "1993-Q3", "1993-Q4", "1994-Q1",
"1994-Q2", "1994-Q3", "1994-Q4", "1995-Q1", "1995-Q2", "1995-Q3",
"1995-Q4", "1996-Q1", "1996-Q2", "1996-Q3", "1996-Q4", "1997-Q1",
"1997-Q2", "1997-Q3", "1997-Q4", "1998-Q1", "1998-Q2", "1998-Q3",
"1998-Q4", "1999-Q1", "1999-Q2", "1999-Q3", "1999-Q4", "2000-Q1",
"2000-Q2", "2000-Q3", "2000-Q4", "2001-Q1", "2001-Q2", "2001-Q3",
"2001-Q4", "2002-Q1", "2002-Q2", "2002-Q3", "2002-Q4", "2003-Q1",
"2003-Q2", "2003-Q3", "2003-Q4", "2004-Q1", "2004-Q2", "2004-Q3",
"2004-Q4", "2005-Q1", "2005-Q2", "2005-Q3", "2005-Q4", "2006-Q1",
"2006-Q2", "2006-Q3", "2006-Q4", "2007-Q1", "2007-Q2", "2007-Q3",
"2007-Q4", "2008-Q1", "2008-Q2", "2008-Q3", "2008-Q4", "2009-Q1",
"2009-Q2", "2009-Q3", "2009-Q4", "2010-Q1", "2010-Q2", "2010-Q3",
"2010-Q4", "2011-Q1", "2011-Q2", "2011-Q3", "2011-Q4", "2012-Q1",
"2012-Q2", "2012-Q3", "2012-Q4", "2013-Q1", "2013-Q2", "2013-Q3",
"2013-Q4", "2014-Q1", "2014-Q2", "2014-Q3", "2014-Q4", "2015-Q1",
"2015-Q2", "2015-Q3", "2015-Q4", "2016-Q1", "2016-Q2", "2016-Q3",
"2016-Q4", "2017-Q1", "2017-Q2", "2017-Q3", "2017-Q4", "2018-Q1",
"2018-Q2", "2018-Q3", "2018-Q4", "2019-Q1", "2019-Q2", "2019-Q3",
"2019-Q4")
We can use grep to return the index by matching 'Q2' or (|) 'Q4' at the end ($) of the string
grep("(Q2|Q4)$", line)
#[1] 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61
#[31] 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109
Or the 'Q' can be placed outside as it is common
grep("Q(2|4)$", line)
Or another option is endsWith
which(endsWith(line, "Q2")|endsWith(line, "Q4"))
You can use the grep() function.
grep("[24]$", line, value = T)
The "[24]$" is a regular expression pattern. For a good tutorial on it, you may check here.
https://www.regular-expressions.info/rlanguage.html

Calculate mean value for each row with interval

i need to calculate the mean value for each row (mean of interval). Here is a basic example (maybe anyone has even better idea to do it):
M_1_mb <- (15 : -15)#creating a vector value --> small
M_31 <- cut(M_31_mb,128)# getting 128 groups from the small vector
#M_1_mb <- (1500 : -1500)#creating a vector value
#M_1 <- cut(M_1_mb,128)# getting 128 groups from the vector
I do need to get the mean value for each row/group out of 128 intervals created in M_1 (actually i do not need even those intervals, i just need the mean of them) and i cannot figure out how to do it...
I had a look at the cut2 function from Hmisc library but unfortunatelly there is no option to set up number of intervals into which vector is to be cut (-> but there is an option to get the mean value of created intervals: levels.mean...)
I would appreciate any help! Thanks!
Additional Info:
cut2 function is working well for bigger vectors (M_1_mb), however when my vector is small (M_31_mb), then i am getting a Warning message:
Warning message:
In min(xx[xx > upper]) : no non-missing arguments to min; returning Inf
and only 31 groups are created:
M_31_mb <- (15 : -15) # smaller vector
M_31 <- table(cut2(M_31_mb,g=128,levels.mean = TRUE))
whereas
g = number of quantile groups
like this?
aggregate(M_1_mb,by=list(M_1),mean)
EDIT: Result
Group.1 x
1 (-1.5e+03,-1.48e+03] -1488.5
2 (-1.48e+03,-1.45e+03] -1465.0
3 (-1.45e+03,-1.43e+03] -1441.5
4 (-1.43e+03,-1.41e+03] -1418.0
5 (-1.41e+03,-1.38e+03] -1394.5
6 (-1.38e+03,-1.36e+03] -1371.0
7 (-1.36e+03,-1.34e+03] -1347.5
8 (-1.34e+03,-1.31e+03] -1324.0
9 (-1.31e+03,-1.29e+03] -1301.0
10 (-1.29e+03,-1.27e+03] -1277.5
11 (-1.27e+03,-1.24e+03] -1254.0
12 (-1.24e+03,-1.22e+03] -1230.5
13 (-1.22e+03,-1.2e+03] -1207.0
14 (-1.2e+03,-1.17e+03] -1183.5
15 (-1.17e+03,-1.15e+03] -1160.0
16 (-1.15e+03,-1.12e+03] -1136.5
17 (-1.12e+03,-1.1e+03] -1113.0
18 (-1.1e+03,-1.08e+03] -1090.0
19 (-1.08e+03,-1.05e+03] -1066.5
20 (-1.05e+03,-1.03e+03] -1043.0
21 (-1.03e+03,-1.01e+03] -1019.5
22 (-1.01e+03,-984] -996.0
23 (-984,-961] -972.5
24 (-961,-938] -949.0
25 (-938,-914] -926.0
26 (-914,-891] -902.5
27 (-891,-867] -879.0
28 (-867,-844] -855.5
29 (-844,-820] -832.0
30 (-820,-797] -808.5
31 (-797,-773] -785.0
32 (-773,-750] -761.5
33 (-750,-727] -738.0
34 (-727,-703] -715.0
35 (-703,-680] -691.5
36 (-680,-656] -668.0
37 (-656,-633] -644.5
38 (-633,-609] -621.0
39 (-609,-586] -597.5
40 (-586,-562] -574.0
41 (-562,-539] -551.0
42 (-539,-516] -527.5
43 (-516,-492] -504.0
44 (-492,-469] -480.5
45 (-469,-445] -457.0
46 (-445,-422] -433.5
47 (-422,-398] -410.0
48 (-398,-375] -386.5
49 (-375,-352] -363.0
50 (-352,-328] -340.0
51 (-328,-305] -316.5
52 (-305,-281] -293.0
53 (-281,-258] -269.5
54 (-258,-234] -246.0
55 (-234,-211] -222.5
56 (-211,-188] -199.0
57 (-188,-164] -176.0
58 (-164,-141] -152.5
59 (-141,-117] -129.0
60 (-117,-93.8] -105.5
61 (-93.8,-70.3] -82.0
62 (-70.3,-46.9] -58.5
63 (-46.9,-23.4] -35.0
64 (-23.4,0] -11.5
65 (0,23.4] 12.0
66 (23.4,46.9] 35.0
67 (46.9,70.3] 58.5
68 (70.3,93.8] 82.0
69 (93.8,117] 105.5
70 (117,141] 129.0
71 (141,164] 152.5
72 (164,188] 176.0
73 (188,211] 199.0
74 (211,234] 222.5
75 (234,258] 246.0
76 (258,281] 269.5
77 (281,305] 293.0
78 (305,328] 316.5
79 (328,352] 340.0
80 (352,375] 363.5
81 (375,398] 387.0
82 (398,422] 410.0
83 (422,445] 433.5
84 (445,469] 457.0
85 (469,492] 480.5
86 (492,516] 504.0
87 (516,539] 527.5
88 (539,562] 551.0
89 (562,586] 574.0
90 (586,609] 597.5
91 (609,633] 621.0
92 (633,656] 644.5
93 (656,680] 668.0
94 (680,703] 691.5
95 (703,727] 715.0
96 (727,750] 738.5
97 (750,773] 762.0
98 (773,797] 785.0
99 (797,820] 808.5
100 (820,844] 832.0
101 (844,867] 855.5
102 (867,891] 879.0
103 (891,914] 902.5
104 (914,938] 926.0
105 (938,961] 949.0
106 (961,984] 972.5
107 (984,1.01e+03] 996.0
108 (1.01e+03,1.03e+03] 1019.5
109 (1.03e+03,1.05e+03] 1043.0
110 (1.05e+03,1.08e+03] 1066.5
111 (1.08e+03,1.1e+03] 1090.0
112 (1.1e+03,1.12e+03] 1113.5
113 (1.12e+03,1.15e+03] 1137.0
114 (1.15e+03,1.17e+03] 1160.0
115 (1.17e+03,1.2e+03] 1183.5
116 (1.2e+03,1.22e+03] 1207.0
117 (1.22e+03,1.24e+03] 1230.5
118 (1.24e+03,1.27e+03] 1254.0
119 (1.27e+03,1.29e+03] 1277.5
120 (1.29e+03,1.31e+03] 1301.0
121 (1.31e+03,1.34e+03] 1324.0
122 (1.34e+03,1.36e+03] 1347.5
123 (1.36e+03,1.38e+03] 1371.0
124 (1.38e+03,1.41e+03] 1394.5
125 (1.41e+03,1.43e+03] 1418.0
126 (1.43e+03,1.45e+03] 1441.5
127 (1.45e+03,1.48e+03] 1465.0
128 (1.48e+03,1.5e+03] 1488.5

Saving an output from R into excel format?

After running the predict function for glm i get an output in the below format:
1 2 3 4 5 6 7 8 9 10 11 12
3.954947e-01 8.938624e-01 7.775473e-01 1.294646e-02 3.954947e-01 9.625746e-01 9.144256e-01 4.739872e-01 1.443219e-01 1.180850e-04 2.138978e-01 7.775473e-01
13 14 15 16 17 18 19 20 21 22 23 24
5.425436e-03 2.069844e-04 2.723969e-01 4.739872e-01 9.144256e-01 1.091998e-01 2.070056e-02 5.114936e-01 1.443219e-01 5.922029e-01 7.578099e-02 8.937642e-01
25 26 27 28 29 30 31 32 33 34 35 36
6.069970e-02 6.069970e-02 1.337947e-01 1.090992e-01 4.841467e-02 9.205547e-01 3.954947e-01 3.874915e-05 3.855242e-02 1.344839e-01 6.318574e-04 2.723969e-01
37 38 39 40 41 42 43 44 45 46 47 48
7.400276e-04 8.593199e-01 6.666800e-01 2.069844e-04 8.161623e-01 4.916555e-05 3.060374e-02 3.402079e-01 2.256598e-03 9.363767e-01 6.116082e-01 3.940969e-03
49 50 51 52 53 54 55 56 57 58 59 60
7.336723e-01 2.425257e-02 3.369967e-03 5.624262e-02 1.090992e-01 1.357630e-06 1.278169e-04 3.046189e-01 8.938624e-01 4.535894e-01 5.132348e-01 3.220426e-01
61 62 63 64 65 66 67 68 69 70 71 72
3.366492e-03 1.357630e-06 1.014721e-01 1.294646e-02 9.144256e-01 1.636988e-02 2.070056e-02 1.012835e-01 5.000274e-03 8.165247e-02 1.357630e-06 8.033850e-03
IS there any code by which I can get the complete output vertically or in an excel format? Thank you in advance!
The simplest way is to write a character separated value file using a comma as the delimiter:
[Acknowledge Roland's comment] write.csv(data.frame(predict(yourGLM)), "file.csv")
Excel reads these automatically, especially if you save the file with a .csv extension.
If its just a matter of viewing it vertically first create the data:
# create test data
example(predict.glm)
pred <- predict(budworm.lg)
1) Separate R Window Use View to display it in a separate R window:
View(pred)
2) R Console to display it on the R console vertically:
data.frame(pred)
3) Browser to display it in the browser vertically:
library(R2HTML)
HTMLStart(); HTML(data.frame(pred)); w <- HTMLStop()
browseURL(w)
4) Excel to display it in Excel vertically using w we just computed:
shell(paste("start excel", w))

how do I select points in a dataset above x% contour of a density map?

I have a matrix of data (see below) and I am trying to turn it into a density contour map (Can1 and Can2 variables), maybe with ks or sm packages.
My question is how do I select those points in the dataset which lie above (say) 80% contour of the density map?
Thanks
ID Can1 Can2
4 -12.3235137 -1.0788867664
1 -12.2949912 -0.9321009837
5 -12.2835123 -1.0164225574
2 -12.2571822 -0.7094457036
3 -12.2713779 -0.9908419863
10 -12.9870438 -1.0936405526
6 -12.7167605 -1.4620772026
7 -12.8193776 -1.0911349785
8 -12.9781963 -1.1762698594
9 -12.7983478 -1.3453369581
13 -14.0389948 0.2855210115
11 -14.0015922 0.1467552738
15 -14.0723604 0.0244576488
14 -14.0743560 0.1417245145
12 -13.9898266 0.0005437008
20 -6.5881994 0.5124980991
17 -6.1812321 0.6789584579
16 -6.4704200 0.5942317307
18 -6.6960456 0.5720874622
19 -6.1159788 0.5960966790
22 -2.4794887 2.5493267897
24 -2.4918040 2.7823374576
21 -2.5145044 2.5877290160
23 -2.5048371 2.4916280770
25 -2.5018765 2.8536302559
29 -0.1781852 2.0805229401
26 -0.1581308 2.0151355747
28 -0.2118605 1.9658284615
27 -0.4184119 2.0540218901
30 -0.2994573 2.0205573385
35 2.6254869 1.3858705991
31 2.3146430 1.3510499304
33 2.5346138 1.2524229847
34 2.3741699 1.3842499455
32 2.6008389 1.3446707509
37 3.0920503 1.5807032840
38 3.1559727 1.4924092104
36 3.1593556 1.5803284343
39 3.0801444 1.6031732981
40 3.2562384 1.5810975265
43 4.8414364 2.1539254215
41 4.7938193 2.1613978258
44 4.7919209 2.2151527426
42 4.9830802 2.2374622446
45 4.7629268 2.4217335005
46 5.5631728 0.9986762598
50 5.5250403 1.0549399894
48 5.5833619 1.1368625963
47 5.5660312 1.1881215490
49 5.6224256 1.1634998303
53 5.5536366 0.2513665533
54 5.5276808 0.2685455911
51 5.7103045 0.2193839293
52 5.6014729 0.2353172964
55 5.5959034 0.2447836618
56 5.1542133 0.6070006863
59 5.0043394 0.4518710615
58 5.2314146 0.5656457888
60 5.1318728 0.4771275341
57 5.3599822 0.4918185651
61 7.0235173 -0.2669136870
63 7.0216315 -0.0097862523
64 7.0521253 -0.2457722410
62 7.0150637 -0.1456269078
65 7.0729018 -0.3573952321
69 5.8115406 -1.4652084167
67 5.7624475 -1.4147564126
68 5.8692888 -1.4695783153
70 5.9088094 -1.4927034632
66 5.8400205 -1.4817447808
71 4.8586107 -1.3111515744
73 4.7198564 -1.2891991780
72 4.9153659 -1.4499710448
74 4.7653488 -1.2839433419
75 4.7754971 -1.4655359108
77 3.8955675 -7.0922887151
78 3.8338151 -7.1595858283
80 3.7255063 -7.2147373050
79 3.7367055 -7.3468877516
76 4.0166957 -7.1952570639
Calculate the 80% point. One way: y<- x[x > 0.8 * max(x)] (I'm assuming you wanted 80% of the max level, not the 80th percentile) .
Then plot y .
After a bit of searching I think it can be achieved using the kde2d function from the MASS package.

Resources