### Packages
library(dplyr)
library(ggmap)
library(OpenStreetMap)
### Get a map based on lat long coordinates
fn_get_map_osm <- function(lon, lat, dist = 1, ...){
h <- 0.0035 * dist * 3.5
w <- 0.0035 * dist * 4.0
print(c(lat-h, lon-w, lat+h, lon+w))
map_osm <- openmap(c(lat-h, lon-w), c(lat+h, lon+w), ...) %>% openproj()
map_osm
}
### 위도(latitude)와 경도(longitude) 상자 좌표 계산 함수
fn_lat_lon_box <- function(lon, lat, dist = 1){
h <- 0.0035 * dist * 3.5
w <- 0.0035 * dist * 4.0
#c(lat-h, lon-w, lat+h, lon+w)
list(upperLeft = c(lat-h, lon-w),
lowerRight = c(lat+h, lon+w))
}
#### (1)
lat1 <- 36.277; lat2 <- 36.473; lon1 <- 127.273; lon2 <- 127.497
krMap <- openmap(c(lat2, lon1), c(lat1, lon2),
zoom = 12, type = "osm",
mergeTiles = T) %>% openproj()
autoplot(krMap)
#### (2)
boxLocation <- fn_lat_lon_box(127.385, 36.375, 8)
krMap <- openmap(boxLocation$upperLeft, boxLocation$lowerRight,
zoom = 11, type = "osm-public-transport",
mergeTiles = T) %>% openproj()
autoplot(krMap)
#### (3)
krMap <- fn_get_map_osm(lon = 127.385, lat = 36.375, dist = 8,
zoom = 11, type = "esri-topo",
mergeTiles = T)
[1] 36.277 127.273 36.473 127.497
autoplot(krMap)
### Data
store.file <- "./data/소상공인시장진흥공단_상가(상권)정보_대전_202403.csv"
data.store <- read.csv(store.file, header=T, fileEncoding = "UTF-8")
data.store %>% head()
상가업소번호 <chr> | 상호명 <chr> | 지점명 <chr> | 상권업종대분류코드 <chr> | 상권업종대분류명 <chr> | 상권업종중분류코드 <chr> | 상권업종중분류명 <chr> | ||
---|---|---|---|---|---|---|---|---|
1 | MA0101202210A0009580 | 백파식당 | I2 | 음식 | I201 | 한식 | ||
2 | MA0101202210A0068117 | 줄또웰빙 | G2 | 소매 | G205 | 식료품 소매 | ||
3 | MA010120220805431091 | 정성약국 | G2 | 소매 | G215 | 의약·화장품 소매 | ||
4 | MA010120220805431125 | 다비치안경원 | G2 | 소매 | G216 | 안경·정밀기기 소매 | ||
5 | MA010120220805431202 | 베리도넛 | I2 | 음식 | I210 | 기타 간이 | ||
6 | MA010120220805431256 | 씨유 | G2 | 소매 | G204 | 종합 소매 |
### 자료 추출 - 커피전문점
data.cafe <- data.store %>% subset(상권업종소분류명=="카페")
### 산점도 – ggplot()
ggplot() + geom_point(data=data.cafe, aes(x=경도, y=위도, colour=시군구명))
### 지도 표시
autoplot(krMap) +
geom_point(data=data.cafe, aes(x=경도, y=위도, colour=시군구명)) +
lims(x=c(127.273,127.497), y=c(36.277, 36.473)) +
theme_minimal()
Scale for x is already present.
Adding another scale for x, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
경고: Removed 38 rows containing missing values or values outside the scale range (`geom_point()`).
### 등고선
autoplot(krMap) +
stat_density2d(data=data.cafe, aes(x=경도, y=위도)) +
lims(x=c(127.273,127.497), y=c(36.277, 36.473)) +
theme_minimal()
Scale for x is already present.
Adding another scale for x, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
경고: Removed 38 rows containing non-finite outside the scale range (`stat_density2d()`).
### 밀도
autoplot(krMap) +
stat_density2d(data=data.cafe, aes(x=경도, y=위도), geom="polygon", alpha=0.2) +
lims(x=c(127.273,127.497), y=c(36.277, 36.473)) +
theme_minimal()
Scale for x is already present.
Adding another scale for x, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
경고: Removed 38 rows containing non-finite outside the scale range (`stat_density2d()`).
### 밀도 + 색상
autoplot(krMap) +
stat_density2d(data=data.cafe,
aes(x=경도, y=위도, fill=after_stat(level)),
geom="polygon", alpha=0.2) +
scale_fill_gradient(low="yellow", high="red") +
lims(x=c(127.273,127.497), y=c(36.277, 36.473)) +
theme_minimal()
Scale for x is already present.
Adding another scale for x, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
경고: Removed 38 rows containing non-finite outside the scale range (`stat_density2d()`).