I want to plot a map with some points on it. I tried this code:
lon <- c(103.25,103.28)
lat <- c(3.80, 3.78)
df <- as.data.frame(cbind(lon,lat))
Getting the map:
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 12,maptype = "satellite", scale = 3)
Plotting the map with some points on it:
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8),size = 5, shape = 21) +guides(fill=FALSE, alpha=FALSE, size=FALSE)
Based on this code, the same color of points appear. My question is, I want to create multiple color of points on the map. Kindly assist, your help is highly appreciated. Thank you.
You need to add a categorical variable (what should the colors express?) to govern the color aesthetics:
#create some dummy data
df$coloringCategory <- rep(c("A","B"),length(df$lat)/2)
#in ggplot include the categorical variable
geom_point(data = df, aes(x = lon, y = lat, color= coloringCategory, alpha = 0.8),size = 5, shape = 21)
Related
Questions about map legend editing exist (e.g.), but not exactly what I need.
Using ggmap, how do I select points in a map and add annotations superimposed on the map? Take the following code:
Map <- get_map(location = 'Santiago, Chile', zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),lat=c(-34.36935,-34.29322))
Map_Points <- Map + geom_point(data = Points,aes(x=lon,y=lat,size=6))
So now I have a nice map with a few points. How do I write some annotation near one of the points?
Quite straightforward:
Code
library(ggrepel) # for the auto-repelling label
Map +
geom_point(data = Points,
aes(x = lon, y = lat),
size = 3) +
geom_label_repel(data = Points,
aes(x = lon, y = lat, label = name),
size = 3,
vjust = -2,
hjust = 1)
Data
library(tmaptools) # for the geocode lookup
library(ggmap)
santiago_coords <- rbind(as.numeric(paste(geocode_OSM("Santiago, Chile")$coords)))
Map <- get_map(location = santiago_coords, zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),
lat=c(-34.36935,-34.29322),
name=c("Location One", "Location Two"))
With help of ggmap and plot I want to show the centers of states on the map. The result should be something like this
I tried this block of code but is doesnt show above map
data(state)
cen_df <- as.data.frame(state.center)
library(ggmap)
library(ggplot2)
d <- data.frame(lat = cen_df[2],
lon = cen_df[1])
US <- get_map("united states", zoom = 12)
p <- ggmap(US)
p + geom_point(data = d, aes(x = lon, y = lat), color = "red", size = 30, alpha = 0.5)
ggplot_build(p)
But it shows something lie this:
Any help?
I modified your code as follows. The zoom should be 4. It is also better to use base_layer argument to put your ggplot2 object.
data(state)
library(ggmap)
library(ggplot2)
d <- data.frame(lat = state.center$y,
lon = state.center$x)
US <- get_map("united states", zoom = 4)
p <- ggmap(US, base_layer = ggplot(data = d)) +
geom_point(aes(x = lon, y = lat), color = "red", size = 2, alpha = 0.5)
p
Is it possible to use ggplot2 / ggmap to plot geom_points randomly within a given spatial area defined by a shapefile?
I considered geom_jitter, however I need the plots to be randomly distributed and not cross spatial borders.
Sample data shamelessly borrowed from #matthiash here.
library(rgdal)
library(ggmap)
# Get shapefile with Drammen municipality borders
tmpzip<-tempfile()
tmpdir<-tempfile()
dir.create(tmpdir)
download.file("http://www.kartverket.no/Documents/Kart/N50-N5000%20Kartdata/33_N5000_shape.zip",tmpzip)
unzip(tmpzip, exdir=tmpdir)
kommune <- readOGR(dsn=tmpdir, layer="NO_AdminOmrader_pol")
kommune<-kommune[kommune$NAVN=="Drammen",]
kommune<-spTransform(kommune, CRS("+init=epsg:4326"))
dat<-fortify(kommune)
#get the base map
map <- get_map(location = "Drammen",
maptype = "watercolor", source = "stamen", zoom = 11)
Below code plots the base map with region id 154 from the shapefile plotted on top.
ggmap(map, extent = "normal", maprange = TRUE)+
geom_polygon(data = dat,
aes(long, lat, group = group),
fill = "orange", colour = "red", alpha = 0.2)
What I'd like to do is plot 10 points randomly within the shapefile region defined by dat$id==154
Ok, I sorted it out. The solution is in spsample() in package "raster".
d<-data.frame(id=NA,x=NA,y=NA)
l<-data.frame(id=154,n=10)
for (i in unique(l$id)){
temp<-spsample(kommune[which(kommune$OBJECTID==i),],n=l[l$id==i,"n"],type="random")
temp<-as.data.frame(temp)
temp$id<-i
d<-rbind(d,temp[,c("id","x","y")])
}
d<-d[-1,] #drop the first empty row
ggmap(map, extent = "normal", maprange = T)+
geom_polygon(data = dat,
aes(long, lat, group = group),
fill = "blue", colour = "yellow", alpha = 0.1)+
geom_point(aes(x = x, y = y), data = d[which(d$id==154),], alpha = .9,show.legend = T)
I have a dataset like
latitude longitude Class prediction
9.7 21.757 244732 1
12.21 36.736 112206 0
-15.966 126.844 133969 1
Now i am trying to group all '1' at prediction column and take their latitude and longitude, later i want to display the all points on a single map.
Actually the code i wrote its takes each '1' on prediction column and takes lat and long respectively and display one point on map each time. But I want to collect all lat and long where prediction is 1 and display all points on a one map.
library(ggplot2)
library(ggmap) #install.packages("ggmap")
#data set name testData1
for (i in 1:100){
if (testData1$prediction[i]==1) {
lat <- testData1$latitude[i]
lon <- testData1$longitude[i]
df <- as.data.frame(cbind(lon,lat))
# getting the map
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4,
maptype = "satellite", scale = 2)
# plotting the map with some points on it
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)
}
}
I think you're overcomplicating things. You could simply subset df like so:
ggmap(mapgilbert) +
geom_point(data = subset(df, prediction == 1), aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
guides(fill = FALSE, alpha = FALSE, size = FALSE)
I am working with ggmap. the goal is to plot coordinate points on the map and label the points with their names. I have data frame with name, longitude and latitude.
The data looks like:
df <- structure(list(Station.Area = c("Balbriggan", "Blanchardstown",
"Dolphins Barn", "Donnybrook", "Dun Laoghaire", "Finglas"), Latitude = c(53.608319,
53.386813, 53.333532, 53.319259, 53.294396, 53.390325), Longitude = c(-6.18208,
-6.377197, -6.29146, -6.232017, -6.133867, -6.298401)), .Names =c("Station.Area","Latitude", "Longitude"), row.names = c(NA, 6L), class = "data.frame")
The code I wrote is as below:
library(ggmap)
library(ggplot2)
dub_map <- get_map(location = "Dublin", zoom = "auto", scale="auto", crop = TRUE, maptype = "hybrid")
ggmap(dub_map) +`
geom_point(data = df, aes(x = Longitude, y = Latitude,
fill = "green", alpha =` `0.8, size = 5, shape = 21)) +`
guides(fill=FALSE, alpha=FALSE, size=FALSE)+
geom_text(label=df$Station.Area)+
scale_shape_identity()
But i am getting
Error: Aesthetics must be either length 1 or the same as the data (4): label
I have tried to put various aesthetics in geom_text like size,color,x & Y but it still gives out same error.
Am i doing it correctly for my goal? Please help.
Getting this without geom_text now I just want to label the points
There are a couple of things not quite right in your code.
For the geom_point you only want the x and y in your aes. The other arguments
should be outside, giving
geom_point(data = df, aes(x = Longitude, y = Latitude),
fill = "green", alpha =0.8, size = 5, shape = 21)
Also the label for the geom_text should be inside aes. However,
as there is no data, x or y at a higher level, then geom_text
will not find the label variable or the positions of where to place the labels. So you also need to include these in the call
geom_text(data=df, aes(x = Longitude, y = Latitude, label=Station.Area))
However, you can omit some of this repetition by using the base_layer argument
of ggmap:
ggmap(dub_map,
base_layer = ggplot(data=df, aes(x = Longitude,
y = Latitude,
label=Station.Area))) +
geom_point(fill = "green", alpha =0.8, size = 5, shape = 21) +
geom_text()