我如何调整我的小叶子代码,只显示点? 类似于我的散点图。我在下面插入了一个可执行的代码,以及一个图比较两个.我真的不知道如何工作与小叶包。如果有人能帮助我,我很感激。
library(leaflet)
library(geosphere)
#database
df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10), Latitude = c(-23.2, -23.6, -23.9, -23.9, -23.6, -23.5, -23.9, -23.9, -23.6, -23.9),
Longitude = c(-49.6, -49.6, -49.6, -49.4, -49.3, -49.9, -49.3, -49.2, -49.6, -49.9)), class="data.frame",row.names = c(NA, -10L))
#clusters
d<-as.dist(distm(df[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, 4)
df$cluster<-clusters
#Map for Scatterplot
suppressPackageStartupMessages(library(ggplot2))
g<-ggplot(df, aes(x=Longitude,y=Latitude, color=factor(clusters)))+ geom_point()
g
#Map using leaflet
example=df
m=leaflet(data=example)%>%
addTiles() %>%
addPolylines(lat=~Latitude,lng = ~Longitude,color="red")
m
使用
addMarkers(lat= ~Latitude, lng = ~Longitude, color= "red")
代替
addPolylines(lat= ~Latitude, lng = ~Longitude, color= "red")
或使用
addCircleMarkers(lat=~Latitude, lng = ~Longitude, color= "red")
为了给来自同一簇的点分配相同的颜色,请使用以下代码
getColor <- function(example) {
sapply(example$cluster, function(cluster) {
if(cluster == 1) {
"blue"
} else if(cluster == 2) {
"green"
} else if(cluster == 3) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(example)
)
leaflet(example) %>% addTiles() %>%
addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(cluster))