get_map does not download full extent of map wanted - r

I have a data.frame b that consists of 51 geographical points.
Using the ggmap package I tried to show the points on a google map
first I downloaded the map from google using a bounding box:
p<-ggmap(get_map(location = c(left = min(b$coords.x1), bottom = min(b$coords.x2), right =max(b$coords.x1) , top = max(b$coords.x2)))) but when I ploted the map :
p+geom_point(aes(x=coords.x1, y=coords.x2),data=b, alpa=0.5, size=3)
I got the next warning:
Removed 21 rows containing missing values (geom_point)
here is my data frame:
>b
coords.x1 coords.x2
1 12.51787 41.87951
2 12.47803 41.89199
3 12.48278 41.90599
4 12.47687 41.89861
5 12.49223 41.89021
6 12.47090 41.90332
7 12.46656 41.89767
8 12.48494 41.90068
9 12.45351 41.90665
10 12.47221 41.89556
11 12.48449 41.89064
12 12.50552 41.89576
13 12.47714 41.85862
14 12.49313 41.87940
15 12.45394 41.90217
16 12.45446 41.90305
17 12.45446 41.90305
18 12.49214 41.91421
19 12.48331 41.90093
20 12.49060 41.88977
21 12.48533 41.89242
22 12.48111 41.89503
23 12.48313 41.89385
24 12.47454 41.89958
25 12.47631 41.91145
26 12.48694 41.88833
27 12.47477 41.88356
28 12.50742 41.88401
29 12.46809 41.88218
30 12.52118 41.91346
31 12.49211 41.88647
32 12.51339 41.88052
33 12.51339 41.88052
34 12.49666 41.88929
35 12.47537 41.89831
36 12.47733 41.89943
37 12.48074 41.90128
38 12.47543 41.90618
39 12.47597 41.90692
40 12.47724 41.90568
41 12.46651 41.90308
42 12.46693 41.92834
43 12.47402 41.93220
44 12.53848 41.83618
45 12.47951 41.89143
46 12.47307 41.89916
47 12.47705 41.90555
48 12.47938 41.90352
49 12.47922 41.90470
50 12.47639 41.91081
51 12.49242 41.89481

I usually follow the strategy of give my data a 'window' with f from make_bbox:
bbox <- ggmap::make_bbox(Long, Lat, data, f = 0.5)
map_loc <- get_map(location = bbox, source = 'google', maptype = 'terrain')
map <- ggmap(map_loc, extent = 'device', maprange=FALSE, darken = c(0.5, "white"))
Another option is to set a different zoom factor:
map_loc <- get_map(location = c(lon = mean(data$lon), mean(data$lat)),
source = 'google', zoom = 14)
map <- ggmap(map_loc, extent = 'device')
play with zoom level to get the desired window area

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:

Why does gDistance return distances that do not make sense with my data?

