-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathincome_updated.Rmd
150 lines (120 loc) · 4.01 KB
/
income_updated.Rmd
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
---
title: "Vegetarian Restaurant and Income"
author: "Ming Gong"
output:
html_document:
code_folding: hide
---
```{r, include=FALSE, results='hide', warning=FALSE}
library(knitr)
opts_chunk$set(fig.path="figures\\",
cache.path="cache\\",
cache=FALSE,
echo=TRUE,
message=FALSE,
warning=FALSE,
fig.align = 'center')
library(leaflet)
library(dplyr)
library(tidyr)
library(magrittr)
library(RANN)
library(ggplot2)
library(lubridate)
library(sf)
library(ggmap)
library(geojsonio)
library(ggthemes)
library(viridis)
library(raster)
library(urbnmapr)
library(tidyverse)
library(stringr)
library(sp)
library(maps)
library(maptools)
library(plotly)
```
```{r}
Income=read.csv("data\\Income.csv",stringsAsFactors=F)
Income<-Income %>%
rename(
county_fips = State...County.Name,
county_id=County.ID,
povertyrate=All.Ages.in.Poverty.Percent
)
Res=read.csv("data\\Restaurant.csv", stringsAsFactors=F)
a =Res$cuisines
vege <- str_detect(a,"Vegetarian")
ResV <- cbind(vege,Res)
ResV$vege <- as.numeric(ResV$vege)
ResVV <- subset(ResV,vege==1)
ResVVV <- ResVV %>%
dplyr::select(id,city,name,latitude, longitude,phones,paymentTypes,postalCode) %>%
mutate(latitude=as.numeric(latitude), longitude=as.numeric(longitude)) %>%
na.omit()
latlong2county <- function(pointsDF) {
# Prepare SpatialPolygons object with one SpatialPolygon
# per county
counties <- map('county', fill=TRUE, col="transparent", plot=FALSE)
IDs <- sapply(strsplit(counties$names, ":"), function(x) x[1])
counties_sp <- map2SpatialPolygons(counties, IDs=IDs,
proj4string=CRS("+proj=longlat +datum=WGS84"))
# Convert pointsDF to a SpatialPoints object
pointsSP <- SpatialPoints(pointsDF,
proj4string=CRS("+proj=longlat +datum=WGS84"))
# Use 'over' to get _indices_ of the Polygons object containing each point
indices <- over(pointsSP, counties_sp)
# Return the county names of the Polygons object containing each point
countyNames <- sapply(counties_sp@polygons, function(x) x@ID)
countyNames[indices]
}
# Test the function using points in Wisconsin and Oregon.
testPoints <- data.frame(x = ResVVV$longitude, y = ResVVV$latitude)
county_list<- latlong2county(testPoints)
county_list_data<-as.data.frame(county_list)
VRes<-cbind(ResVVV,county_list_data)
VRes<-VRes %>%
na.omit
#unique(VRes$county_list)
county_name <- as.character(VRes$county_list)
# Remove all before and up to ",":
county_name2 <- gsub(".*,","",county_name)
data <- cbind(county_name2,VRes)
data$county_list <- NULL
#unique(data$county_name2)
Income=read.csv("data\\Income.csv",stringsAsFactors=F)
Income<-Income %>%
rename(
county_fips = State...County.Name,
county_id=County.ID,
povertyrate=All.Ages.in.Poverty.Percent
)
Income$county_id=as.character(Income$county_id)
```
How is the distribution of vegetarian restaurants in New York State? Does poverty rate relate to that distribution? To dive into those questions, we combine two datasets (one contains NY restaurants from datafiniti and another comes from U.S.Income & Poverty rate dataset) together for visualization.
### Plot income(poverty) map
```{r}
counties_sf <- get_urbn_map("counties", sf = TRUE)
counties_sf<-counties_sf %>%
filter(state_name == "New York")
spatial_data <- left_join(counties_sf,
Income,
by=c("county_fips"="county_id"))
ggplot() +
geom_sf(spatial_data,
mapping = aes(fill = povertyrate),
color = "#ffffff", size = 0.25) +
labs(fill = "povertyrate")
```
```{r}
data1 <- st_as_sf(data, coords = c("longitude", "latitude"),
crs = 4326, agr = "constant")
g<-ggplot(data=spatial_data)+geom_sf(mapping = aes(fill = povertyrate))+geom_sf(data=data1,size = 4, shape = 23, fill = "darkred")+theme_map()+theme(legend.position="right")
g1<-ggplotly(g) %>%
highlight(
"plotly_hover",
selected = attrs_selected(line = list(color = "black"))
)
g1
```