Skip to main content
11 votes

Creating map of Denmark using ggmap

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 ...
Spacedman's user avatar
  • 65.6k
11 votes
Accepted

Plotting a raster stack with ggplot2

You can use scale_fill_gradientn to specify the fill, and facet_wrap should pick the colours from there: If you have rasterVis package installed, you can use gplot function to create the ggplot ...
Liman's user avatar
  • 450
10 votes
Accepted

Creating an sf object from the maps package

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), ...
jsta's user avatar
  • 1,355
5 votes

fit two sf polygons seamlessly

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,"...
Spacedman's user avatar
  • 65.6k
5 votes
Accepted

Overlay shapefile on multiband raster using ggRGB and ggplot2 in R

You miss a very important part. First, an example: library(raster) library(ggplot2) library(RStoolbox) data(lsat) lsat poly <- as(extent(622999,625000,-417000,-415000),'SpatialPolygons') poly &...
aldo_tapia's user avatar
  • 13.8k
5 votes
Accepted

Plot two layers (raster and polygon-vector) with ggplot and use the fill-scale* for both layers

Sounds like you are looking for something like the functionality in the ggnewscale package. In your case, you could use its new_scale_fill() function, like this: library(ggnewscale) # make the ...
Josh O'Brien's user avatar
4 votes

Keep customized colors with geom_polygon()

The trick to get the same colors in the ggplot is that you have to make a new vector repeating your color vector to reach the same length as the number of rows of the mapFrance object. Try the ...
Guz's user avatar
  • 3,176
4 votes
Accepted

Convert all rasters in a rasterstack to polygons

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 ...
Spacedman's user avatar
  • 65.6k
4 votes
Accepted

Polygons in shapefile that are close are drawn far away: how make the close polygons be drawn together?

From my point of view, you should be able to handle this by shifting geographical coordinates for a Pacific view making use of sf::st_shift_longitude(): ?st_shift_longitude: All longitudes < 0 are ...
dimfalk's user avatar
  • 1,415
3 votes

Unable to Export Kriging Prediction Map

You will not be able to export symbology from the plot function, only the raster(s). This is not a shapefile (polygon, point, line) but rather a raster format. Whereas there are steps to convert to a ...
Jeffrey Evans's user avatar
3 votes

raster::plot() plotting raster too small- is there an alternative on ggplot?

The base plot method works fine for me: so I suspect something has gone amiss with your graphics device. If using rstudio, I think there's a little button to reset the graphics window, otherwise try ...
Spacedman's user avatar
  • 65.6k
3 votes
Accepted

Keep customized colors with geom_polygon()

The easiest way is to use scale_fill_identity in ggplot2: library("ggplot2") library("maps") library("dplyr") # create data frame and merge predefined colors in vector `color` map_fr <- map_data("...
rcs's user avatar
  • 3,894
3 votes

Remove points outside a map with ggplot2

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")],...
Spacedman's user avatar
  • 65.6k
3 votes
Accepted

Adding a legend to points in the tmap package

My two cents... Set labels and text in tm_add_legend() and set legend.col.show = F in tm_text: tm_shape(st_as_sf(Europe)) + tm_polygons() + ...
aldo_tapia's user avatar
  • 13.8k
3 votes
Accepted

R error fortifying dataframe from shapefile

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@...
Spacedman's user avatar
  • 65.6k
3 votes
Accepted

ggplot2 fills holes in polygons

You can now use package sf and the development version of ggplot2 (e.g. devtools::install_github('tidyverse/ggplot2')). # load libraries library(sf) library(ggplot2) # read the sample data you ...
sebdalgarno's user avatar
  • 1,733
3 votes

Plot tilted map in R

Here's an approach using 'sf', also at https://gist.github.com/obrl-soil/ad588993511d7294143406585cdf8f62 library(sf) nc <- st_read(system.file('shape/nc.shp', package = 'sf')) sm <- matrix(c(2,...
obrl_soil's user avatar
  • 3,772
3 votes
Accepted

symbols for different polygon layers in ggplot2

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) ...
sebdalgarno's user avatar
  • 1,733
3 votes
Accepted

Setting fill to a variable based on data within SpatialPolygonsDataFrame?

Okidokey. So - it turns out that even though ggmap2 can manage SpatialDataFrames, assigning values to it is pretty tricky. The issue I was having is a mismatch between each element of the polygon (so ...
rtob's user avatar
  • 73
3 votes
Accepted

Binding multiple polygons in R

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 ...
Jeffrey Evans's user avatar
3 votes

Axis labels with geom_sf & ggmap

I've repeated your example and I wonder why do you need to add additional scale properties? Everything works fine ever without additional scale settings. As far as I understand you are getting this ...
Anatolii's user avatar
  • 201
3 votes

Wrong scale in ggplot2 map

I suspect your scale is wrong because annotation_scale() automatically uses map units based on your projection to determine the proportion of the scale bar. I recommend trying out the scalebar() ...
Keri Klinges's user avatar
3 votes
Accepted

sf package point in polygon

You can use st_intersects() and it will let you know which points are within the polygon. Add st_intersects(poly, goodpoints)[[1]] and it will return [1] 1 2. If you add coordinate points 136.1, 36.1 ...
Adam Thom's user avatar
  • 1,512
3 votes
Accepted

Limit the x-axis values of a barplot in r

You can do this by plotting the data as a factor but that also means you need to change some values to be ..count../sum(..count..). First if you can't give us your data, you can always try and make ...
Spacedman's user avatar
  • 65.6k
3 votes
Accepted

Reprojected data from stars R package takes forever with geom_stars and ggplot2

When you do that transformation... > x_proj stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. L7_ETMs.tif 1 54 69 68....
Spacedman's user avatar
  • 65.6k
2 votes
Accepted

Plotting islands in ggplot2

It looks like @rcs had the right idea, although generifying that into a function is a little more difficult. This blog post suggests that the 'easy way' is to use the non-CRAN package ggspatial (which ...
paleolimbot's user avatar
2 votes
Accepted

Save a rasterPCA plot as a raster file in R

Slot map inside PCA result is a RasterBrick: library(RStoolbox) library(ggplot2) library(reshape2) data(rlogo) set.seed(25) rpc <- rasterPCA(rlogo) class(rpc$map) [1] "RasterBrick" ...
aldo_tapia's user avatar
  • 13.8k
2 votes

How to create a crisp topographical/terrain map with ggplot2?

Have you tried lowering the resolution of your raster data before creating the plot? Unfortunately, R is still slow for plotting large images so there is a trade-off between resolution and plotting ...
rafa.pereira's user avatar
  • 1,277
2 votes

simple plotting of (latitude, longitude) points on a global map

Sample code. Given a world map (e.g. lat: 80 ~ -80, lon: -160 ~ 160) was downloaded as PNG ("Downloaded_Image.png"). library(png) Downloaded_Image <- readPNG("Downloaded_Image.png") library(...
Kazuhito's user avatar
  • 31.1k

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