I am new to R and am working on a project for a class using R for geospatial analysis. The goal of my project is to find the distances between the points of recreation opportunities () to the the nearest national park polygon (). When I used gDistance, it returns crazy big numbers that don't makes sense with my data. Everything is within the lower 48 states. I have made sure to have all datasets in the same projection. This is the data I am using:
NPS Boundaries
USFS Recreation Opportunities
Census Bureau State Boundaries
library(tidyverse)
library(sp)
library(rgdal)
library(spdplyr)
ops <- readOGR(dsn = "C:/Users/amand/Desktop/GISCert/Geospatial Analysis/Final/Recreation_Opportunities_(Feature_Layer)/Recreation_Opportunities_(Feature_Layer).shp")[enter image description here][1]
discamping <- ops %>%
filter(MARKERACTI == "Dispersed Camping")
discampspts = SpatialPoints(discamping)
library(maptools)
P4S.latlon <- CRS("+proj=longlat +datum=NAD83 +no_defs ")
npssp <- readShapePoly("C:/Users/amand/Desktop/GISCert/Geospatial Analysis/Final/NPS_-_Land_Resources_Division_Boundary_and_Tract_Data_Service/NPS_-_Land_Resources_Division_Boundary_and_Tract_Data_Service.shp", verbose=TRUE, proj4string=P4S.latlon)
distance <- apply(gDistance(spgeom1 = discampspts, spgeom2 = npssp, byid=TRUE),2,min)
This is a snip of the results I get, which make no sense. Why are all of the them 7,463,000+ meters?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7463573 7463573 7463573 7463572 7463572 7463572 7463573 7463573 7463573 7463573 7463572 7463572 7463572 7463572 7463572 7463572 7463572 7463544 7463557 7463557
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
7463547 7463547 7463547 7463547 7463546 7463546 7463546 7463546 7463546 7463546 7463547 7463546 7463548 7463549 7463549 7463548 7463549 7463548 7463549 7463548
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
7463548 7463543 7463543 7463541 7463542 7463543 7463583 7463583 7463583 7463554 7463555 7463555 7463554 7463554 7463554 7463552 7463555 7463578 7463576 7463577
I changed the code to read the second shape file using readOGR and then explicitly transform it to the required spatial projection using spTransform because the help for the readShapePoly function indicates it is deprecated. The original shape file has EPSG:3857 - WGS 84 / Pseudo-Mercator as its projection, so a transformation is required. The gDistance function requires planar coordinates, not lat long, but it's only a warning and it does work.
EDIT: added use of planar projection and use mapview to check result.
library(tidyverse)
library(sp)
library(rgdal)
library(spdplyr)
library(rgeos)
library(maptools)
P4S.latlon <- CRS("+proj=longlat +datum=NAD83 +no_defs")
#https://data-usfs.hub.arcgis.com/datasets/usfs::recreation-opportunities-feature-layer/about
ops <- readOGR(dsn = "spatialdistance/data/recreation/Recreation_Opportunities_(Feature_Layer).shp")
discamping <- ops %>% filter(MARKERACTI == "Dispersed Camping")
discampspts = SpatialPoints(discamping, proj4string = P4S.latlon)
#https://public-nps.opendata.arcgis.com/datasets/nps::nps-boundary-1/about
nps <- readOGR('spatialdistance/data/land/NPS_-_Land_Resources_Division_Boundary_and_Tract_Data_Service.shp')
npssp <- spTransform(nps, P4S.latlon)
distance <- apply(gDistance(spgeom1 = discampspts, spgeom2 = npssp, byid=TRUE),2,min)
head(distance)
# These are difficult to use
# 1 2 3 4 5 6
#0.5834679 0.4499780 0.5872363 0.5635809 0.9635063 1.0696972
# Transform to use the nps projection
discampspts.proj <- spTransform(discampspts, proj4string(nps))
distance2 <- apply(gDistance(spgeom1 = discampspts.proj, spgeom2 = nps, byid=TRUE),2,min)
head(distance2) # These are in metres
#1 2 3 4 5 6
#71733.78 54134.95 72504.21 72545.55 120861.89 129781.84
# Plot a point and a shape to check manually that the distance is in metres
library(mapview)
p1 = discampspts.proj[203,]
p2 = nps[7,]
mapview(p1)+mapview(p2)
gDistance(p1, p2, byid = T)
#1 46179.27
Output from mapview showing that the point and shape seem to be about 46 km apart.

test for in-tile for Dirichlet tile, using R

So I can take points and use the R libraries deldir or spatstat::dirichlet to find the dirichlet tesselation of those points.
Now I have a point not in the set, and I want to know the indices of the points forming the dirichlet tile which my not-in-set-point is interior to. I can get there by knowing the tile label (or index).
Are there any libraries or methods to do this? I'm thinking spatstat, but not finding something there yet.
The function cut.ppp() can take a point pattern and find which tesselation
tile each point in the pattern belongs to. Below is the code for a simple
example of a point pattern that only contains a single point (0.5, 0.5).
library(spatstat)
dd <- dirichlet(cells)
plot.tess(dd, do.labels = TRUE)
xx <- ppp(.5, .5, window = Window(dd))
plot(xx, add = TRUE, col = "red", cex = 2, pch = 20)
yy <- cut(xx, dd)
yy
#> Marked planar point pattern: 1 point
#> Multitype, with levels =
#> 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
#> window: rectangle = [0, 1] x [0, 1] units
marks(yy)
#> [1] 18
#> 42 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ... 42
Created on 2018-12-03 by the reprex package (v0.2.1)
If X is a point pattern and B is a tessellation, then
M <- marks(cut(X, B))
returns a factor (vector of categorical values) identifying which tile contains each of the points of X. Alternatively,
M <- tileindex(X$x, X$y, B)
or
f <- as.function(B)
M <- f(X)

