【问题标题】:R ggmap - Display Clusters on a MapR ggmap - 在地图上显示集群
【发布时间】:2018-06-28 23:30:36
【问题描述】:

我进行了聚类分析,现在我想在地图上显示不同的组。

我做了一个我拥有的数据框的例子(原来的太大了)。

x1 <- c("Station1", "Station2", "Station3", "Station4", "Station5", "Station6", "Station7", "Station8")
x2 <- c(1, 2, 1, 3, 4, 1, 5, 3)
x3 <- seq(13.0, 15.3, length=8)
x4 <- seq(45.0, 48.0, length=8) 
x5 <- c("rural", "urban", "rural", "suburban", "urban", "suburban", "ubran", "rural")

TestCluster = data.frame(Station = x1, Cluster = x2, LON = x3, LAT = x4, Area = x5)

>TestCluster
  Station Cluster      LON      LAT     Area
1 Station1       1 13.00000 45.00000    rural
2 Station2       2 13.32857 45.42857    urban
3 Station3       1 13.65714 45.85714    rural
4 Station4       3 13.98571 46.28571 suburban
5 Station5       4 14.31429 46.71429    urban
6 Station6       1 14.64286 47.14286 suburban
7 Station7       5 14.97143 47.57143    ubran
8 Station8       3 15.30000 48.00000    rural

我想在地图上显示每个站点并为每个集群使用不同的颜色。

我正在使用此代码,但我总是收到 2 条不同的错误消息。

library(ggmap)
library(ggplot2)

Europe <- get_map(location = "Europe", zoom = 4)
p = ggmap(Europe)

p = p + geom_point(data = TestCluster, aes(LON, LAT, color = Cluster), size = 1) + 
    scale_color_manual(name = "Cluster", values = c("1" = "yellow",
                                                    "2" = "orange",
                                                    "3" = "red",
                                                    "4" = "green",
                                                    "5" = "blue"))
p

我收到以下错误消息:

Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.

Error: Continuous value supplied to discrete scale

我读到第二个错误应该用 color = as.factor(Cluster) 命令解决,但它对我不起作用。

知道为什么它不起作用吗?

【问题讨论】:

  • 将其更改为 color=as.factor(Cluster) 确实为我消除了第二个错误。如果我运行 p = p + geom_point(... 命令两次,我会收到第一个错误。

标签: r google-maps maps cluster-analysis ggmap


【解决方案1】:

我现在才看到这个问题。正如杰森所说,您想将Cluster 视为因素。这解决了第二个问题。至于第一条警告信息,我无法复制它。我成功地制作了一张地图。我在下载地图时更改了位置,因为您的数据点在哪里。您可能想检查您使用的是哪个版本的 ggplot2 和 ggmap。这可能是首先要检查的事情之一。就我而言,我使用了 ggmap_2.6.1ggplot2_2.2.1 和 R 版本 3.4.3。

library(ggplot2)
library(ggmap)

Europe <- get_map(location = "Munich", zoom = 6)

ggmap(Europe) +
geom_point(data = TestCluster, aes(x = LON, y = LAT, color = factor(Cluster)), size = 3) +
scale_color_manual(name = "Cluster", 
                   values = c(`1` = "yellow",
                              `2` = "orange",
                              `3` = "red",
                              `4` = "green",
                              `5` = "blue"))

【讨论】:

  • 非常感谢!我现在自己找到了解决方案。不知何故存在问题,因为我使用数字而不是字符串。当我在“Cluster1”中重命名“1”时就没有问题了!
  • @Essi 在您的原始代码中,数字是数字,而不是字符。因此,如果您键入“1”,那将不起作用。如果您想将数字用作数字,则需要使用 `` 。我很高兴你找到了自己的路!
  • 您好,谢谢!我尝试了 is.numeric,但它仍然出现问题,我只少了 1 个错误。 ://
  • @Essi 在我的机器上,两个版本都给了我上图。
  • 非常感谢!你似乎很擅长这些事情。我只是有另一个问题。我可以通过 PM 以某种方式与您联系吗?我已经在这里问过了:stackoverflow.com/questions/48614328/…
猜你喜欢
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
相关资源
最近更新 更多