4

Background:

I am using ggplot2 geom_point to map animal location points by week. As a basemap, I am using a shapefile from my computer.

Here is an example of my data:

datexample <- data.frame(
  "animal" = c("A","B"), 
  "yearweek" = c(202028, 202028, 202029, 202029),
  "lat" =  c(45.25, 44.75, 45.25, 45.75), 
  "lon" = c(-61.75, -61.25, -62.75, -62.25)
)
datexample

Here is an example of the ggplot without the basemap added:

geom_point(data = dat, aes(x = lon, y = lat, alpha = yearweek))+
  facet_grid(cols = vars(animal)) +
  xlab("Longitude")+
  ylab("Latitude")

Problem:

The above code worked before I updated R and RStudio to the newest version (last week). Now, these maps will not draw, and I get the following error:

Error in st_normalize.sfc(x, c(x_range[1], y_range[1], x_range[2], y_range[2])) : domain must have a positive range 

What I've tried:

  • Restarted R and RStudio
  • Re-installed and updated ggplot2 and dplyr

I have not moved my shapefiles to another folder on my computer and they imported into RStudio correctly. I also have defined coord_sf correctly. Like I said, this code worked until the update. Any help would be appreciated.

Phil
  • 7,287
  • 3
  • 36
  • 66
cgxytf
  • 421
  • 4
  • 11
  • What libraries do you have loaded on your system? A few people have faced unexpected problems after upgrading to R 4.0. Perhaps you can look up the github pages of your libraries (in particular, any mapping libraries) to see if anyone has posted any similar issues. Also, your code is not reproducible. Among other things, I dont see any info on your location variable (from: cols = vars(location)) – Piranha Jun 03 '20 at 20:44
  • Sorry, I just fixed that in the question. It was supposed to be "animal" where it said "location". The only libraries I'm using are ggplot2 and ggspatial – cgxytf Jun 03 '20 at 21:10
  • I'm sorry, I don't know how to make it more reproducible as I can't upload the shapefile. – cgxytf Jun 03 '20 at 21:20
  • 1
    @ jl748795I'm having the exact same problem. Opened up a map I made last year and my code is throwing the exact same error. I'm running R 3.6.3 because I'm on an older Mac. I'm wondering if its a package update issue. Please keep us posted on anything you find! – Skiea Oct 21 '20 at 22:16
  • 1
    @Skiea Unfortunately I haven't resolved this. I ended up using ArcMap to make the maps, and then I stitched them together in R. I hope you find a solution! – cgxytf Oct 22 '20 at 16:37
  • I just had this problem after updating only ggplot and dependencies. Strangely, I have on map that does print, and then this appears when I update coordinates. – ohnoplus Mar 19 '21 at 19:04
  • In my case, I seem to have specified the latitude coordinates backward in coord_sf(). No problem before the update, now it breaks the map. In you case you never specify coordinates though. – ohnoplus Mar 19 '21 at 19:08

1 Answers1

0

I made some minor modifications to your code and was able to get this plot using the r version shown below (R version 3.6.3). Is this is what you were expecting to see?

library(tidyverse)
library(ggspatial)

ggplot(data = datexample) +
  geom_point(aes(x = lon, y = lat, alpha = yearweek)) +
  facet_grid(cols = vars(animal)) +
  xlab("Longitude")+
  ylab("Latitude")

[![My Plot][1]][1]

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggspatial_1.1.2 forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.3     readr_1.3.1    
 [7] tidyr_1.0.2     tibble_3.0.0    ggplot2_3.3.0   tidyverse_1.3.0


  [1]: https://i.stack.imgur.com/gfwce.jpg
Piranha
  • 116
  • 6
  • Yes, this is what I was expecting to see, but with my shapefile as a basemap. This code works, but when I apply the shapefile using geom_sf() and coord_sf(), I get the error above. – cgxytf Jun 03 '20 at 21:51
  • 1
    Hmm. Not sure if there is much more I can say here. To me, I see two options for you. (1) temporarily roll back to a prior version of R, and hopefully you can get through the project in the short term (2) I suspect something changed in the syntax of geom_sf and/or coord_sf, and you have to track down that change. Maybe someone else will spot this question and will be able to help. – Piranha Jun 04 '20 at 00:30