首页 > 解决方案 > 地图中的标绘点不会因价格而异

问题描述

我在地图上绘制了一些数据点。地图是华盛顿的赏金之王。我成功地将它们绘制在地图上。我的数据集包含一个名为价格的列。它们的值范围在 0-800000 之间。我创建了一个名为 MapColor 的新列,以根据价格值将值打印为红色或蓝色。如果价格> 400000 红色,否则为蓝色。

现在在地图中绘制点时,如果 MapColor 为红色,我需要将其映射为红色点,如果不是黑色。这就是我尝试的方式。但颜色仅绘制黑色。这是我尝试过的

long <- c(47.5112,47.7210   ,47.3684)
lat <- c(-122.257, -122.319, -122.031)
price <- c(287655,456355,662500,234563)

House <- data.frame(long, lat, price)
House$MapColor  <- ifelse(House$price >=400000, "red", "black")

col <- as.character(House$MapColor)

states <- map_data("state")

wa_df <- states %>%
  filter(region == "washington", subregion == 'king')



counties <- map_data("county")
wa_county <- counties %>%
  filter(region == "washington")

wa_base <-
  ggplot(data = wa_df,
         mapping = aes(x = long, y = lat, group = group)) +   geom_point(data = House,aes(x = long, y = lat),size = 0.5,inherit.aes = FALSE) +
  coord_fixed(1.3) +scale_color_manual(values=col)+
  geom_polygon(color = "black", fill = "gray")
#geom_point(data = House, mapping = aes(x = long, y = lat), color = "red")

wa_base + theme_nothing() +
  geom_polygon(data = wa_county, fill = NA, color = "black") +
  geom_polygon(color = "black", fill = NA)  # get the state border back on top

在此处输入图像描述

标签: r

解决方案


如果这是您的想法,请告诉我。

我认为这些示例点的 long 和 lat 是相反的。我根据描述将颜色设置为红色或蓝色。

它映射州多边形,然后是县,然后使用添加点color = House$MapColor

library(ggplot2)
library(ggmap)

lat <- c(47.5112, 47.7210, 47.3684)
long <- c(-122.257, -122.319, -122.031)
price <- c(287655, 456355, 662500)

House <- data.frame(long, lat, price)

House$MapColor <- ifelse(House$price >= 400000, "red", "blue")

wa_df <- map_data("state") %>%
  filter(region == "washington")

wa_county <- map_data("county") %>%
  filter(region == "washington")

ggplot(data = wa_df, mapping = aes(x = long, y = lat, group = group))+  
  geom_polygon(color = "black", fill = NA)+
  geom_polygon(data = wa_county, fill = NA, color = "black")+
  geom_point(data = House, mapping = aes(x = long, y = lat), color = House$MapColor)+
  coord_fixed(1.3)+
  theme_nothing()

带有选定颜色点的华盛顿州


推荐阅读