【问题标题】:Create a map that differentiates clusters by different colors创建一个用不同颜色区分集群的地图
【发布时间】:2022-06-12 23:14:37
【问题描述】:

我将在下面插入两个执行相同操作的代码。我这样做是因为在一个中我使用raster 函数生成地图,另一个使用ggplot 函数。这些代码之一可能更容易帮助解决此问题。这些代码的想法是生成带有点的地图,这些点通过它们所在的集群来区分。正如您在生成的输出中看到的那样,集群由不同的颜色区分。在这种情况下,我有两个集群,它们是:1 和 2。但是,我不想像以前那样区分点,但我想在集群的区域中显示不同的颜色,根据这个例子下面的地图,代表了我想要的想法。

请注意地图上的集群 1 区域为绿色,集群 2 区域为蓝色。所以这与我想做的事情相似。你能帮帮我吗?

我想要的类似地图:

第一个代码:

    library(rgdal)
    library(sf)
    library(raster)
    library (dplyr)
    
    temp <- tempfile()
    temp2 <- tempfile()
    download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp)
    
    
    unzip(zipfile = temp, exdir = temp2)
    shp <- readOGR(temp2)
    shp_subset <- shp[shp$NM_MUNICIP == "CASTRO",]
    
    #Points
   Points_properties<-structure(list(Latitude = c(-24.781624, -24.775017, -24.769196, 
     -24.761741, -24.752019, -24.748008, -24.737312, -24.744718, -24.751996, 
     -24.724589, -24.8004, -24.796899, -24.795041, -24.780501, -24.763376, 
     -24.801715, -24.728005, -24.737845, -24.743485, -24.742601, -24.766422, 
     -24.767525, -24.775631, -24.792703, -24.790994, -24.787275, -24.795902, 
     -24.785587, -24.787558, -24.799524), Longitude = c(-49.937369, 
     -49.950576, -49.927608, -49.92762, -49.920608, -49.927707, -49.922095, 
     -49.915438, -49.910843, -49.899478, -49.901775, -49.89364, -49.925657, 
     -49.893193, -49.94081, -49.911967, -49.893358, -49.903904, -49.906435, 
     -49.927951, -49.939603, -49.941541, -49.94455, -49.929797, -49.92141, 
     -49.915141, -49.91042, -49.904772, -49.894034, -49.86651), cluster = c("1", "1", 
     "1", "1", "2", "2", "2", "2", "2", "2", "1", "1", "1", "1", "1", 
     "1", "2", "2", "2", "2", "1", "1", "1", "1", "1", "1", "1", "1", 
     "1", "1")), row.names = c(NA, -30L), class = c("tbl_df", "tbl", 
     "data.frame"))
    
#Generate map

marsize <- .2
par(mar = rep(marsize,4))
z <- .4
shp_subset_cropped <- raster::crop(shp_subset,
                                   extent(c(
                                     (extent(shp_subset)@"xmin"*(1-z)+z*min(Points_properties$Longitude)),
                                     (extent(shp_subset)@"xmax"*(1-z)+z*max(Points_properties$Longitude)),
                                     (extent(shp_subset)@"ymin"*(1-z)+z*min(Points_properties$Latitude)),
                                     (extent(shp_subset)@"ymax"*(1-z)+z*max(Points_properties$Latitude))
                                   )))
plot(shp_subset_cropped)

pp1 <- filter(Points_properties,
          cluster==1)

pp2 <- filter(Points_properties,
          cluster==2)

points(x = pp1$Longitude,
       y= pp1$Latitude, col = "red",pch = 16,cex=1)
points(x = pp2$Longitude,
       y= pp2$Latitude, col = "blue",pch = 16,cex=1)

生成的输出 1

第二个代码:

library(rgdal)
library(ggplot2)
library(sf)

temp <- tempfile()
temp2 <- tempfile()
download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip",temp)
unzip(zipfile = temp, exdir = temp2)
shp <- sf::read_sf(temp2)
shp_subset <- shp[shp$NM_MUNICIP == "CASTRO",]



#Points
Points_properties<-structure(list(Latitude = c(-24.781624, -24.775017, -24.769196, 
                                               -24.761741, -24.752019, -24.748008, -24.737312, -24.744718, -24.751996, 
                                               -24.724589, -24.8004, -24.796899, -24.795041, -24.780501, -24.763376, 
                                               -24.801715, -24.728005, -24.737845, -24.743485, -24.742601, -24.766422, 
                                               -24.767525, -24.775631, -24.792703, -24.790994, -24.787275, -24.795902, 
                                               -24.785587, -24.787558, -24.799524), Longitude = c(-49.937369, 
                                               -49.950576, -49.927608, -49.92762, -49.920608, -49.927707, -49.922095, 
                                               -49.915438, -49.910843, -49.899478, -49.901775, -49.89364, -49.925657, 
                                               -49.893193, -49.94081, -49.911967, -49.893358, -49.903904, -49.906435, 
                                               -49.927951, -49.939603, -49.941541, -49.94455, -49.929797, -49.92141, 
                                               -49.915141, -49.91042, -49.904772, -49.894034, -49.86651), 
                                               cluster = c("1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "1", "1", "1", "1", "1", 
                                               "1", "2", "2", "2", "2", "1", "1", "1", "1", "1", "1", "1", "1", 
                                               "1", "1")), row.names = c(NA, -30L), class = "data.frame")


#Generate map
ggplot() + 
  geom_sf(data = shp_subset, aes()) +
  geom_point(data = Points_properties, 
             aes(x = Longitude, y = Latitude, color = cluster))+
  coord_sf(xlim = c(min(Points_properties$Longitude)-0.1,
                    max(Points_properties$Longitude)+0.1),
           ylim = c(min(Points_properties$Latitude)-0.1,
                    max(Points_properties$Latitude)+0.1),
           expand = FALSE) +
theme_void()

生成的输出 2

【问题讨论】:

  • 我不清楚你到底想要什么。您是否想要一种基于点的属性值将最终绘图中的多边形分成两部分的方法,以便您可以在第一张图像中绘制类似于标题为“(b)”的图形?
  • 没错,@Spacedman!我的主要想法是通过不同的颜色来区分簇的区域,因此,簇区域 1 是一种颜色,簇区域 2 是另一种颜色。

标签: r ggplot2 cluster-analysis raster rgdal


猜你喜欢
  • 2016-06-14
  • 2021-02-07
  • 2020-08-29
  • 2020-09-09
  • 1970-01-01
  • 2017-08-16
  • 2014-07-28
  • 1970-01-01
  • 2017-11-29
相关资源
最近更新 更多