Questions tagged [ggplot2]
Use this tag when referring to the R package for high level data visualization and mapping.
157
questions
0
votes
1
answer
18
views
Color code sf points object by category in R
I am trying to color code an sf points object in R, but can't quite figure out how to do it. I want the NAs to show up as a separate category. Here is the code I have so far.
dat = data.frame(utmn = c(...
0
votes
1
answer
39
views
Adding borders to 2D plot in ggplot2
the displayed code produces the following plot with ggplot2:
plot_DK <- ggplot(coordinate_grid, aes(coordinate_east, coordinate_north, fill = model1_predictions)) +
geom_tile() +
...
0
votes
1
answer
48
views
Polygons in shapefile that are close are drawn far away: how make the close polygons be drawn together?
I have a shapefile with terrestrial birds conservation regions (https://birdscanada.org/download/gislab/bcr_terrestrial_shape.zip?_ga=2.173557475.1349516436.1715087042-735583731.1713557857) from the ...
1
vote
0
answers
133
views
Overlay shapefile boundaries on geom_tile heat map in R
I have a square matrix which corresponds to a square area of a map.
The values represent the mean value of any centroids in the relevant grid square.
I am visualising this using geom_tile in ggplot.
...
0
votes
0
answers
48
views
plot lidR Hull object with geom_sf with a basemap
So I am trying to create a static map by overlaying the tree canopy hulls (created using crown_metrics function) as hollow polygons with yellow color on a ESRI basemap.
How can I resolve the error? I ...
0
votes
0
answers
89
views
ggplot and sf package: broken polygons after projection
My standard hack for fixing broken polygons after re-projection (see https://stackoverflow.com/questions/77298627/strange-behavior-with-ggplot-sf-package-and-orthographic-projection) does NOT seem to ...
0
votes
0
answers
95
views
Strange behavior with ggplot, sf package and orthographic projection
Cross posting with stack-overflow: the ggplot package throws errors for some versions of an orthographic projection. For example, this works fine
library(tmap)
library(sf)
library(tidyverse)
data(&...
0
votes
1
answer
119
views
Oceans as global background won't reproject properly
I'm trying to get an line for the edge of the globe (or a polygon for the oceans as an equivalent) with ggplot and sf. My code works for a basic Mollweide projection ...
library(tmap)
library(sf)
...
1
vote
2
answers
3k
views
Using a downloaded shapefile in ggplot2 and R, readOGR error
I am having trouble taking a downloaded shapefile and using it in R and {ggplot2}. The online instructions (like this one) use code that have the function readOGR(). When I use readOGR(), I get ...
2
votes
1
answer
108
views
Specifying ggplot2 aesthetics for factors stored in SpatialLinesDataFrame
I produced a set of 2 contour lines for 6 different individuals. The contours for each individual were originally stored in a single list which contained 6 separate SpatialLinesDataFrames (one per ...
1
vote
0
answers
428
views
Making map with marginal graphics like rasterVis::levelplot(margin=T)
I love rasterVis::levelplot(margin=T) because its marginal graphics show the column (x)
and row (y) summaries of the Raster object. I think it is very helpful to visualize the raster.
Package ...
0
votes
0
answers
1k
views
Why is coord_sf needed to plot a geom_raster with ggplot2?
Why is coord_sf() needed to plot a geom_raster in ggplot2 and what exactly does coord_sf() do?
According to ggplot2's Map project's documentation: coord_map projects a portion of spherical earth onto ...
1
vote
1
answer
260
views
Kernel density plot in R, plot discreet values on map for specific grid cells
I am trying to highlight single 1x1 degree grid squares on a map.
It works highlighting individual grid squares for the first 1 squares but after highlight the 4th square it begins to highlight ...
1
vote
1
answer
71
views
Overlay colors onto specific 1 degree x 1 degree grid squares in R
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 use utilize the data points I have to color in the entire grid ...
1
vote
1
answer
208
views
Working and saving histogram of data
I'm new to R and struggling to create a histogram and add a mean line. I've tried both hist() and doing it through ggplot, but keep seeing errors.
I'm using the newest version of R studio on a windows ...
0
votes
2
answers
698
views
SF Collection Plots in lat/long but Geometry is Not
I'm using urbnmapr to get a county base map. I want to plot my own points on top of this map. Even though the urbnmapr map plots with lat/long on the x y axis, the lat/long points that I want to plot ...
0
votes
1
answer
289
views
How to set number of columns with stack or brick or spatraster in rasterVis::levelplot?
If I have a spatraster with 6 layers, rasterVis::levelplot will give a 2 rows and 3 columns plot.
How can I change it to 3 rows and 2columns?
library(rasterVis)
library(terra)
f <- system.file(&...
0
votes
0
answers
377
views
Recenter map that crosses international dateline with ggplot and geom_sf
Consider the map of Europe below. Parts of Russia are on the other side of the map. How might I "recenter" the map so that there is less empty space? I guess I need to somehow move the ...
1
vote
1
answer
357
views
Exporting raster of a map to QGIS from GGplot in R
I used R to make density surface models of whales in GGPLOT. However, l can not work out how to get the model into a raster so i can use it in QGIS. I have been trying as below but the squares of the ...
0
votes
1
answer
498
views
Coloring polygons (made with concaveman) in ggplot by column
I'm adapting code for a map made by someone else that uses a package concaveman to generate concave hulls from points. The goal is to plot a number of different polygons in the oceans, and to color-...
0
votes
1
answer
712
views
Problem with multiple raster plot
This is my code to try plotting three different rasters with ggplot2. However, when I run the code just one raster is displayed.
What could the problem be?
library(ggplot2)
library(dplyr)
library(...
0
votes
1
answer
1k
views
Only a part of the shapefile plotted with ggplot2 in R
I would like to plot shapefiles from the EUFORGEN website (http://www.euforgen.org/) in R (European beech and Scots pine). At first I had problems, because they are files with holes. I was able to ...
1
vote
0
answers
445
views
How to add polygon in ggplot2 with raster? Polygon is missing from final plot after ggplot
I have a stack of raster's and suppose to find the mean of it. I have done it. Now, I want to add polygon on the mean of raster image that I got. Then, from this raster I wants to save and make it ...
2
votes
1
answer
597
views
Reprojected data from stars R package takes forever with geom_stars and ggplot2
I'm finding very different plotting results with stars objects after I reproject them and use ggplot2. Consider the following:
library(stars)
library(ggplot2)
system.file("tif/L7_ETMs.tif", ...
1
vote
1
answer
1k
views
Plotting a heatmap of Kriging with coordinate in R
I performed Kriging in R. But I have trouble getting a plot of a colored heatmap showing the resulted interpolated area along with a lon/lat values at the side and on the button.
Here is an example, ...
1
vote
1
answer
866
views
Limit the x-axis values of a barplot in r
I have a raster that includes the 120 million pixel values 0,1,10,11.
I generate a percentage raster. But I want the x-axis to show only the pixel values.
a = as.data.frame(diff_raster[[5]])
ggplot(a,...
0
votes
0
answers
206
views
Loop over dataframes and extract specific values to generate multiple rasters/plots
I would seek help to revise this script.
I need to generate multiple plots.
For example, the following code works well ggplot and gsave but I would prefer to use raster, and I tried something such as ...
4
votes
1
answer
4k
views
sf package point in polygon
I'm trying to create a simple polygon in sf and select only points within that polygon. What am I doing wrong here?
library(sf)
library(concaveman)
library(ggplot2)
foo.df <- data.frame("long&...
5
votes
1
answer
3k
views
Plot two layers (raster and polygon-vector) with ggplot and use the fill-scale* for both layers
I am a little lost. I have a stars object where I would like to use the first attribute and map it to the
scale_fill*-function in ggplot.
I also have an polygon-object of class sf that has an ...
0
votes
1
answer
131
views
Eurostat shapefiles plotting with errors
I am trying to plot interactive maps at the NUTS3 level. Initially I was using shapefiles that I constructed myself for the data in QGIS, however, when I tried to use them in R they rendered poorly ...
2
votes
1
answer
2k
views
Simplify raster size to plot using ggplot geom_raster()
I want to create a map using 'ggplot2'. My data contains shapefile (points data), polygons (European countries). Underneath the vector data, I wanted to place the raster showing extends of forests ...
0
votes
1
answer
876
views
geom_raster showing inverted colors
I am trying to plot a simple single band raster in R using ggplot. The only problem with my plot is that the colors are inverted. How can I fix that?
### For plotting I am using ggplot2 and ggpspatial ...
2
votes
1
answer
5k
views
Why won't ggplot fill heat map using geom_tile in R?
I am trying to emulate some geom_tile plots I saw in a research paper. The plots were created with simulated data. Below I created a dataframe of "dummy data" using a subset of the actual ...
6
votes
1
answer
4k
views
Plotting multiple raster stacks with rasterVis::gplot and ggplot2::facet_wrap
I'm trying to ggplot2::facet_wrap a stack of rasters, and then call geom_raster to add more rasters on top of the previously mapped raster stack.
Here is a very simple example dataset to re-create ...
1
vote
1
answer
152
views
Why does not the coordinate of the graph match the dataframe?
I drawing the plot of Île-de-France with
library(sf)
library(ggplot2)
df <- read.csv("https://raw.githubusercontent.com/leanhdung1994/Stat_Cons/main/station.csv")[1:3]
df2 <- st_as_sf(...
8
votes
1
answer
13k
views
Plotting a raster stack with ggplot2
My aim is to plot a rasterstack as a single image using ggplot2. First, I apply ggplot on a single layer raster using the following code:
#Load packages
library(raster)
library(ggplot2)
#Create a ...
2
votes
1
answer
325
views
Spatial neighbors in ggplot2
I'm trying to make a space plot with ggplot() as indicated here, but I'm having trouble using the nb2lines() function.
.shp files are here
Error in if (!is.na(projargs) && !nzchar(projargs)) ...
1
vote
1
answer
2k
views
How to perform point to raster operation and plot it with raster of different spatial resolution in R?
I posted a question (please find the link below) regarding raster plots at stackoverflow but could not get the solution. Does anyone here has the solution?
https://stackoverflow.com/questions/63209707/...
1
vote
0
answers
409
views
Plotting arrows in a map to show shifts in coordinates or points using ggplot2
Plotting arrows in a map to show shifts in coordinates or points using ggplot2
I have three points in my map (latitudes and longitudes of the points: a, b and c are provided below). I would like to ...
1
vote
1
answer
1k
views
Stat_density_2d density plot seem to show too high density values for a small dataset
I'm working with a two-column data.frame with the columns containing x and y coordinates (Longitude, Latitude).
A simple way to visualize a density map is by using the 'stat_density_2d' function. When ...
1
vote
1
answer
4k
views
Wrong scale in ggplot2 map
I am trying to create a political map of Morocco with regions, using ggplot2, but the scale seems to be wrong. This is the map:
And below is the code I used:
library(ggplot2)
library(ggspatial)
YK = ...
3
votes
1
answer
4k
views
Axis labels with geom_sf & ggmap
I'm trying to create a map using a basemap pulled in using ggmap (get_stamenmap) with some shapefiles plopped on top (geom_sf). I want to change axis labels! Why's it so difficult!
When I only plot ...
0
votes
1
answer
344
views
plot_usmap Incorrect Legend Values
I am trying to use plot_usmap to map a data frame with two columns - fips and change2 - where the values for change2 range from -34.7 to 91.5. When I run this code:
plot_usmap(regions = "counties", ...
0
votes
2
answers
586
views
Computing Moran's I from a ggplot map using R
In an effort to detect spatial autocorrelation using Moran's I in some variables, I was wondering how I could go about creating a polygonal shapefile from a ggplot-created map. My data lists multiple ...
1
vote
1
answer
2k
views
Adding elevation point data and lines to topography plot using ggplot2
I have pieced together some code to make a topography plot using elevation data and ggplot2.
However, now I would like to overlay onto this plot the country outlines, along with points to show the ...
1
vote
1
answer
1k
views
geom_rect rectangle disappears when used together with coord_sf in R
I am plotting a map with ggplot2 and would like to have a rectangle surrounding it. I have tried to create the rectangle using geom_rect function, but whenever I include coord_sf function in the code ...
1
vote
1
answer
1k
views
Base-R legend for quick plot of sf-objects
I have two sf-objects that are polygons and one that is a layer of points. The attributes (columns) don't really matter for quick visualization purposes. I just want to make a basic legend and give ...
3
votes
0
answers
384
views
What is tilde (~) for in R? [closed]
I know this is an almost too simple question. But maybe there is a fast answer. Looking through some code to build a leaflet map in R I found that part of code:
mypal = colorNumeric(
palette = "...
3
votes
0
answers
175
views
Remediating path that crosses international dateline
I'm trying to plot the paths of multiple ships on a world map using R and a series of Lat/Lon points.
All is well until the ship crosses the international dateline (-180/180) when the path jumps ...
1
vote
1
answer
2k
views
ggplot2lend for sf-objects
I wanted to make a simple plot of four sf-objects which don't contain much more than the sfc-column and some postal-code information.
The sf-objects have the names level 1-4 and plz. What I would ...