Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

13

You should you use group=group in the aes mapping. Here is a reproducible example of your problem: library("ggplot2") library("raster") x <- getData('GADM', country='GBR', level=2) y <- fortify(x, region="NAME_2") head(y) # long lat order hole piece group id # 1 -2.049 57.23 1 FALSE 1 Aberdeen.1 Aberdeen # 2 -2.049 57.23 ...


10

@jazzurro, you perfectly can do this with R, just look up osmar package! Read the osmar documentation (osmar.r-forge.r-project.org/RJpreprint.pdf). At pages 11 pp. you can find a detailed example for extracting roads/highways by the according tags for munich.osm! After pulling and extracting the data from a planet file for Australia you can convert to any ...


10

This is not an R solution, but Quantum GIS (QGIS) is a great way to achieve what you want. You can simply load the .osm file (Add Vector tool), right-click it in the Table of Contents and Save As ESRI Shapefile. QGIS may crash with such a large extract, so to avoid this you can uses OSM Tools like the OverPass API to download only what you need using ...


9

Robin Lovelace has provided a nice little function to download a ggmap object and convert it to a raster. Using this you could do: library(ggmap) library(raster) library(rgdal) # courtesy R Lovelace ggmap_rast <- function(map){ map_bbox <- attr(map, 'bb') .extent <- extent(as.numeric(map_bbox[c(2,4,1,3)])) my_map <- raster(.extent, nrow= ...


9

You just need to cut the data first: library(rgeos) library(maptools) library(raster) library(ggplot2) library(dplyr) library(ggthemes) library(ggalt) library(scales) #load shapefile ken <- getData("GADM", country = "KEN", level = 1) # make it less complex ken <- SpatialPolygonsDataFrame(gSimplify(ken, 0.001, TRUE), data=ken@data) #fortify for ...


8

The examples in your link look like the coordinates have been transformed via a shear and a scale matrix. You can easily apply this to the coordinates you get from the usual fortify/join data that ggplot requires. Need a unique character ID value: oregon.tract$id=as.character(1:nrow(oregon.tract)) Fortify on that ID and join attribute data: ofort = ...


8

Get the bounds of Denmark in lat-long and use coord_fixed: ggplot() + borders("world", colour="gray50", fill="gray50") + coord_fixed(xlim=c(7, 12), ylim=c(52, 58)) You can get the bounds from the map package: > map("world", "Denmark", plot=FALSE)$range [1] 8.121484 15.137110 54.628857 57.736916 And you might want to expand these a bit for nicer ...


7

Instead of outlines to indicate the irrigated areas you should use something like a transparent fill pattern (e.g. lines, hachures). An example would look similar to this: or just google "map fill patterns" to get an overview of the options. Using outlines only for the irrigated areas would give the impression that irrigation is not a continous phenomenon.


6