Aesthetics must be either length 1 or the same as the data: ymin, ymax, x, y, colour, when using a second geom_errorbar function

I'm trying to add error bars to a second curve (using dataset "pmfprofbs01"), but I'm having problems and I couldn't fix this.
There are a few threads on this error, but unfortunately it looks like every other answer is case specific, and I'm not able to overcome this error in my code. I am able to plot a first smoothed curve (stat_smooth) and overlapping errorbars (using geom_errobar). The problem rises when I try to add a second curve to the same graph, for comparison purposes.
With following code, I get the following error: "Error: Aesthetics must be either length 1 or the same as the data (35): ymin, ymax, x, y, colour"
I am looking to add additional errorbars to the second smoothed curve (corresponding to datasets pmfprof01 and pmfprofbs01).
Could someone explain why I keep getting this error? The code works until using the second call of geom_errorbar().
These are my 4 datasets (all used as data frames):
- pmfprof1 and pmfprof01 are the two datasets used for applying the smoothing method.
- pmfprofbs1 and pmfprofbs01 contain additional information based on an error analysis for plotting error bars.
> pmfprof1
Z correctedpmfprof1
1 -1.1023900 -8.025386e-22
2 -1.0570000 6.257110e-02
3 -1.0116000 1.251420e-01
4 -0.9662020 2.143170e-01
5 -0.9208040 3.300960e-01
6 -0.8754060 4.658550e-01
7 -0.8300090 6.113410e-01
8 -0.7846110 4.902430e-01
9 -0.7392140 3.344200e-01
10 -0.6938160 4.002040e-01
11 -0.6484190 1.215460e-01
12 -0.6030210 -1.724360e-01
13 -0.5576240 -6.077170e-01
14 -0.5122260 -1.513420e+00
15 -0.4668290 -2.075330e+00
16 -0.4214310 -2.617160e+00
17 -0.3760340 -3.350500e+00
18 -0.3306360 -4.076220e+00
19 -0.2852380 -4.926540e+00
20 -0.2398410 -5.826390e+00
21 -0.1944430 -6.761300e+00
22 -0.1490460 -7.301530e+00
23 -0.1036480 -7.303880e+00
24 -0.0582507 -7.026800e+00
25 -0.0128532 -6.627960e+00
26 0.0325444 -6.651490e+00
27 0.0779419 -6.919830e+00
28 0.1233390 -6.686490e+00
29 0.1687370 -6.129060e+00
30 0.2141350 -6.120890e+00
31 0.2595320 -6.455160e+00
32 0.3049300 -6.554560e+00
33 0.3503270 -6.983390e+00
34 0.3957250 -7.413500e+00
35 0.4411220 -6.697370e+00
36 0.4865200 -5.477230e+00
37 0.5319170 -4.552890e+00
38 0.5773150 -3.393060e+00
39 0.6227120 -2.449930e+00
40 0.6681100 -2.183190e+00
41 0.7135080 -1.673980e+00
42 0.7589050 -8.003740e-01
43 0.8043030 -2.918780e-01
44 0.8497000 -1.159710e-01
45 0.8950980 9.123767e-22
> pmfprof01
Z correctedpmfprof01
1 -1.25634000 -1.878749e-21
2 -1.20387000 -1.750190e-01
3 -1.15141000 -3.500380e-01
4 -1.09894000 -6.005650e-01
5 -1.04647000 -7.935110e-01
6 -0.99400600 -8.626150e-01
7 -0.94153900 -1.313880e+00
8 -0.88907200 -2.067770e+00
9 -0.83660500 -2.662440e+00
10 -0.78413800 -4.514190e+00
11 -0.73167100 -7.989510e+00
12 -0.67920400 -1.186870e+01
13 -0.62673800 -1.535970e+01
14 -0.57427100 -1.829150e+01
15 -0.52180400 -2.067170e+01
16 -0.46933700 -2.167890e+01
17 -0.41687000 -2.069820e+01
18 -0.36440300 -1.662640e+01
19 -0.31193600 -1.265950e+01
20 -0.25946900 -1.182580e+01
21 -0.20700200 -1.213370e+01
22 -0.15453500 -1.233680e+01
23 -0.10206800 -1.235160e+01
24 -0.04960160 -1.123630e+01
25 0.00286531 -9.086940e+00
26 0.05533220 -6.562710e+00
27 0.10779900 -4.185860e+00
28 0.16026600 -3.087430e+00
29 0.21273300 -2.005150e+00
30 0.26520000 -9.295540e-02
31 0.31766700 1.450360e+00
32 0.37013400 1.123910e+00
33 0.42260100 2.426750e-01
34 0.47506700 1.213370e-01
35 0.52753400 5.265226e-21
> pmfprofbs1
Z correctedpmfprof01 bsmean bssd bsse bsci
1 -1.1023900 -8.025386e-22 0.00000000 0.0000000 0.00000000 0.0000000
2 -1.0570000 6.257110e-02 1.46519200 0.6691245 0.09974719 0.2010273
3 -1.0116000 1.251420e-01 1.62453300 0.6368053 0.09492933 0.1913175
4 -0.9662020 2.143170e-01 1.62111600 0.7200497 0.10733867 0.2163269
5 -0.9208040 3.300960e-01 1.44754700 0.7236743 0.10787900 0.2174158
6 -0.8754060 4.658550e-01 1.67509800 0.7148755 0.10656735 0.2147724
7 -0.8300090 6.113410e-01 1.78144200 0.7374481 0.10993227 0.2215539
8 -0.7846110 4.902430e-01 1.73058700 0.7701354 0.11480501 0.2313743
9 -0.7392140 3.344200e-01 0.97430090 0.7809477 0.11641681 0.2346227
10 -0.6938160 4.002040e-01 1.26812000 0.8033838 0.11976139 0.2413632
11 -0.6484190 1.215460e-01 0.93601510 0.7927926 0.11818254 0.2381813
12 -0.6030210 -1.724360e-01 0.63201080 0.8210839 0.12239996 0.2466809
13 -0.5576240 -6.077170e-01 0.05952252 0.8653050 0.12899205 0.2599664
14 -0.5122260 -1.513420e+00 0.57893690 0.8858471 0.13205429 0.2661379
15 -0.4668290 -2.075330e+00 -0.08164613 0.8921298 0.13299086 0.2680255
16 -0.4214310 -2.617160e+00 -1.08074600 0.8906925 0.13277660 0.2675937
17 -0.3760340 -3.350500e+00 -1.67279700 0.9081813 0.13538367 0.2728479
18 -0.3306360 -4.076220e+00 -2.50074900 1.0641550 0.15863486 0.3197076
19 -0.2852380 -4.926540e+00 -3.12062200 1.0639080 0.15859804 0.3196333
20 -0.2398410 -5.826390e+00 -4.47060100 1.1320770 0.16876008 0.3401136
21 -0.1944430 -6.761300e+00 -5.40812700 1.1471780 0.17101120 0.3446504
22 -0.1490460 -7.301530e+00 -6.42419100 1.1685490 0.17419700 0.3510710
23 -0.1036480 -7.303880e+00 -5.79613500 1.1935850 0.17792915 0.3585926
24 -0.0582507 -7.026800e+00 -5.85496900 1.2117630 0.18063896 0.3640539
25 -0.0128532 -6.627960e+00 -6.70480400 1.1961400 0.17831002 0.3593602
26 0.0325444 -6.651490e+00 -8.27106200 1.3376870 0.19941060 0.4018857
27 0.0779419 -6.919830e+00 -8.79402900 1.3582760 0.20247983 0.4080713
28 0.1233390 -6.686490e+00 -8.35947700 1.3673080 0.20382624 0.4107848
29 0.1687370 -6.129060e+00 -8.04437600 1.3921620 0.20753126 0.4182518
30 0.2141350 -6.120890e+00 -8.18588300 1.5220550 0.22689456 0.4572759
31 0.2595320 -6.455160e+00 -8.37217600 1.5436800 0.23011823 0.4637728
32 0.3049300 -6.554560e+00 -8.59346400 1.6276880 0.24264140 0.4890116
33 0.3503270 -6.983390e+00 -8.88378700 1.6557140 0.24681927 0.4974316
34 0.3957250 -7.413500e+00 -9.72709800 1.6569390 0.24700188 0.4977996
35 0.4411220 -6.697370e+00 -9.46033400 1.6378470 0.24415582 0.4920637
36 0.4865200 -5.477230e+00 -8.37590600 1.6262700 0.24243002 0.4885856
37 0.5319170 -4.552890e+00 -7.52867000 1.6617010 0.24771176 0.4992302
38 0.5773150 -3.393060e+00 -6.89192300 1.6667330 0.24846189 0.5007420
39 0.6227120 -2.449930e+00 -6.25115300 1.6670390 0.24850750 0.5008340
40 0.6681100 -2.183190e+00 -6.05373800 1.6720180 0.24924973 0.5023298
41 0.7135080 -1.673980e+00 -5.10526700 1.6668400 0.24847784 0.5007742
42 0.7589050 -8.003740e-01 -4.42001600 1.6561830 0.24688918 0.4975725
43 0.8043030 -2.918780e-01 -4.26640200 1.6588970 0.24729376 0.4983878
44 0.8497000 -1.159710e-01 -4.46318500 1.6533830 0.24647179 0.4967312
45 0.8950980 9.123767e-22 -5.17173200 1.6557990 0.24683194 0.4974571
> pmfprofbs01
Z correctedpmfprof01 bsmean bssd bsse bsci
1 -1.25634000 -1.878749e-21 0.000000 0.0000000 0.00000000 0.0000000
2 -1.20387000 -1.750190e-01 2.316589 0.4646486 0.07853995 0.1596124
3 -1.15141000 -3.500380e-01 2.320647 0.4619668 0.07808664 0.1586911
4 -1.09894000 -6.005650e-01 2.635883 0.6519826 0.11020517 0.2239639
5 -1.04647000 -7.935110e-01 2.814679 0.6789875 0.11476983 0.2332404
6 -0.99400600 -8.626150e-01 2.588038 0.7324196 0.12380151 0.2515949
7 -0.94153900 -1.313880e+00 2.033736 0.7635401 0.12906183 0.2622852
8 -0.88907200 -2.067770e+00 2.394285 0.8120181 0.13725611 0.2789380
9 -0.83660500 -2.662440e+00 2.465425 0.9485307 0.16033095 0.3258317
10 -0.78413800 -4.514190e+00 0.998115 1.0177400 0.17202946 0.3496059
11 -0.73167100 -7.989510e+00 -1.585430 1.0502190 0.17751941 0.3607628
12 -0.67920400 -1.186870e+01 -5.740894 1.2281430 0.20759406 0.4218819
13 -0.62673800 -1.535970e+01 -9.325951 1.3289330 0.22463068 0.4565045
14 -0.57427100 -1.829150e+01 -12.010540 1.3279860 0.22447060 0.4561792
15 -0.52180400 -2.067170e+01 -14.672770 1.3296720 0.22475559 0.4567583
16 -0.46933700 -2.167890e+01 -14.912250 1.3192610 0.22299581 0.4531820
17 -0.41687000 -2.069820e+01 -12.850570 1.3288470 0.22461614 0.4564749
18 -0.36440300 -1.662640e+01 -6.093746 1.3497100 0.22814263 0.4636416
19 -0.31193600 -1.265950e+01 -5.210692 1.3602240 0.22991982 0.4672533
20 -0.25946900 -1.182580e+01 -6.041660 1.3818700 0.23357866 0.4746890
21 -0.20700200 -1.213370e+01 -5.765808 1.3854680 0.23418683 0.4759249
22 -0.15453500 -1.233680e+01 -6.985883 1.4025360 0.23707185 0.4817880
23 -0.10206800 -1.235160e+01 -7.152865 1.4224030 0.24042999 0.4886125
24 -0.04960160 -1.123630e+01 -3.600538 1.4122650 0.23871635 0.4851300
25 0.00286531 -9.086940e+00 -0.751673 1.5764920 0.26647578 0.5415439
26 0.05533220 -6.562710e+00 2.852910 1.5535620 0.26259991 0.5336672
27 0.10779900 -4.185860e+00 5.398850 1.5915640 0.26902342 0.5467214
28 0.16026600 -3.087430e+00 6.262459 1.6137360 0.27277117 0.5543377
29 0.21273300 -2.005150e+00 8.047920 1.6283340 0.27523868 0.5593523
30 0.26520000 -9.295540e-02 11.168640 1.6267620 0.27497297 0.5588123
31 0.31766700 1.450360e+00 12.345900 1.6363310 0.27659042 0.5620994
32 0.37013400 1.123910e+00 12.124650 1.6289230 0.27533824 0.5595546
33 0.42260100 2.426750e-01 11.279890 1.6137100 0.27276677 0.5543288
34 0.47506700 1.213370e-01 11.531670 1.6311490 0.27571450 0.5603193
35 0.52753400 5.265226e-21 11.284980 1.6662890 0.28165425 0.5723903
The code for plotting both curves is:
deltamean01<-pmfprofbs01[,"bsmean"]-
pmfprofbs01[,"correctedpmfprof01"]
correctmean01<-pmfprofbs01[,"bsmean"]-deltamean01
deltamean1<-pmfprofbs1[,"bsmean"]-
pmfprofbs1[,"correctedpmfprof1"]
correctmean1<-pmfprofbs1[,"bsmean"]-deltamean1
pl<- ggplot(pmfprof1, aes(x=pmfprof1[,1], y=pmfprof1[,2],
colour="red")) +
list(
stat_smooth(method = "gam", formula = y ~ s(x), size = 1,
colour="chartreuse3",fill="chartreuse3", alpha = 0.3),
geom_line(data=pmfprof1,linetype=4, size=0.5,colour="chartreuse3"),
geom_errorbar(aes(ymin=correctmean1-pmfprofbs1[,"bsci"],
ymax=correctmean1+pmfprofbs1[,"bsci"]),
data=pmfprofbs1,colour="chartreuse3",
width=0.02,size=0.9),
geom_point(data=pmfprof1,size=1,colour="chartreuse3"),
xlab(expression(xi*(nm))),
ylab("PMF (KJ/mol)"),
## GCD
geom_errorbar(aes(ymin=correctmean01-pmfprofbs01[,"bsci"],
ymax=correctmean01+pmfprofbs01[,"bsci"]),
data=pmfprofbs01,
width=0.02,size=0.9),
geom_line(data=pmfprof01,aes(x=pmfprof01[,1],y=pmfprof01[,2]),
linetype=4, size=0.5,colour="darkgreen"),
stat_smooth(data=pmfprof01,method = "gam",aes(x=pmfprof01[,1],pmfprof01[,2]),
formula = y ~ s(x), size = 1,
colour="darkgreen",fill="darkgreen", alpha = 0.3),
theme(text = element_text(size=20),
axis.text.x = element_text(size=20,colour="black"),
axis.text.y = element_text(size=20,colour="black")),
scale_x_continuous(breaks=number_ticks(8)),
scale_y_continuous(breaks=number_ticks(8)),
theme(panel.background = element_rect(fill ='white',
colour='gray')),
theme(plot.background = element_rect(fill='white',
colour='white')),
theme(legend.position="none"),
theme(legend.key = element_blank()),
theme(legend.title = element_text(colour='gray', size=20)),
NULL
)
pl
This is the result of using pl,
[enter image description here][1]
[1]: https://i.stack.imgur.com/x8FjY.png
Thanks in advance for any suggestion,

