I have a data frame of lat lons that surround a center point. The lat lons represent two groups. The groups are represented with a 1 or a 0. I can plot around the center point with different shapes
and colors, but then I would like the colors to be more distinct. I would also like to plot two circles around the points, 300 meters, and 600 meters respectively. I have tried working with
Plot circle with a certain radius around point on a map in ggplot2
but have had no luck
a small sample looks like this
lat <- c(42.99052, 42.99085, 42.99046, 42.99081, 42.99197, 42.99122, 42.99154,42.99161, 42.99102, 42.99014, 42.98966, 42.99091, 42.99092, 42.99114 ,42.99000)
lon <-c(78.69961, -78.69871, -78.69878, -78.69868, -78.69825, -78.69929, -78.69784, -78.69960, -78.69904, -78.69918, -78.69998, -78.69746, -78.70145, -78.70020, -78.70010)
response <- c(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1)
data <- data.frame(lat, lon, response)
My center point is
(42.990707, -78.698899)
I get my map without circles and distinct colors like this
library(ggplot2)
library(ggmap)
library(viridis)
#getting map
mapgilbert <- get_map(location = c(lon = -78.698899 , lat=42.990707), zoom = 17,
maptype = "terrain", scale = 2)
ggmap(mapgilbert) +
geom_point(data = data, aes(x = lon, y = lat, color = response , alpha = 1, fill = response),
size = 2, shape = response) +
guides(fill = FALSE, alpha = FALSE, size = FALSE) +
scale_color_viridis(begin = 0, end = .7)
any help would be appreciated.
I got the solution by modifying the answer in
Plot circle with a certain radius around point on a map in ggplot2
center_df<-data.frame(ID=c(1),lon=c(-78.698899),lat=c(42.990707))
make_circles <- function(centers, radius, nPoints = 500){
# centers: the data frame of centers with ID
# radius: radius measured in kilometer
#
meanLat <- mean(centers$lat)
# length per longitude changes with lattitude, so need correction
radiusLon <- radius /111 / cos(meanLat/57.3)
radiusLat <- radius / 111
circleDF <- data.frame(ID = rep(centers$ID, each = nPoints))
angle <- seq(0,2*pi,length.out = nPoints)
circleDF$lon <- unlist(lapply(centers$lon, function(x) x + radiusLon * cos(angle)))
circleDF$lat <- unlist(lapply(centers$lat, function(x) x + radiusLat * sin(angle)))
return(circleDF)
}
myCircles <- make_circles(center_df, 0.2)
myCircles2 <- make_circles(center_df, 0.1)
mapgilbert2<- get_map(location = c(lon = -78.70000 , lat=42.991107), zoom = 16,
maptype = "terrain", scale = 2)
ggmap(mapgilbert2)+
geom_point(data = data, aes(x = lon, y = lat, fill=as.factor(exposure) , alpha = 1), size = 2, pch=21)+
guides(fill=FALSE, alpha=FALSE, size=guide_legend)+
scale_color_viridis(begin = 0, end = .7)+
geom_point(data=myCircles,aes(x=lon,y=lat), color='red')+
geom_point(data=myCircles2,aes(x=lon,y=lat), color='blue')+
scale_fill_manual(values=c("purple", "green"))
Related
I'm trying to use the rayshader package in R to produce an elevation plot with points on the surface (or floating just above) that represent where samples were taken. However, I can't seem to get the points to show up on the map, or when they do, they don't show up where I expect them.
Here's a toy example:
library(raster)
set.seed(1)
x <- raster(ncol=50, nrow=50, xmn=-1, xmx=1, ymn=-1, ymx=1)
res(x) <- .5
x[] <- rnorm(16, -5, 10)
fakepoints <- data.frame(x = c(0, -.5),
y = c(0, 0))
fakepoints$elev <- (raster::extract(x, fakepoints))
x_dat <- data.frame(rasterToPoints(x, spatial = T))
library(rayshader)
library(ggplot2)
e_mat = raster_to_matrix(x)
a <- ggplot()+
geom_tile(data =x_dat, aes(x =x, y = y, fill = layer ))+
scale_fill_gradientn(colors = rev(topo.colors(10)))
height <- plot_gg(a, multicore = TRUE, raytrace = TRUE, width = 7, height = 4,
scale = 300, windowsize = c(1400, 866), zoom = .5, theta = 30, max_error = 0.001,save_height_matrix = T)
render_points(extent = attr(x,"extent"),
size = 10,
color = "black",
heightmap = height,
altitude = fakepoints$elev+.1,
zscale = 1,
offset = 0,
lat = fakepoints$y, long = fakepoints$x,
clear_previous = T)
The points should show up at (0,0) and (-.5, 0), but I can't see to make them appear anywhere.
I am trying to color in specific grid cells within a map to highlight sampling effort.
I can generate the map but then I am unsure how to utilize the data points I have to color in the entire grid cell. The data points I have are for the top left hand corner of a grid cell.
I have tried using stat_density2d (kernel density func), as far as I can work out the issue is I just want the discrete values plotted but currently it is filling in everything in-between.
library(ggOceanMaps)
library(ggOceanMapsData)
dt <- data.frame(lon = c(35, 35, 60, 60), lat = c(-25, -25, -40, -40))
grid_2_colour <- data.frame(lat=c(-29), long=c(50))
basemap(data = dt, bathymetry = TRUE,
lon.interval = 1,
lat.interval = 1,
# bathy.style = "contour_blues",
bathy.border.col = NA,
bathy.size = 0.1,
bathy.alpha = 1) +
stat_density2d(data = grid_2_colour, aes(x = long, y = lat, fill =..density..), geom = 'tile', contour = F)
found a solution:
m1 = basemap(data = dt, bathymetry = TRUE,
lon.interval = 1,
lat.interval = 1,
# bathy.style = "contour_blues",
bathy.border.col = NA,
bathy.size = 0.1,
bathy.alpha = 1)
m1 + stat_density2d(data = grid_2_colour, aes(x = long, y = lat, fill =..density..), geom = 'tile', contour = F)
I have a table containing all the latitudes and longitudes of some locations in a city called queryResult and I do the following:
1 - Get the Raster map of the city[Blackpool for instance]
cityMapRaster = get_map(location = 'Blackpool', zoom = 12, source = 'google', maptype = 'roadmap')
dataToShow <- ggmap(cityMapRaster) + geom_point(aes(x = Longitude, y = Latitude), data = queryResult, alpha = .5, color = "darkred", size = 1)
print(dataToShow)
and this will return the following points on the map
Now I want to draw the outer boundary [city border line] of all these latitude and longitudes similar to the following expected result
Update 1 : Providing input data and applying suggested ahull solution:
ggmap(cityMapRaster) + geom_point(aes(x = Longitude, y = Latitude), data = queryResult, alpha = .5, color = "darkred") + ahull.gg
I applied the ahull solution suggested by #spacedman and #cuttlefish44 and got the following result which is far different than the expected polygon:
You can download the .csv file containing all latitudes and longitudes from the following link : Blackpool Lat,Lon
Googles suggested area boundary looks like the following :
If you don't want a simple convex hull (and the polygon you've drawn is far from convex) then look at alpha-shapes in the alphahull package.
I wrote an example of how to get a polygon from an alpha-shape using that package and some points that make up a complex Norway boundary:
http://rpubs.com/geospacedman/alphasimple
You should be able to follow that to get a polygon for your data, and it might even be simpler now since that was a few years ago.
Here's a reproducible example of how to use chull to calculate a convex hull solution to this. I just generate some random points for queryResult, as you did not provide data.
If you prefer a concave hull boundary, then see the answer from #Spacedman
library(ggmap)
cityMapRaster = get_map(location = 'Blackpool', zoom = 12, source = 'google', maptype = 'roadmap')
extent = attr(cityMapRaster, "bb")
queryResult = data.frame(Longitude = rnorm(200, as.numeric(extent[2] + extent[4])/2, 0.01),
Latitude = rnorm(200, as.numeric(extent[1] + extent[3])/2, 0.02))
boundary = chull(as.matrix(queryResult))
ggmap(cityMapRaster) +
geom_point(aes(x = Longitude, y = Latitude),
data = queryResult, alpha = .5, color = "darkred", size = 2) +
geom_path(aes(x = Longitude, y = Latitude), data = queryResult[c(boundary, boundary[1]),])
I suppose queryResult is x and y datasets. As far as I see, your boundary isn't convex hull, so I used alphahull package.
## example `queryResult`
set.seed(1)
df <- data.frame(Longitude = runif(200, -3.05, -2.97), Latitude = rnorm(200, 53.82, 0.02))
library(alphahull)
ahull.obj <- ahull(df, alpha = 0.03)
plot(ahull.obj) # to check
# ahull_track() returns the output as a list of geom_path objs
ahull.gg <- ahull_track(df, alpha=0.03, nps = 1000)
## change graphic param
for(i in 1:length(ahull.gg)) ahull.gg[[i]]$aes_params$colour <- "green3"
ggmap(cityMapRaster) +
geom_point(aes(x = Longitude, y = Latitude), data = df, alpha = .5, color = "darkred") +
ahull.gg
## if you like not curve but linear
ashape.obj <- ashape(df, alpha = 0.015)
plot(ashape.obj) # to check
ashape.df <- as.data.frame(ashape.obj$edge[,c("x1", "x2", "y1", "y2")])
ggmap(cityMapRaster) +
geom_point(aes(x = Longitude, y = Latitude), data = df, alpha = .5, color = "darkred") +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2), data = ashape.df, colour="green3", alpha=0.8)
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 have a map with the 8 points plotted on it:
library(ggplot2)
library(ggmap)
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)
island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, color = "#ff0000")
islandMap + RL + scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) + scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0))
Now I want to plot a circle around each of the 8 plotted locations. The circle has to have a radius of 450 meters.
This is what I mean, but then using ggplot: https://gis.stackexchange.com/questions/119736/ggmap-create-circle-symbol-where-radius-represents-distance-miles-or-km
How can I achieve this?
If you only work on a small area of the earth, here is a approximation. Each degree of the latitude represents 40075 / 360 kilometers. Each degrees of longitude represents (40075 / 360) * cos(latitude) kilomemters. With this, we can calculate approximately a data frame including all points on circles, knowing the circle centers and radius.
library(ggplot2)
library(ggmap)
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)
#################################################################################
# create circles data frame from the centers data frame
make_circles <- function(centers, radius, nPoints = 100){
# centers: the data frame of centers with ID
# radius: radius measured in kilometer
#
meanLat <- mean(centers$latitude)
# length per longitude changes with lattitude, so need correction
radiusLon <- radius /111 / cos(meanLat/57.3)
radiusLat <- radius / 111
circleDF <- data.frame(ID = rep(centers$ID, each = nPoints))
angle <- seq(0,2*pi,length.out = nPoints)
circleDF$lon <- unlist(lapply(centers$longitude, function(x) x + radiusLon * cos(angle)))
circleDF$lat <- unlist(lapply(centers$latitude, function(x) x + radiusLat * sin(angle)))
return(circleDF)
}
# here is the data frame for all circles
myCircles <- make_circles(data, 0.45)
##################################################################################
island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, color = "#ff0000")
islandMap + RL +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0)) +
########### add circles
geom_polygon(data = myCircles, aes(lon, lat, group = ID), color = "red", alpha = 0)
Well, as the referred posting already suggests - switch to a projection that is based in meters, and then back:
library(rgeos)
library(sp)
d <- SpatialPointsDataFrame(coords = data[, -1],
data = data,
proj4string = CRS("+init=epsg:4326"))
d_mrc <- spTransform(d, CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=#null +no_defs"))
Now, the width can be specified in meters:
d_mrc_bff_mrc <- gBuffer(d_mrc, byid = TRUE, width = 450)
Transform it back and add it to the plot using geom_path:
d_mrc_bff <- spTransform(d_mrc_bff_mrc, CRS("+init=epsg:4326"))
d_mrc_bff_fort <- fortify(d_mrc_bff)
islandMap +
RL +
geom_path(data=d_mrc_bff_fort, aes(long, lat, group=group), color="red") +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0))
Calculating distance in km given latitude and longitude isn't super straightforward; 1 degree lat/long is a greater distance at the equator than at the poles, for example. If you want an easy workaround that you can eyeball for accuracy, you might try:
islandMap + RL +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0)) +
geom_point(aes(x = longitude, y = latitude), data = data, size = 20, shape = 1, color = "#ff0000")
You'll need to adjust the size paramter in the 2nd geom_point to get closer to what you want. I hope that helps!
An accurate solution is using the geosphere::destPoint() function. This works without switching projections.
Define function to determine 360 points with a certain radius around one point:
library(dplyr)
library(geosphere)
fn_circle <- function(id1, lon1, lat1, radius){
data.frame(ID = id1, degree = 1:360) %>%
rowwise() %>%
mutate(lon = destPoint(c(lon1, lat1), degree, radius)[1]) %>%
mutate(lat = destPoint(c(lon1, lat1), degree, radius)[2])
}
Apply function to each row of data and convert to data.frame:
circle <- apply(data, 1, function(x) fn_circle(x[1], x[2], x[3], 450))
circle <- do.call(rbind, circle)
Then the map can be easily obtained by:
islandMap +
RL +
scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) +
scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0)) +
geom_polygon(data = circle, aes(lon, lat, group = ID), color = "red", alpha = 0)
A solution using st_buffer() from the sf package.
library(ggmap)
library(ggplot2)
library(sf)
data <- data.frame(
ID = 1:8,
longitude = c(-63.27462, -63.26499, -63.25658, -63.2519,
-63.2311, -63.2175, -63.23623, -63.25958),
latitude = c(17.6328, 17.64614, 17.64755, 17.64632,
17.64888, 17.63113, 17.61252, 17.62463)
)
Convert data.frame to sf object:
points_sf <- sf::st_as_sf(data, coords = c("longitude", "latitude"), crs = 4326)
For this example we use UTM zone 20, which contains the coordinates of the island:
data_sf_utm <- sf::st_transform(points_sf, "+proj=utm +zone=20")
Now we can buffer the point by 450 meters:
circle <- sf::st_buffer(data_sf_utm, dist = 450)
ggmap seems to have some issues with geom_sf. Setting inherit.aes to FALSE returns the desired map.
island <- ggmap::get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 14, maptype = "satellite")
ggmap(island, extent = "panel", legend = "bottomright") +
geom_sf(data = points_sf, color = "red", inherit.aes = FALSE) +
geom_sf(data = circle, color = "red", alpha = 0, inherit.aes = FALSE)
Created on 2020-10-11 by the reprex package (v0.3.0)