-
Notifications
You must be signed in to change notification settings - Fork 1
/
rCharts_demo.R
65 lines (56 loc) · 1.64 KB
/
rCharts_demo.R
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
library(rCharts)
library(plyr)
cList <- read.csv("cList.csv")
grouplist <- levels(cList$groupName)
#from data.frame to list
getList <- function (df) {
#for color
groupPool <<- levels(droplevels(df$groupName))
groupNum <<- length(groupPool)
colorPal <<- hue_pal()(groupNum)
L2 <- dlply(df, .(X),.drop=TRUE,
summarize,
Name = clinicName,
Addr = clinicAddr,
group = groupName,
groupRef = groupRef,
longitude=lon, latitude=lat,
fillColor = colorPal[match(groupName, groupPool)],
popup = sprintf("%s <br/> %s", clinicName, groupName)
)
for(i in 1:length(L2)) {
L2[[i]] <- as.list(L2[[i]])
}
return(L2)
}
#get center
getCenter <- function(data) {
center <- c(mean(data$lat), mean(data$lon))
return(center)
}
plotMap <- function(data) {
center <- getCenter(data)
list <- getList(data)
L3 <- Leaflet$new()
L3$set(width=800, height=500)
L3$setView(center,11)
L3$tileLayer(provider = 'MapQuestOpen.OSM')
L3$geoJson(toGeoJSON(list),
onEachFeature = '#! function(feature, layer){
layer.bindPopup(feature.properties.popup)
} !#',
pointToLayer = "#! function(feature, latlng){
return L.circleMarker(latlng, {
radius: 8,
fillColor: feature.properties.fillColor || 'red',
color: '#000',
weight: 0.5,
fillOpacity: 0.7
})
} !#")
L3$legend(position="bottomright", colors = colorPal, labels=groupPool)
L3$fullScreen(TRUE)
return(L3)
}
glist <- getList(cList)
plotMap(cList)