Rachel Boey
Rachel Boey

Reputation: 1

plot data points on googlemap in R

I extracted tweets from twitter using searchtwitter function and created a csv file containing the columns "longitude" and "latitude" and I created a variable "tweets" to read the csv file. Each tweet/row has longitude and latitude data. I want to plot the location of the tweets onto the google map of Singapore.

How do I plot the points on the google map that I created using PlotOnStaticMap function? This is the code to achieve my google map:

sgmap<-GetMap(center="Singapore",zoom=11)
PlotOnStaticMap(sgmap)
points(tweets$longitude,tweets$latitude,col="red",cex=.6)

I've also tried this code:

sgmap<-GetMap(center="Singapore",zoom=11)
PlotOnStaticMap(sgmap,cex=.6,col="red",FUN=points,add=F)
points(tweets$longitude,tweets$latitude,col="red",cex=.6)

and:

sgmap<-GetMap(center=c(1.352083,103.8198),zoom=11,destfile="map.png",maptype="satellite")
PlotOnStaticMap(lat=tweets$latitude,lon=tweets$longitude,zoom=11,cex=.6,col="red",FUN=points)

Upvotes: 0

Views: 3076

Answers (1)

jazzurro
jazzurro

Reputation: 23574

Here is another way to do this task using ggmap and ggplot2. You download a map using ggmap then plot data points on the map using geom_point in ggplot2.

library(ggmap)
library(ggplot2)

sing <- get_map(location = "singapore", color = "bw",
                zoom = 11, maptype = "toner", source = "google")

# This is a pseudo tweets data frame including long and lat only
set.seed(12)
foo <- data.frame(long = runif(300, 103.68, 104),
                  lat = runif(300, 1.3, 1.42))

ggmap(sing) +
geom_point(data = foo, aes(x = long, y = lat), color = "red")

enter image description here

Upvotes: 1

Related Questions