mapping chemical concentrations with ggplot

I am trying to make map to show the concentrations of Chromium recorded in topsoil in Scotland (n = 1000). The following is a sub-set of the data:
Easting Northing Concentration
1 -4.327230 55.94000 1.913814
2 -4.336588 55.77886 1.408240
3 -4.334057 55.93637 1.798651
4 -4.340633 55.94451 1.629410
5 -4.341627 55.77247 1.382017
6 -4.354362 55.78004 1.432969
7 -4.327912 55.94871 1.488551
8 -4.301948 55.77286 1.278754
9 -4.317669 55.77715 1.465383
10 -4.266635 55.77981 1.793092
11 -4.349507 55.77358 1.336460
12 -4.331458 55.92509 1.546543
13 -4.360420 55.77211 1.720986
14 -4.316048 55.93779 1.876795
15 -4.348813 55.92620 1.637490
16 -4.358550 55.92574 1.460898
17 -4.271819 55.88522 2.011570
18 -4.350699 55.93884 1.385606
19 -4.323065 55.78208 1.620136
20 -4.305748 55.94769 1.463893
21 -4.324094 55.76453 1.416641
22 -4.311998 55.77294 1.390935
23 -4.295788 55.77657 1.378398
24 -4.351286 55.94323 1.485721
25 -4.344118 55.78473 1.623249
26 -4.358147 55.93492 1.454845
27 -4.310889 55.78653 1.372912
28 -4.270665 55.77506 1.706718
29 -4.341747 55.78244 1.561101
30 -4.312615 55.93929 1.521138
31 -4.330014 55.78626 1.564666
32 -4.328320 55.95283 2.313656
33 -4.334340 55.93043 2.007748
34 -4.317788 55.76303 1.309630
35 -4.342244 55.93936 1.680336
36 -4.351105 55.94818 1.673942
37 -4.351354 55.93379 1.396199
38 -4.318706 55.93135 1.854913
39 -4.315999 55.93428 1.361728
40 -4.326163 55.78588 1.646404
41 -4.302010 55.78203 2.023664
42 -4.318585 55.78720 1.305351
43 -4.304388 55.94097 1.465383
44 -4.309106 55.93414 1.539076
45 -4.297275 55.77474 1.503791
46 -4.298785 55.93290 1.447158
47 -4.326837 55.77311 1.555094
48 -4.342423 55.92641 1.338456
49 -4.332528 55.77228 1.491362
50 -4.347461 55.78197 1.426511
str(dat.tmp)
'data.frame': 50 obs. of 3 variables:
$ Easting : num -4.33 -4.34 -4.33 -4.34 -4.34 ...
$ Northing : num 55.9 55.8 55.9 55.9 55.8 ...
$ Concentration: num 1.91 1.41 1.8 1.63 1.38 ...
This is the code I am currently using to produce the concentrations on a map of Glasgow:
qmap(location="glasgow", maptype = "terrain",zoom=10,color="bw"
,extent="panel",
maprange=FALSE) +
stat_contour(data = dat.tmp, geom="polygon",
aes(x =Easting, y = Northing, z = Concentration
, fill = ..level.. ) ) +
scale_fill_continuous(name = "Cu (mg/kg)", low = "yellow", high = "red" )
When executing, this is returning:
Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0
In addition: Warning message:
Not possible to generate contour data
This is a similar issue to a previous post - the map / plot I am trying create is very similar too.
Filled contour plot with R/ggplot/ggmap
Any help would be greatly appreciated - thank you.
I've reproduced the error, and also spotted a warning:
Warning message: Not possible to generate contour data
A quick google points at a similar question, which is resolved by using stat_density2d instead of stat_contour:
qmap(location="glasgow", maptype = "terrain", zoom=10, color="bw",
extent="panel", maprange=FALSE) +
stat_density2d(data=dat,
aes(x=Easting, y=Northing, z=Concentration, fill=..level.. ))

Resources