How to visualize a layer onto a map with R? - 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

Related

How can I stop geom_point from removing rows in order to create a map

My intention is to plot several locations for which I have the longitude and the latitude onto a map (as simple dots). The locations are distributed across Uganda.
print(locations)
Latitude Longitude
1 0.482980 30.212160
2 0.647717 30.315984
3 0.44735 30.18063
4 0.58416316 30.2066327
5 0.60012 30.19998
6 0.433483 30.20179
7 0.625317 30.224837
8 0.654277 30.251667
9 0.387517 30.197475
10 0.607402 30.292068
11 0.770128 30.403456
12 0.767266 30.414246
13 0.777873 30.389111
14 0.631774 30.290356
15 0.734015 30.279161
16 0.722133 30.277941
17 0.66322994 30.22795225
18 0.66900827 30.21357739
19 0.450372 30.197764
20 0.493699 30.250891
21 0.479716 30.180958
22 0.483242 30.284576
23 0.645044 30.321270
24 0.602389 30.275637
25 0.868827 30.465939
26 0.631194 30.263565
27 0.631576 30.263855
28 0.413701 30.247934
29 0.67135 30.2675
30 0.492360 30.223620
31 0.81481 30.39311
32 0.396665 30.26309
33 0.666170 30.308960
34 0.610067 30.306058
35 0.677144 30.196810
36 0.677144 30.196810
37 0.555555 30.231681
38 0.63874 30.231691
39 0.512953 30.207603
40 0.442291 30.279173
41 0.575658 30.310231
42 0.423129 30.211289
43 0.623838 30.256925
44 0.639643 30.341620
45 0.653550 30.170428
46 0.752630 30.401040
47 0.478544 30.191938
48 0.48114 30.198471
49 0.679820 30.259800
50 0.581293 30.158619
51 0.730410 30.376620
52 0.504059 30.178556
53 0.587441 30.310364
54 0.588072 30.277877
55 0.70893233 30.19008103
56 0.81699 30.41799
57 0.609300 30.271613
58 0.595226 30.315580
59 0.459029 30.277659
60 0.727873 30.216385
61 0.647722 30.217760
62 0.690064 30.193881
63 0.512339 30.140107
64 0.649181 30.302570
65 0.649881 30.303974
66 0.649736 30.302481
67 0.722082 30.226063
68 0.463480 30.203050
69 0.692930 30.281880
70 0.652864 30.229106
71 0.491520 30.233780
72 0.778370 30.415920
73 0.682090 30.276460
74 0.564670 30.148920
75 0.655588 30.243047
76 0.647717 30.315984
77 0.518769 30.159384
78 0.683070 30.339650
79 0.662980 30.253890
80 0.591899 30.145857
81 0.699690 30.344650
82 0.441030 30.177240
83 0.612202 30.213022
84 0.472530 30.236980
85 0.473722 30.165020
86 0.499181 30.159485
87 0.6598021 30.29158
88 0.6601362 30.29119
89 0.48386 30.23142
90 0.679470 30.282190
91 0.685860 30.271070
92 0.528797 30.171251
93 0.514863 30.243976
94 0.603612 30.258705
95 0.484708 30.142588
96 0.523857 30.233239
97 0.395356 30.215351
98 0.612247 30.269341
99 0.55878815 30.17702095
100 0.747630 30.384240
101 0.538778 30.326353
102 0.554198 30.299815
103 0.504410 30.298260
104 0.418705 30.259747
105 0.669850 30.324100
106 0.654277 30.251667
107 0.460830 30.214070
108 0.378725 30.216429
Here is what I managed to do so far:
locations$Latitude=as.numeric(levels(locations$Latitude))[locations$Latitude]
locations$Longitude=as.numeric(levels(locations$Longitude))[locations$Longitude]
uganda <- raster::getData('GADM', country='UGA', level=1)
ggplot() +
geom_polygon(data = uganda,
aes(x = long, y = lat, group = group),
colour = "grey10", fill = "#fff7bc") +
geom_point(data = locations,
aes(x = Longitude, y = Latitude)) +
coord_map() +
theme_bw() +
xlab("Longitude") + ylab("Latitude")
As you can see by executing the code above, the map of Uganda is loaded from the GADM database and displayed correctly. However, I get the following warning message:
Warning:
Removed 108 rows containing missing values (geom_point).
I read in another post (Explain ggplot2 warning: "Removed k rows containing missing values") that this error might be caused by erroneous axis ranges. I'm not familiar with the plotting of geographic data and GADM maps, though. This is why I wasn't able to adjust the ranges (I guess this would be done in the geom_polygon -part). Can somebody help me, please?
I am not sure why you are running your first part of the code:
locations$Latitude=as.numeric(levels(locations$Latitude))[locations$Latitude] locations$Longitude=as.numeric(levels(locations$Longitude))[locations$Longitude]
If you don't run that part, there won't be any NA anymore. So if you run the following code, it should work:
library(tidyverse)
library(raster)
uganda <- raster::getData('GADM', country='UGA', level=1)
ggplot() +
geom_polygon(data = uganda,
aes(x = long, y = lat, group = group),
colour = "grey10", fill = "#fff7bc") +
geom_point(data = locations,
aes(x = Longitude, y = Latitude)) +
coord_map() +
theme_bw() +
xlab("Longitude") + ylab("Latitude")
Output:

