多边形按点绘制而不是区域 ggplot2 和 ggmap in r

Polygon draws pointwise instead of area ggplot2 and ggmap in r

我想画一张类似这样的地图

我的目标是去除不同的点形状并保持其余部分不变。但是,我最终得到了以下情节。

除了多边形,其他都是正确的。如何更改此多边形绘图?

library(data.table)
plot.data <- as.data.frame(fread("plotdata.csv", header = TRUE, sep = ","))

library(ggmap)
library(ggplot2)
map <- get_map(location = "california", maptype = "roadmap", zoom = 6, source = 'google', scale = 1,color = "color")
plot.size = 11
point.size = 0.85
line.size = 0.3

p2 <- ggmap(map) +
  geom_point(data = plot.data,  size = 1,
             aes_string("x", "y", color = as.factor(plot.data$cluster)), alpha = 0.7) +
  scale_shape(solid = T) + 
  geom_polygon(data = plot.data, mapping = aes_string(x = "x", 
                                                       y = "y", 
                                                       colour = as.factor(plot.data$cluster), 
                                                       fill = as.factor(plot.data$cluster)), 
               alpha = 0.3) +
  geom_text(aes(label = "", x = -114, y = 41), size = plot.size-3) +
  xlab(expression(paste("Longitude  [", degree, "]"))) +
  ylab(expression(paste("Latitude [", degree, "]"))) +
  theme_bw() +
  theme(plot.margin = unit(c(1,1,0,0), "lines"), text = element_text(family = "Times"), axis.title = element_text(size = plot.size), axis.text = element_text(size = plot.size), legend.position = "none")
p2

数据在下面link。 https://mega.nz/#!J9JiwD4R!3cEmCvoE1oDvGoYZtl7Pumw95uQtHqtPvrfR3y5mUc4

get_map有时会报错,请继续尝试。最终,它会下载地图。

