So I'm putting trees in forest plots into a map of Madagascar
The beginning on the data set looks like this:
#ggmap!
library(ggmap)
library(mapproj)
map <- get_map(location = 'Madagascar', zoom = 4)
ggmap(map)
map <- get_map(location = 'Madagascar', zoom = 10)
geocode("kianjavato")
#lon lat
#47.86682 -21.38024
k <- "kianjavato"
#qmap(k, zoom = 16)
#qmap(k, zoom = 16, source = "stamen", maptype = "toner")
myMap <- get_map(location=k, source="stamen", maptype="toner", crop=FALSE, zoom=16)
# having trouble zooming
ggmap(myMap)
Here is the code I use to make the map and get the points:
ggplot(data=GPScorrect,aes(x=Lon,y=Lat))+geom_point()
But when I go to plot the two together using this code:
ggmap(myMap) + geom_point(data=GPScorrect,aes(x = 'Lon', y ='Lat'))
I get this error message:
Error: Discrete value supplied to continuous scale
PLEASE HELP
When asking questions, be sure to provide a working example that executes front to back until it encounters the error you're asking about. In this case, you don't supply data for GPScorrect.
Un-quote your aesthetic mappings. If you re-work the following stripped down example you should get what you're looking for:
library(ggmap)
library(mapproj)
myMap <- get_map(location = 'Madagascar', zoom = 6)
e1 <- geocode('Antananarivo')
e2 <- geocode('Toamasina')
e3 <- geocode('Antsirabe')
GPScorrect <- rbind(e1, e2, e3)
ggmap(myMap) + geom_point(data=GPScorrect,aes(x = lon, y =lat), color = 'firebrick', size = 3)
Related
I'm working on various mapping of data. One of the maps of interest is taking boundary maps and overlaying them on an exist map. These existing maps may come from Google or other sources.
The issue that I'm having is that when I use geom_path(), with an alpha < 1, if two areas share a boundary, that boundary is darker than other boundaries. Any way to make all the boundaries the same transparency?
library(ggplot2)
library(ggmap)
library(maptools)
library(plyr)
zips <- readShapePoly('cb_2016_us_zcta510_500k.shp')
test <- fortify(zips)
zips#data$id <- rownames(zips#data)
zips.points <- fortify(zips)
zips.df <- join(zips.points, zips#data, by = "id")
test_zips <- c(64002,64012,64013,64014,64015,64029,64030,64034,64050
,64051,64052,64053,64054,64055,64056,64057,64063,64064,64065,64070
,64075,64080,64081,64082,64083,64086,64108,64109,64110,64111,64112
,64113,64114,64121,64123,64124,64125,64126,64127,64128,64129,64130
,64131,64132,64133,64134,64136,64137,64138,64139,64141,64145,64146
,64147,64148,64149,64170,64171,64179,64191,64198,64999,66119,66160
,66205,66206,66207,66208,66209,66211,66222,66224,66251)
Valid_Zips <- zips.df[zips.df$GEOID10 %in% test_zips,]
mapImage <- get_map(location = c(lon = -94.4, lat = 38.9),
color = "color",
source = "google",
maptype = "toner-2011",
zoom = 10)
ggmap(mapImage) +
geom_path(aes(x = long, y = lat, group = group)
,color="red",data=Valid_Zips,size=1,alpha = .3)
I made static heatmaps with the library(ggmap) and the stat_density2d() function. Looking to recreate this in a shiny app on a dynamic leaflet map, I found addHeatmap(). However, the resulting images are dissimilar, with the ggmap version seemingly offering the correct result.
GGMAP
LEAFLET
What is causing this difference?
To run both of the below reproducible examples, you can download some data (csv file) I put here.
https://drive.google.com/drive/folders/0B8_GTHBuoKSRR1VIRmhOUTJKYU0?usp=sharing
Note that the leaflet result differs with zoom level, but never matches the ggmap result (e.g. in terms location of maximum heat).
This is the ggmap code.
library(ggmap)
data <- read.csv("DATA.csv", sep=";")
data <- subset(data, !is.na(CrdLatDeg))
xmin <- min(data$CrdLonDeg)
xmax <- max(data$CrdLonDeg)
ymin <- min(data$CrdLatDeg)
ymax <- max(data$CrdLatDeg)
lon <- c(xmin,xmax)
lat <- c(ymin,ymax)
map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 17,
maptype = "satellite", source = "google")
ggmap(map) +
labs(x="longitude", y="latitude") +
stat_density2d(data=data, aes(x=CrdLonDeg, y=CrdLatDeg, alpha= ..level.., fill= ..level..), colour=FALSE,
geom="polygon", bins=100) +
scale_fill_gradientn(colours=c(rev(rainbow(100, start=0, end=.7)))) + scale_alpha(range=c(0,.8)) +
guides(alpha=FALSE,fill=FALSE)
This is the leaflet code.
library(leaflet.extras)
data <- read.csv("DATA.csv", sep=";")
data <- subset(data, !is.na(CrdLatDeg))
leaflet(data) %>%
addTiles(group="OSM") %>%
addHeatmap(group="heat", lng=~CrdLonDeg, lat=~CrdLatDeg, max=.6, blur = 60)
The images look different because the algorithms are different.
stat_density2d() extrapolates a probability density function from the discrete data.
Leaflet implementation of heatmaps rely on libraries like simpleheat, heatmap.js or webgl-heatmap. These heatmaps do not rely on probability density. (I'm not fully sure which of these is used by r-leaflet's addHeatmap).
Instead, these heatmaps work by drawing a blurred circle for each point, raising the value of each pixel by an amount directly proportional to the intensity of the point (constant in your case), and inversely proportional to the distance between the point and the circle. Every data point is shown in the heatmap as a circle. You can see this by playing with your mouse cursor in the heatmap.js webpage, or by looking at this lone point in the top-right of your image:
Think of a heatmap like a visualization of the function
f(pixel) = ∑ ( max( 0, radius - distance(pixel, point) ) · intensity(point) )
One can tweak the radius and intensity of heatmaps, but the result will never be the same as a statistical density estimation.
I've found this answer over at GIS, and I've attempted to create a function and applied it to this case. I haven't figured out how to finetune the colour gradient scheme as of yet, but it seems like a good first start otherwise:
library(leaflet)
library(rlang)
addHeatMap <- function(data, lon, lat, intensity, show.legend, ...) {
df <- data.table::as.data.table(data)
df_expanded <- dplyr::slice(df, rep(1:dplyr::n(), times = !! enquo(intensity)))
lon_var <- dplyr::pull(df_expanded, !! enquo(lon))
lat_var <- dplyr::pull(df_expanded, !! enquo(lat))
lon_bw <- MASS::bandwidth.nrd(lon_var)
lat_bw <- MASS::bandwidth.nrd(lat_var)
lon_lat_df <- dplyr::select(df_expanded, !! enquo(lon), !! enquo(lat))
kde <- KernSmooth::bkde2D(lon_lat_df, bandwidth = c(lon_bw, lat_bw))
CL <- contourLines(kde$x1 , kde$x2 , kde$fhat)
LEVS <- as.factor(sapply(CL, `[[`, "level"))
NLEV <- nlevels(LEVS)
pgons <- lapply(1:length(CL), function(i)
sp::Polygons(list(sp::Polygon(cbind(CL[[i]]$x, CL[[i]]$y))), ID = i))
spgons <- sp::SpatialPolygons(pgons)
if (show.legend) {
leaflet::addPolygons(data = spgons, color = heat.colors(NLEV, NULL)[LEVS], stroke = FALSE, ...) %>%
leaflet::addLegend(colors = heat.colors(NLEV, NULL)[LEVS], labels = LEVS)
} else {
leaflet::addPolygons(data = spgons, color = heat.colors(NLEV, NULL)[LEVS], stroke = FALSE, ...)
}
}
mydata <- read.csv("DATA.csv", sep=";")
mydata <- subset(mydata, !is.na(CrdLatDeg))
leaflet() %>%
addTiles(group = "OSM") %>%
addHeatMap(data = mydata, lon = CrdLonDeg, lat = CrdLatDeg, intensity = FsmIdf, show.legend = TRUE)
Both use a different algorithm. You need to tweak the radius and blur arguments of addHeatmap and the h argument of stat_density2d to get somewhat similar results.
I am trying to plot few points on map. But I get a blank map with just the points marked in red. The map is missing
Below code is reporducible
library(maps)
library(mapdata)
library(ggplot2)
library(ggmap)
lon <- c(-122.1817252,-119.4179324,-95.7128910,-71.0588801,-81.0348144)
lat <- c(37.452960,36.778261,37.090240,42.360082,34.000710)
all_places_geocoded <- data.frame(lon,lat)
gc <- geocode('australia')
center <- as.numeric(gc)
map <- get_googlemap(location = center,maptype = "roadmap", zoom=1)
ggmap(map) +
geom_point(data = all_places_geocoded, color = "red3", size = 1)
I am plotting all latitude longitude values which I receive on a map like this.
The map has the color as per the time duration of halt recorded at that point.
My agonies are the following:
I cannot zoom in and zoom out - Centering the map is very painful - And I have 10*100 such collections to be plotted and evaluated
I can hardly make out the areas where the halt was specific, all I can see is that it was good with no halt but reality is not that
Hence I looked for plotly and to my surprise they don't have the support.
How can I achieve the results? I don't have a tool like tableau
Please suggest if you can help.
Below code is an example of what I am trying to do with ggmap :
library(ggplot2)
library(data.table)
library(ggmap)
library(maps)
library(mapdata)
lat <- seq(31.26415,31.26831,0.00010)
lon <- seq(76.80890,76.82320,0.00015)
lon <- lon[seq(1,96.2)]
lon <- lon[1:42]
lat_long <- data.frame(lat,lon)
lat_median <- 31.26751
lon_median <- 76.82003
#map <- get_map(location = c(lon_median, lat_median), maptype = "roadmap", zoom = 15)
map <- get_googlemap(center = c(lon_median, lat_median), zoom = 12, maptype = "roadmap",size = c(600, 300), scale = 2)
ggmap(map) +
geom_path(data = lat_long, aes(), size = 2, lineend = "butt") +
geom_point(data = lat_long, color = "red3", size = 1)
I am plotting few lat longs using ggmap and I get the output like this
I use the following code to generate this output, below code is part of the o/p
library(ggplot2)
library(data.table)
library(ggmap)
library(maps)
library(mapdata)
lat <- seq(31.26415,31.26831,0.00010)
lon <- seq(76.80890,76.82320,0.00015)
lon <- lon[seq(1,96.2)]
lon <- lon[1:42]
lat_long <- data.frame(lat,lon)
lat_median <- 31.26751
lon_median <- 76.82003
map <- get_map(location = c(lon_median, lat_median), maptype = "roadmap", zoom = 15)
ggmap(map) +
geom_path(data = lat_long, aes(), size = 2, lineend = "butt") +
geom_point(data = lat_long, color = "red3", size = 1)
My output window has lot of white space, which I could have used to show in the map. Can I increase this map output to better fit the window?
If you use the get_googlemap function you can specify the dimensions and it doesn't have to be square e.g.:
map <- get_googlemap('paris', zoom = 15, size = c(500, 200), scale = 2)