OK, here comes the correct answer: Make sure that rgdal (version >= 1.0.4) is installed install.packages('rgdal') packageVersion('rgdal') [1] ‘1.0.4’ Make sure that gdal (version >= 1.11.0) is installed library(rgdal) getGDALVersionInfo() [1] "GDAL 1.11.2, released 2015/02/10" Make sure that gdal is compiled with Expat/OSM and SQLite support: c('...


6

# make it reproducible set.seed(1492) Sector <- rep(c("S01","S02","S03","S04","S05","S06","S07"),times=7) Year <- rep(c("1950","1960","1970","1980","1990","2000","2010"),each=7) Value <- runif(49, 10, 100) df <- data.frame(Sector,Year,Value) gg <- ggplot(df, aes(x=as.numeric(as.character(Year)), y=Value)) gg <- gg + geom_area(aes(colour=...


6

Extending on @Spacedman's answer, creating a stacked map like the one shown in the question becomes quite simple. You just need to add another map layer and displace its y axis: e.g. aes(x=x, y=y+5) : ggplot(data= ofort) + geom_polygon( aes(x=x, y=y, group=id), fill= "white", color="gray30") + # layer 1 geom_polygon( aes(x=x, y=y+5, group=id, fill=...


6

I figured it out. The polygons need to be manually closed by rbinding the first row: state_data <- ggplot2::map_data("state") state_data_sf <- lapply(unique(state_data$region), function(x) list(matrix(unlist( rbind( state_data[state_data$region == x,][,c("long", "lat")], ...


5

I'm assuming that you are using the Meridian 2 data from the Ordnance Survey and have used that and your data to create the chart below. I have stripped away the parts of your code that seemed to me to be redundant. Basically as far as I can see you want to automatically remove points that do not fall within the area in question, in this case the county of ...


5

Here is a suggestion. I create the circles with gBuffer and then reproject them into WGS84 for ggmap. To change the colors of the heat map use scale_fill_gradient(). library(ggmap) library(sp) library(rgdal) library(rgeos) # get the NY coordinates nyc <- geocode("New York") # create spatialPoint object coordinates(nyc) <- ~ lon + lat proj4string(...


5

I used 10-m lake shapefile data from Natural Earth along with country borders from rworldmap to reproduce what I guess you were trying to achieve. Hadley Wickham wrote a nice tutorial on how to visualize spatial data using ggplot2 and I strongly encourage you to have a look at it. Still, a short workaround is required in order to get the visualization to ...


4

ggplot2 always expects a data frame as input. See ggplot2: Elegant Graphics for Data Analysis (Use R!), Section 4.4 Data: The restriction on the data is simple: it must be a data frame. This is restrictive, and unlike other graphics packages in R. Lattice functions can take an optional data frame or use vectors directly from the global environment. ...


4

Thanks to a chat with immense help from @MLavoie, we solved the issue by changing the data type of my ML1 attributes from an integer to factor and adding a c() to properly format the list of colors. New code should look like this: ggplot(eth.df) + aes(long, lat, group = group, fill = factor(ML1)) + geom_polygon() + scale_fill_manual(values = c("...


4

After reading in your sample data, transform REG to the same coords as NEI. Combine the two objects, and save as a shapefile: REG = st_transform(REG, st_crs(NEI)) d = rbind(NEI, REG) st_write(d,"NEIREG.shp") Then in the shell, run pprepair: pprepair -i NEIREG.shp -o FIX.shp -fix Back in R, load the new shapefile: fix = st_read("FIX.shp") Here's the "...


3

A group aesthetic is missing: ggplot() + geom_polygon(data=fortify(regions), aes(long, lat, group=group)) Otherwise the last point of a polygon is connected with the first point of the next polygon. See also here: SpatialPolygonDataFrame plotting using ggplot Remove connecting lines in ggplot2 geom_polygon


3

Simple answer, time to go home... image.plot(x = c(0:10), y = c(0:10), z = matrix(runif(100, 0,1), nrow = 10), col = terrain.colors(20)) # fake data so lines() plays nice. plot(SpP)


3

I found out what was wrong. No Layer call inside ggplot, here the right code: ##Vector processing=group ##showplots ##Layer=vector ##a=Field Layer ##b=Field Layer library("ggplot2") ggplot() + geom_point(aes(Layer[[a]], Layer[[b]])) Cheers


3

So why doesn't it work? Look at your code, specifically the line with the attribute fields. You need to point R/QGIS directly towards the field data vectors unless you attach them beforehand. The following code works without issues for me (provided that you have installed ggplot2): ##Vector processing=group ##showplots ##Layer=vector ##Field1=Field Layer ##...


3

Include fill/color in your aesthetics mapping: ggplot() + geom_polygon(data = tz.prj, aes(long, lat, group = group), fill = "#f1f4c7", color = "#afb38a") + geom_point(data = tz.c, aes(lon, lat, fill = "Hospitals"), pch = 21, color = "black"...


3

Try clipping the polygons before using them (also, please try to provide complete code including library calls in the future): library(ggmap) library(rgdal) library(rgeos) library(ggplot2) URL <- 'https://ago-item-storage.s3.amazonaws.com/f7f805eb65eb4ab787a0a3e1116ca7e5/states_21basic.zip?AWSAccessKeyId=AKIAJLEZ6UDU5TV4KMBQ&Expires=1454295860&...


3

Your question boils down to getting this error with your fourth shapefile: > library(raster); library(sp); library(rgdal) > s4 = shapefile("SST-shape-199301-4.shp") > s4$id = rownames(s4@data) > f4 = fortify(s4, region="id") Error: TopologyException: Input geom 0 is invalid: Hole lies outside shell at or near point 11 35 at 11 35 Interestingly ...


3

Personally I'd ditch ggplot and use raster as much as possible. But here's a solution. Work out which points are in and which are out of France: inout = over( SpatialPoints(df[,c("lon","lat")],proj4string=CRS(projection(fr))), as(fr,"SpatialPolygons") ) Then ggplot the subset that isn't NA: > ggplot() + geom_tile(data=df[!is.na(inout),],aes(x=...


3

lapply(r,..) fails because even though a stack has some semantics of a list (like defining r[[1]] to r[[N]]) that's not sufficient for lapply to work with a raster stack. Convert your stack to a list of single-band rasters with as.list, then lapply as usual: My stack: > s class : RasterStack dimensions : 4, 4, 16, 3 (nrow, ncol, ncell, ...


3

Here is an alternative using sf package: # load libraries including development version of ggplot2, which is installed from GitHub (devtools::install_github('tidyverse/ggplot2')) library(raster) library(sf) library(ggplot2) mex <- raster::getData(country="MEX", level=1) munis <- raster::getData(country="MEX", level=2) # convert to sf object mex <...


3

Your code is coercing the data into a data.frame before piping into the list object so, don't do that. I would recommend some small changes to your code and then using do.call on the list to bind the results into a single sp SpatialPolygonsDataFrame object. templist <- list() for (i in 1:length(basins)){ thisbasin <- readOGR(dsn=dir, layer=...


Only top voted, non community-wiki answers of a minimum length are eligible