Geom_segment plot on ggmap object in R-lines not appearing

I want to build strings (to be precise: an element geom_segment) on my object ggmap(which is an object ggplot2, as I understand it).

I am using the following code:

library(ggmap)

mapImageData <- get_map(location = c(lon = (16.8 + ( 17.2-16.8)/2), 
                                     lat = (51 + (51.2-51)/2)),
                        color = "color",
                        source = "google",
                        maptype = "roadmap",
                        zoom = 11)

ggmap(mapImageData, extent = "device", ylab = "Latitude", xlab = "Longitude") + 
geom_segment(aes(x = 51, y = 16.8, xend = 51.2, yend = 17.2))

A clear display is drawn:

enter image description here

but no line (from geom_segment) appears . What am I doing wrong?

+4
source share
2 answers

Lattitude values ​​correspond to y values ​​and longitude to x values. So you need to change the x and y values ​​in geom_segment().

ggmap(mapImageData, extent = "device", ylab = "Latitude", xlab = "Longitude") + 
      geom_segment(aes(y = 51, x = 16.8, yend = 51.2, xend = 17.2))
+7
source

geom_path, gps :

gpsData <- read.csv("001.txt")
  lonCenter <- mean(gpsData[[3]], na.rm = TRUE)
  latCenter <- mean(gpsData[[4]], na.rm = TRUE)

  map <- get_map(location = c(lon = lonCenter, lat = latCenter), zoom = 10)

  dataFrame <- structure(
    list(
      taxiId = gpsData[[1]],
      longitude = gpsData[[3]],
      latitude = gpsData[[4]]      
     ),
    .Names = c("id", "longitude", "latitude"), class = "data.frame"
  )

  mapImage <- ggmap(map) + 
    geom_path(
       data = dataFrame, 
       aes(x = longitude, y = latitude, fill = "red", colour = "red", alpha = 1/3),
       size = 3, shape = 21
    ) + 
    guides(
      fill=FALSE, alpha=FALSE, size=FALSE, colour = FALSE
    )

  ggsave(mapImage, filename = "001.png")
+2

Source: https://habr.com/ru/post/1543768/


All Articles