在R中以不同的标签和颜色绘制地图

时间:2019-03-13 12:49:24

标签: r ggplot2 ggmap

这是我整理数据以绘制地图的方式。我希望所有带有split标签Night的点都具有一种颜色(比如蓝色),所有带有标签Day的点都具有不同的颜色(不能请参见下面的示例中的Day):

> head(times)
              DateTime  Latitude Longitude split
1: 2018-06-18 03:01:00 -2.434901  34.85359 Night
2: 2018-06-18 03:06:00 -2.434598  34.85387 Night
3: 2018-06-18 03:08:00 -2.434726  34.85382 Night
4: 2018-06-18 03:12:00 -2.434816  34.85371 Night
5: 2018-06-18 03:16:00 -2.434613  34.85372 Night
6: 2018-06-18 03:20:00 -2.434511  34.85376 Night

我一直在尝试遵循和修改在线其他帖子中的代码,但是由于我希望用颜色标记我的观点,因此遇到了麻烦。这是我到目前为止所走的距离,现在我只需要一些帮助来绘制点即可:

# creating a sample data.frame with your lat/lon points
lon <- times$Longitude
lat <- times$Latitude
df <- as.data.frame(cbind(lon,lat))

# getting the map
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 15,
                      maptype = "satellite", scale = 2)

# plotting the map, but don't know how to label points according to information on column "split"
ggmap(mapgilbert) +
  geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 3, shape = 21) +
  guides(fill=FALSE, alpha=FALSE, size=FALSE)

无论如何,任何输入都是值得的。

欢呼

1 个答案:

答案 0 :(得分:0)

您可以尝试传单:

times$color <- ifelse(times$split == "Night", "#000cff", "#ffa500")

lon <- times$Longitude
lat <- times$Latitude
split <- times$split
color <- times$color
df <- cbind(lon,lat,split,color)

m <- leaflet() %>%
    addTiles() %>%  # Add default OpenStreetMap map tiles
    addCircleMarkers(df, lng = lon, lat = lat, radius = 5, color = color,
               weight = 3, opacity = 1.0, fill = TRUE, fillColor = color,
               fillOpacity = 0.5, popup=split)
m

map with yellow/blue for day/night