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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> suppressPackageStartupMessages(library(sf))
> options(rgdal_show_exportToProj4_warnings = "none")
> library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
>
> # plot linestrings:
> l1 = st_linestring(matrix(runif(6)-0.5,,2))
> l2 = st_linestring(matrix(runif(6)-0.5,,2))
> l3 = st_linestring(matrix(runif(6)-0.5,,2))
> s = st_sf(a=2:4, b=st_sfc(l1,l2,l3))
> plot(s, col = s$a, axes = FALSE)
> plot(s, col = s$a)
> attr(s$b, "proj4string") = sp::CRS("+proj=longlat +ellps=WGS84 +no_defs")@projargs
> plot(s, col = s$a, axes = TRUE)
> plot(s, col = s$a, lty = s$a, lwd = s$a, pch = s$a, type = 'b')
> l4 = st_linestring(matrix(runif(6),,2))
> plot(st_sf(a=1,b=st_sfc(l4)), add = TRUE)
> # plot multilinestrings:
> ml1 = st_multilinestring(list(l1, l2))
> ml2 = st_multilinestring(list(l3, l4))
> ml = st_sf(a = 2:3, b = st_sfc(ml1, ml2))
> plot(ml, col = ml$a, lty = ml$a, lwd = ml$a, pch = ml$a, type = 'b')
> # plot points:
> p1 = st_point(c(1,2))
> p2 = st_point(c(3,3))
> p3 = st_point(c(3,0))
> p = st_sf(a=2:4, b=st_sfc(p1,p2,p3))
> plot(p, col = s$a, axes = TRUE)
> plot(p, col = s$a)
> plot(p, col = p$a, pch = p$a, cex = p$a, bg = s$a, lwd = 2, lty = 2, type = 'b')
> p4 = st_point(c(2,2))
> plot(st_sf(a=1, st_sfc(p4)), add = TRUE)
> # multipoints:
> mp1 = st_multipoint(matrix(1:4,2))
> mp2 = st_multipoint(matrix(5:8,2))
> mp = st_sf(a = 2:3, b = st_sfc(mp1, mp2))
> plot(mp)
> plot(mp, col = mp$a, pch = mp$a, cex = mp$a, bg = mp$a, lwd = mp$a, lty = mp$a, type = 'b')
> # polygon:
> outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE)
> hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE)
> hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE)
> pl1 = st_polygon(list(outer, hole1, hole2))
> pl2 = st_polygon(list(outer+10, hole1+10, hole2+10))
> po = st_sf(a = 2:3, st_sfc(pl1,pl2))
> plot(po, col = po$a, border = rev(po$a), lwd=3)
> # multipolygon
> r10 = matrix(rep(c(0,10),each=5),5)
> pl1 = list(outer, hole1, hole2)
> pl2 = list(outer+10, hole1+10, hole2+10)
> pl3 = list(outer+r10, hole1+r10, hole2+r10)
> mpo1 = st_multipolygon(list(pl1,pl2))
> mpo2 = st_multipolygon(list(pl3))
> mpo = st_sf(a=2:3, b=st_sfc(mpo1,mpo2))
> plot(mpo, col = mpo$a, border = rev(mpo$a), lwd = 2)
> # geometrycollection:
> gc1 = st_geometrycollection(list(mpo1, st_point(c(21,21)), l1 * 2 + 21))
> gc2 = st_geometrycollection(list(mpo2, l2 - 2, l3 - 2, st_point(c(-1,-1))))
> gc = st_sf(a=2:3, b = st_sfc(gc1,gc2))
> plot(gc, cex = gc$a, col = gc$a, border = rev(gc$a) + 2, lwd = 2)
>
> plot(gc1)
>
> plot(st_sfc(mp1, mpo1))
>
> # color ramp
> nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
> plot(nc)
Warning message:
plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all
> plot(nc, axes = TRUE)
Warning message:
plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all
> plot(nc, col="lightgrey")
Warning message:
plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all
> plot(st_centroid(nc), add = TRUE, col = 1)
Warning messages:
1: In st_centroid.sf(nc) :
st_centroid assumes attributes are constant over geometries of x
2: In st_centroid.sfc(st_geometry(x), of_largest_polygon = of_largest_polygon) :
st_centroid does not give correct centroids for longitude/latitude data
3: In plot.sf(st_centroid(nc), add = TRUE, col = 1) :
ignoring all but the first attribute
> nc %>%
+ select(geometry) %>%
+ plot()
>
> nc$f = cut(nc[[1]], 5)
> plot(nc["f"], key.pos = 1)
> plot(nc[1], key.pos = 1)
>
> # test background map plotting:
> load("bgmap.rda")
> merc = st_crs(3857)
> WGS84 = st_crs(4326)
> nc = st_transform(nc, WGS84)
> ## ggmap:
> #library(ggmap)
> #bgMap = get_map(unname(st_bbox(nc)), source = "google", zoom = 8)
> plot(st_transform(nc[1], merc), bgMap = bgMap)
>
> # RgoogleMaps:
> #library(RgoogleMaps)
> #center = c(mean(st_bbox(nc)[c(2,4)]), mean(st_bbox(nc)[c(1,3)]))
> #g = GetMap(center=center, zoom=6) # google
> par(mar = c(0,0,1,0))
> plot(st_transform(nc, merc), bgMap = g)
Warning message:
plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all
>
> m = st_make_grid()
> st_crs(m) = NA_crs_
> m = st_segmentize(m, 2)
> st_crs(m) = 4326
> plot(m, axes = TRUE)
> g = st_transform(m, st_crs("+proj=ortho +lat_0=30 +lon_0=45"), check = TRUE)
> plot(g, axes = TRUE)
>
> nc[[1]] = NA
> nc[[10]] = 1
> plot(nc, pal = rainbow, nbreaks = 3)
Warning messages:
1: plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
> plot(nc, pal = rainbow, breaks = "jenks", nbreaks = 3)
Warning messages:
1: plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
> plot(nc, pal = rainbow, breaks = (0:10)/3)
Warning message:
plotting the first 10 out of 15 attributes; use max.plot = 15 to plot all
>
> # logz:
> nc$e = 10^(nc$SID74)
> plot(nc["e"], logz = TRUE)
>
> # shared key:
> plot(nc[c("SID74", "SID79")], key.pos = -1)
> plot(nc[c("BIR74", "BIR79")], key.pos = 1, logz=TRUE)
>
> proc.time()
user system elapsed
4.064 0.083 4.138
|