简体   繁体   English

Map 中的标绘点不会因价格而异

[英]Plotted points in Map doesn't vary based on the price

I plotted some data points on a map.我在 map 上绘制了一些数据点。 The Map is the bounty King in Washington. Map 是华盛顿的赏金之王。 I plotted them on the map successfully.我成功地将它们绘制在 map 上。 My dataset consists of a column called prices.我的数据集包含一个名为价格的列。 Their values range in between 0-800000.它们的值范围在 0-800000 之间。 I created a new column, called MapColor to print the value either as red or blue based on the price value.我创建了一个名为 MapColor 的新列,以根据价格值将值打印为红色或蓝色。 If price> 400000 red, else blue.如果价格> 400000 红色,否则为蓝色。

Now when plotting the points in the map, if MapColor is red I need to map it as red points and if not black.现在在 map 中绘制点时,如果 MapColor 为红色,我需要将 map 绘制为红色点,如果不是黑色。 This is how I tried it.这就是我尝试的方式。 But the colors are plotting black only.但是 colors 仅绘制黑色。 This is what I tried这是我尝试过的

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

在此处输入图像描述

Please let me know if this is what you had in mind.如果这是您的想法,请告诉我。

I think long and lat for those example points were reversed.我认为这些示例点的 long 和 lat 是相反的。 I set the colors as either red or blue as per the description.根据描述,我将 colors 设置为红色或蓝色。

It maps the state polygon, then county, then adds the points using color = House$MapColor .它映射 state 多边形,然后是县,然后使用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()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM