spatial classification clustering

i need to project result of my clustering in a geographic cart which i downloaded as shapefile
the problem is i do have an error that i couldn't fix it


shape <-readOGR(dsn = "polygon.shp")
###converting shapefile to a data frame 
#Plotting a Plain Map
shape@proj4string
shp_df <- fortify(shape)

proj4string(shape)
######transform  geographical base of my shapefile 
tx_ll <- spTransform(shape, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
#transformer en data frame 
shp_df1 <- fortify(tx_ll)


shpMap <- ggplot(data = shp_df1, aes(x=long,y=lat)) +
  geom_polygon(aes(group = group), fill="black") +
  coord_equal(xlim = c(1.220, 1.225), ylim = c(47.22	, 47.25)) +
  labs(x = "Longitude ",
       y = "Latitude ",
       title = "Map ",
       subtitle = "Map - Based on the Lat Long in Shape Files")
shpMap

### after that i want to do the same cart but with the projected classes of k_means clustering 
km <- kmeans(tablevariable[,1:5],5 , nstart=20)
classes<-as.data.frame(km$centers)


shpMap1 <- ggplot(data  = shp_df1, aes(x=long,y=lat)) +
  geom_polygon(aes(group = group), fill="grey80") +
  geom_point( aes(x =x, y = y), data = classes) +
  coord_equal(xlim = c(1.220, 1.224), ylim = c(47.22	, 47.25)) +
  labs(x = "Longitude",
       y = "Latitude ",
       title = "Map ",
       subtitle = "Map - Based on the Lat Long in Shape Files")
shpMap1

but it gives me that error 
Error: Aesthetics must be either length 1 or the same as the data (35968): x, y
can someone explain to me i don't know where is the problem

Hello Morena & welcome :slight_smile:

If I read your question correctly the problem you are facing is drawing a polygon (read in as a shapefile) and placing a number of points on it.

Your code is not exactly reproducible without your data (shapefile and tablevariable) but you might consider this workflow; it is based on {sf} package and current {ggplot2}, which implements geom_sf geom that greatly simplifies work with spatial objects.

I am using shapefile of North Carolina for no other reason other that it is shipped with {sf} package by default. The two cities are just an example.

library(sf)
library(ggplot2)

nc <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package

pts <- data.frame(name = c("Raleigh", "Greensboro"),
                  x = c(-78.633333, -79.819444),
                  y = c(35.766667, 36.08))

ggplot() +
   geom_sf(data = nc) + # polygon of North Carolina
   geom_point(data = pts, aes(x = x, y = y), color = "red") # a tale of two cities :)

The plot can be much improved with labels, colors and what not; I am choosing to omit this for sake of clarity.

3 Likes

yeah thank you so much for your replying but my problem is with result of clustering i can do clustering on my table byt i want this result to show up on my cart , and all my points of the table should be projected as classes in my cart, that why in my previous code i called kmeans

The error message indicates you have a problem with combining geom_polygon and geom_point with data of different length.

My suggestion is read the shapefile via {sf} package and draw it via geom_sf - you would avoid the hassle of fortify and could keep the rest of your code (provided the clusters are in WGS84, as is likely).

I'd agree with @jlacko's suggestion of trying to move to the sf package, from experience it will improve your workflow.

However, I think your problem is related to the clustering bit. What variables are in tablevariable and classes? Take a look at names(DF) and head(DF), substituting each of tablevariables[,1:5] and classes for DF. I suspect there are no variables called x and y, but in the absence of a reprex, we're a bit in the dark here.

Note that, using clustering does not mean you can't work with the sf package, you could create an sf object from classes, see ?st_to_sf before adding it to your map with geom_sf.

Ron.

@ron actually i have in my table x and y i mean lat and long so i used cbind to add the column of classes in my table the question is ican do k-means and class my result on the map that i have just using sf without ggplot2 if yes could u please give me an example how to do that ??

@jlacko thank you i ll try to do what did you recommend i hope it works :smile:

You can do k-means clustering and then plot the results on your map using sf and ggplot2.

I'm afraid I don't have time to do this at present.

You have your data, once you have the output from kmeans (classes) convert it to an sf object using st_to_sf (you will need to specify the columns containing the lat/lon info, and - I think - set the CRS to the same as your shapefile/map, but these are options to the st_ato_sf function) and then add it to the map using geom_sf. Looking at @jlacko's example, that would replace the geom_point call with a second geom_sf call. Give it a go. If you get stuck, create a reprex and ask about it.

Ron.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.