> dput(plot.data)
structure(list(x = c(-114.62, -115.1, -116.26, -116.7, -115.66, 
-116.02, -116.86, -115.98, -116.22, -115.22, -115.58, -116.42, 
-117.3, -117.46, -116.62, -117.46, -114.74, -114.94, -116.26, 
-117.46, -115.62, -116.26, -116.78, -116.58, -117.22, -117.82, 
-115.42, -118.1, -114.54, -114.7, -115.42, -115.46, -114.82, 
-117.06, -117.1, -117.94, -114.94, -115.86, -117.74, -117.18, 
-115.38, -117.3, -115.42, -115.66, -116.62, -118.94, -114.34, 
-116.38, -116.98, -117.94, -119.14, -118.02, -118.7, -118.94, 
-114.58, -115.34, -116.26, -114.78, -115.5, -114.7, -115.06, 
-115.7, -117.74, -116.06, -116.26, -120.58, -118.02, -119.22, 
-114.54, -116.1, -116.98, -117.46, -115.94, -119.02, -119.34, 
-119.54, -120.5, -115.1, -115.42, -116.46, -116.98, -117.46, 
-115.42, -116.02, -116.54, -117.62, -114.7, -115.38, -116.34, 
-117.54, -117.86, -116.66, -120.58, -114.9, -117.14, -117.9, 
-119.38, -119.9, -117.3, -120.18, -117.5, -120.1, -119.26, -120.38, 
-120.46, -115.38, -115.86, -117.34, -116.7, -119.9, -119.14, 
-115.82, -116.22, -116.94, -119.3, -116.62, -116.74, -118.74, 
-119.1, -119.26, -119.94, -115.78, -115.86, -119.26, -118.3, 
-120.46, -118.38, -119.66, -116.62, -118.22, -118.3, -121.02, 
-116.5, -117.06, -117.62, -117.74, -118.98, -119.62, -119.98, 
-120.74, -120.9, -115.98, -116.66, -118.06, -118.22, -119.1, 
-117.82, -119.34, -119.54, -117.26, -117.38, -120.34, -120.94, 
-119.66, -120.62, -116.46, -121.42, -116.58, -117.62, -118.7, 
-118.94, -120.46, -119.34, -120.86, -116.54, -117.94, -119.86, 
-117.1, -119.26, -121.14, -117.86, -119.5, -120.62, -120.66, 
-118.46, -119.5, -120.7, -120.74, -119.26, -120.74, -121.58, 
-117.26, -120.9, -121.38, -116.82, -117.74, -118.66, -118.98, 
-120.22, -117.26, -118.1, -117.14, -117.58, -119.26, -120.66, 
-121.18, -118.1, -119.18, -119.26, -117.58, -119.22, -121.06, 
-117.38, -118.54, -120.86, -122.02, -117.7, -119.34, -120.7, 
-120.14, -120.66, -118.1, -119.06, -117.5, -118.02, -119.14, 
-120.74, -121.54, -117.54, -120.02, -121.78, -118.58, -119.66, 
-119.7, -117.82, -118.94, -119.38, -119.7, -118.1, -121.7, -121.06, 
-117.9, -118.1, -118.9, -118.58, -119.46, -120.7, -120.82, -121.22, 
-119.9, -121.5, -122.46, -118.34, -119.42, -119.62, -118.98, 
-119.62, -119.74, -120.62, -121.18, -122.18, -122.54, -122.14, 
-121.94, -120.22, -120.98, -121.14, -122.98, -122.58, -119.38, 
-119.5, -121.06, -121.46, -122.74, -119.1, -121.74, -119.1, -121.46, 
-121.9, -121.22, -119.82, -120.74, -120.98, -121.22, -119.34, 
-123.02, -122.78, -122.1, -122.62, -120.06, -120.98, -120.9, 
-123.34, -120.38, -120.66, -120.46, -123.66, -122.46, -120.14, 
-120.1, -120.62, -122.14, -120.82, -121.38, -120.06, -123.18, 
-121.42, -120.58, -120.94, -121.18, -121.62, -121.02, -121.82, 
-122.3, -120.1, -121.34, -121.46, -122.02, -121.74, -121.9, -120.06, 
-120.94, -122.18, -122.66, -123.06, -123.78, -121.5, -120.62, 
-121.3, -120.22, -121.02, -120.3, -120.1, -120.98, -120.98, -121.66, 
-121.82, -121.54, -121.1, -121.06, -122.98, -123.14, -123.9, 
-120.14, -122.3, -120.42, -122.86, -123.46, -122.74, -123.46, 
-121.46, -121.54, -122.42, -122.94, -123.94, -120.46, -121.54, 
-121.94, -120.86, -123.22, -120.54, -120.98, -123.7, -121.42, 
-122.58, -120.82, -121.38, -121.62, -122.06, -120.38, -120.54, 
-120.62, -121.58, -123.1, -123.9, -120.94, -121.74, -122.38, 
-121.78, -123.54, -121.74, -122.46, -122.78, -122.34, -123.26, 
-123.06, -122.94, -120.42, -121.06, -121.1, -120.82, -122.26, 
-123.58, -120.58, -121.82, -123.82, -120.58, -120.62, -122.22, 
-123.74, -120.5, -123.7, -120.74, -122.42, -122.46, -120.78, 
-122.46, -120.42, -123.5, -123.74), y = c(32.81, 32.81, 32.89, 
32.97, 33.09, 33.09, 33.17, 33.21, 33.21, 33.29, 33.37, 33.37, 
33.41, 33.41, 33.45, 33.53, 33.57, 33.57, 33.61, 33.65, 33.69, 
33.73, 33.73, 33.81, 33.81, 33.81, 33.85, 33.85, 33.89, 33.89, 
33.93, 33.97, 34.05, 34.09, 34.09, 34.09, 34.13, 34.17, 34.17, 
34.21, 34.25, 34.25, 34.29, 34.29, 34.29, 34.29, 34.33, 34.33, 
34.37, 34.37, 34.37, 34.41, 34.41, 34.41, 34.45, 34.45, 34.45, 
34.49, 34.49, 34.53, 34.53, 34.57, 34.57, 34.61, 34.61, 34.61, 
34.65, 34.65, 34.69, 34.69, 34.69, 34.69, 34.73, 34.73, 34.73, 
34.73, 34.73, 34.77, 34.77, 34.77, 34.77, 34.81, 34.85, 34.85, 
34.93, 34.93, 34.97, 35.01, 35.01, 35.01, 35.01, 35.05, 35.05, 
35.09, 35.09, 35.09, 35.09, 35.09, 35.13, 35.13, 35.17, 35.21, 
35.29, 35.29, 35.29, 35.33, 35.37, 35.37, 35.45, 35.49, 35.53, 
35.57, 35.57, 35.57, 35.57, 35.69, 35.69, 35.69, 35.69, 35.69, 
35.69, 35.73, 35.73, 35.73, 35.77, 35.77, 35.81, 35.81, 35.85, 
35.85, 35.85, 35.85, 35.89, 35.89, 35.89, 35.89, 35.89, 35.89, 
35.89, 35.89, 35.93, 35.97, 35.97, 35.97, 35.97, 35.97, 36.09, 
36.09, 36.09, 36.13, 36.13, 36.13, 36.13, 36.17, 36.17, 36.21, 
36.25, 36.29, 36.29, 36.29, 36.33, 36.33, 36.37, 36.37, 36.41, 
36.41, 36.41, 36.45, 36.45, 36.45, 36.49, 36.49, 36.49, 36.49, 
36.53, 36.53, 36.53, 36.57, 36.61, 36.61, 36.61, 36.65, 36.65, 
36.65, 36.69, 36.73, 36.73, 36.73, 36.77, 36.81, 36.81, 36.85, 
36.85, 36.85, 36.85, 36.85, 36.89, 36.89, 36.89, 36.93, 36.93, 
36.93, 36.97, 36.97, 36.97, 36.97, 37.01, 37.01, 37.01, 37.05, 
37.05, 37.09, 37.09, 37.17, 37.17, 37.17, 37.17, 37.21, 37.25, 
37.25, 37.29, 37.33, 37.33, 37.33, 37.37, 37.37, 37.37, 37.37, 
37.41, 37.41, 37.45, 37.49, 37.53, 37.53, 37.57, 37.61, 37.61, 
37.61, 37.61, 37.69, 37.69, 37.69, 37.77, 37.77, 37.77, 37.81, 
37.81, 37.81, 37.81, 37.81, 37.81, 37.89, 37.97, 38.01, 38.05, 
38.05, 38.05, 38.05, 38.09, 38.17, 38.17, 38.17, 38.17, 38.21, 
38.25, 38.29, 38.33, 38.33, 38.33, 38.37, 38.41, 38.41, 38.41, 
38.41, 38.53, 38.53, 38.57, 38.61, 38.61, 38.65, 38.65, 38.69, 
38.73, 38.81, 38.89, 38.93, 38.93, 38.97, 39.01, 39.09, 39.13, 
39.13, 39.17, 39.17, 39.21, 39.21, 39.33, 39.41, 39.41, 39.41, 
39.41, 39.45, 39.45, 39.53, 39.57, 39.57, 39.57, 39.61, 39.65, 
39.69, 39.73, 39.73, 39.73, 39.77, 39.77, 39.77, 39.81, 39.85, 
39.85, 39.93, 39.93, 39.97, 40.01, 40.01, 40.05, 40.05, 40.05, 
40.13, 40.17, 40.21, 40.21, 40.21, 40.21, 40.25, 40.25, 40.33, 
40.33, 40.33, 40.37, 40.37, 40.41, 40.41, 40.41, 40.41, 40.45, 
40.49, 40.49, 40.49, 40.61, 40.61, 40.65, 40.65, 40.65, 40.69, 
40.69, 40.73, 40.77, 40.81, 40.81, 40.85, 40.85, 40.85, 40.89, 
40.89, 40.89, 40.93, 40.97, 41.01, 41.09, 41.09, 41.17, 41.17, 
41.17, 41.21, 41.21, 41.25, 41.33, 41.37, 41.37, 41.37, 41.41, 
41.41, 41.49, 41.57, 41.57, 41.57, 41.61, 41.61, 41.61, 41.65, 
41.69, 41.73, 41.77, 41.77, 41.77, 41.85, 41.85, 41.89, 41.93, 
41.97), cluster = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
5L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 3L, 3L, 5L, 3L, 3L, 3L, 3L, 
3L, 3L, 5L, 3L, 3L, 3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L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 
4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L)), .Names = c("x", "y", "cluster"), row.names = c(NA, 
-400L), class = "data.frame")

您可以使用chull(计算一组点的凸包)。

find_hull <- function(df) df[chull(df$x, df$y), ]
hulls <- plyr::ddply(plot.data, "cluster", find_hull)

p2 <- ggmap(map) +
  geom_point(data = plot.data,  size = 1,
             aes_string("x", "y", color = as.factor(plot.data$cluster)), alpha = 0.7) +
  scale_shape(solid = T) + 
  geom_polygon(data = hulls, mapping = aes_string(x = "x", 
                                                  y = "y", 
                                                  colour = as.factor(hulls$cluster), 
                                                  fill = as.factor(hulls$cluster)), 
               alpha = 0.3) +
  geom_text(aes(label = "", x = -114, y = 41), size = plot.size-3) +
  xlab(expression(paste("Longitude  [", degree, "]"))) +
  ylab(expression(paste("Latitude [", degree, "]"))) +
  theme_bw() +
  theme(plot.margin = unit(c(1,1,0,0), "lines"), 
        text = element_text(family = "Times"), 
        axis.title = element_text(size = plot.size), 
        axis.text = element_text(size = plot.size), 
        legend.position = "none")
p2