简体   繁体   English

如何在地图上添加根据变量值着色的点?

[英]how to add points, colored according to the value of a variable, to a map?

Using these instructions: win.graph() 使用这些指令:win.graph()

map("usa")                       
map("usa",col='white',fill=T, xlim=c(-73.7 ,-71.52), ylim=c(38.6,40.92))
points.geodata(x=dat_zero,coords=dat_zero$coords,dat_zero$data,pt.divide="quintiles",
               col=1:5,xlim=c(-73.7 ,-71.52), ylim=c(38.6,40.92),add.to.plot = T)

not from mistakes but does not do it. 不是因为犯错,而是没有做到。

welcome to SO. 欢迎来到。 Generally it's a good idea to include what packages you may have loaded and more description of the problem itself. 通常,最好包括您可能已加载的软件包以及问题本身的更多说明。

Here's an approach using plotly . 这是使用plotly的方法。 I'm using ggplot2::map_data() to generate some sample data (larger dataset) and show how it works: 我正在使用ggplot2::map_data()生成一些示例数据(较大的数据集)并显示其工作方式:

library(ggplot2)
library(plotly)

dat <- map_data(map = 'county')

# map_data() is a large dataset, I'm limiting the map to 50 observations
# the coords$value field is the variable that determines the color of the mapped point
coords <- dat[sample(sample(x = 1:nrow(dat), size = 50, replace = T)), ]
coords$value <- rnorm(n = nrow(coords), mean = 10, sd = 3)

# some code to let plotly know we're plotting a map (projection etc.) 
g <- list(
  scope = 'usa',
  projection = list(type = 'Mercator'),
  showland = TRUE,
  landcolor = toRGB("gray85"),
  subunitwidth = 1,
  countrywidth = 1,
  subunitcolor = toRGB("white"),
  countrycolor = toRGB("white")
)


plt <- plot_geo(locationmode = 'USA-states', sizes = c(1, 250), data = coords) %>%
  add_markers(x = ~long, y = ~lat, color = ~value) %>%
  layout(title = 'County Map', 
         geo = g)

OUTPUT 输出值 在此处输入图片说明

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

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