r - 地图中的标绘点不会因价格而异
问题描述
我在地图上绘制了一些数据点。地图是华盛顿的赏金之王。我成功地将它们绘制在地图上。我的数据集包含一个名为价格的列。它们的值范围在 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
解决方案
如果这是您的想法,请告诉我。
我认为这些示例点的 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()
推荐阅读
- reactjs - 如何模拟从 Jest / 酶测试中的下拉列表中选择?
- ms-access - MS-Access VBA:将变量从一种形式传递到另一种形式,导致运行时错误
- c# - c#如何从另一个类调用一个类中的接口方法?
- java - 带有本机 Android 应用程序的 Azure SignalR 服务?
- javascript - 如何在没有状态的情况下在 React 中填充 DropdownList 选项?
- sql - 如何使用均值、中位数和众数创建新的 SQL 表?
- python-3.x - Python:反斜杠和正号是什么意思?
- python - 如何在 Outlook 和 Python 中从辅助帐户发送电子邮件
- android - 选择另一个 TextInputLayout 样式
- python - Apache 和 mod_wsgi