r - 仅在 R 中的传单包使用的地图上显示点
问题描述
如何调整我的传单代码以仅显示点?类似于我的散点图。我在下面插入了一个可执行代码,以及一个比较两者的图。我真的不知道如何使用传单包。如果有人可以帮助我,我将不胜感激。
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))
推荐阅读
- c# - Windows 窗体测验跟踪单选按钮
- python - 如何使用“upsert”使用 SQLAlchemy 执行批量插入,因此现有密钥被忽略(或更新)并且不会使执行崩溃?
- python - 先前初始化的变量声称“未定义”
- flutter - 如何在不设置路由的情况下导航到新屏幕
- ios - 如何在 Swift 中显示这个 html 结构的 json 响应
- javascript - 将匹配重音字符并将 - 替换为 ' ' 的正则表达式
- python - 带有OOP的python中的KMeans实现?
- php - 当购物车包含延期交货产品时,在 WooCommerce 购物车和结帐页面上显示消息
- amazon-web-services - 显示来自 AWS CloudFront 的 HTTP 400 的错误页面
- react-native - React Native 中身份验证的最佳实践