when you need a Kinhom rather than a Kest?

envelope of the K funcition (and its derivative such as L) is very useful for validating a fitted spatial points process model. for instance, I fit a poisson model for a data J1a2, which is as following:
J1a2.points:
# X.1 X Y
1 1 118.544 1638.445
2 2 325.995 1761.223
3 3 681.625 1553.771
4 4 677.392 1816.261
5 5 986.451 1685.016
6 6 1469.093 1354.787
7 7 1608.805 1625.744
8 8 1994.071 1782.391
9 9 1968.669 1375.955
10 10 2362.403 1337.852
11 11 2701.099 1773.924
12 12 2900.083 1820.495
13 13 2963.588 1668.081
14 14 3412.360 1676.549
15 15 3378.490 1456.396
16 16 3721.420 1464.863
17 17 3823.028 1701.951
18 18 4072.817 1790.859
19 19 4089.751 1388.656
20 20 97.375 715.497
21 21 376.799 1033.025
22 22 563.082 1126.166
23 23 935.647 1206.607
24 24 512.277 486.876
25 25 935.647 757.834
26 26 1409.821 410.670
27 27 1435.223 639.290
28 28 1706.180 1045.726
29 29 1968.669 876.378
30 30 2307.365 711.263
31 31 2624.892 897.546
32 32 2654.528 1236.243
33 33 2857.746 423.371
34 34 3039.795 639.290
35 35 3298.050 707.029
36 36 3111.767 1011.856
37 37 3361.555 1227.775
38 38 4047.414 1185.438
39 39 3569.007 508.045
40 40 4250.632 469.942
41 41 4386.110 872.144
42 42 93.141 237.088
43 43 554.614 186.283
44 44 757.832 148.180
45 45 965.283 220.153
46 46 1723.115 296.360
47 47 1744.283 423.371
48 48 1913.631 203.218
49 49 2167.653 292.126
50 50 2629.126 211.685
51 51 3217.610 283.658
52 52 3827.262 325.996
and:
J1a2.Win<-owin(c(0, 4500.42),c(0, 1917.87))
if you draw evelope for the data with Lest:
library(spatstat)
env.data<-envelope(J1a2, Lest,correction="border",
nsim=19, global=TRUE)
plot(env.data,.-r~r, shade=NULL, legend=FALSE,
xlab=expression(paste("r(",mu,"m)")),ylab="L(r)-r", main = "")
the Lest() curve goes out of the envelope. however, if you use Linhom instead of Lest, you will find the Linhom() are all inside of the envelope.
it seems that this suggest a inhomogenous density kernel of the data. so I use y as covariate in fitting:
poisson.J1a2<-ppm(J1a2~1,Poisson(),correction="border")
y1.J1a2<-ppm(J1a2~y,correction="border")
anova(poisson.J1a2,y.J1a2,test="LR") #p=0.6484
I don't find any evidence of a spatial trend of density along y, or x, or their combinations.
then why the Linhom() outperform the Lest() in this case?
furthermore, when should one decide to use Linhom() instead of Lest?
You should first decide whether or not the intensity can be assumed to be constant. To help you with this you can look at kernel density estimates or do formal tests such as a quadrat test etc. If you decide that the intensity can be assumed to be constant you use Lest() if this is not the case you use Linhom().

How do I visualise multiple routes using Leaflet in 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

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