简体   繁体   中英

geom_polygon and geom_path won't remove connecting lines

I'm having trouble with plotting polygons and I can't understand why. Here is my code :

#Load coordinates
ices <- read.csv("FILE PATH/ices_coord.csv", sep = ",")
#Reproject to European projection
coordinates(ices)<-c("long","lat")
proj4string(ices) <- CRS("+proj=longlat")
ices_laea<-spTransform(ices, CRS("+proj=laea"))
#Create dataframe
ices_laea_df<-data.frame(ices_laea)

library(ggplot2)
ggplot()+
  geom_polygon(data=ices_laea_df, aes(long,lat,group=group), fill="white", color = "gray70")

I get that map 在此输入图像描述

BUT how do I get rid of the connecting lines (I've colored two in red, but there are more). I was quite sure that using "group=group" or "group=ICES_area" would work, but this is not the case. And I've already lost half my hair :)

Using geom_path instead of geom_polygon gives the same result...

Have you already run into this issue? Do you know the trick to fix it?

Thank you very much for your help,

Fred

PS: you can download the ICES coordinates here

THIS IS NOT AN ANSWER BUT CODE TO ILLUSTRATE:

ices <- readr::read_csv("ices_coord.csv")

purrr::map_lgl(unique(ices$ICES_area), function(area) {
  poly <- dplyr::filter(ices, ICES_area==area)
  identical(poly[1,], poly[nrow(poly),])
})

##  [1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
## [11]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
## [21] FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
## [31]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

Those all need to be TRUE for you to get the nicely connected polygons you're asking for (polygons and paths are ordered point lists with the polygon having the condition that the start & end are the same) and it's going to be virtually impossible to tell that since:

dplyr::count(ices, ICES_area, long, lat) %>% 
  dplyr::filter(n>1) %>% 
  dplyr::ungroup() %>% 
  dplyr::count(ICES_area) %>% 
  dplyr::select(area=1, duplcated_pts=2) %>% 
  print(n=39)
## # A tibble: 39 × 2
##     area duplcated_pts
##    <chr>         <int>
## 1     Ia             1
## 2     Ib             2
## 3    IIa           311
## 4    IIb           322
## 5   IIIa             9
## 6   IIIb             1
## 7   IIIc             1
## 8   IIId            76
## 9    IVa             1
## 10   IVb             1
## 11   IVc             1
## 12   IXa             1
## 13   IXb            10
## 14    Va             6
## 15    Vb            29
## 16   VIa             1
## 17   VIb            21
## 18  VIIa             1
## 19  VIIb             1
## 20  VIIc            21
## 21  VIId             1
## 22  VIIe             1
## 23  VIIf             1
## 24  VIIg             1
## 25  VIIh             1
## 26 VIIIa             1
## 27 VIIIb             1
## 28 VIIIc             1
## 29 VIIId            10
## 30 VIIIe             8
## 31  VIIj            14
## 32  VIIk            66
## 33    Xa            95
## 34    Xb             1
## 35  XIIa           106
## 36  XIIb             1
## 37  XIIc             1
## 38  XIVa             1
## 39  XIVb            65

There are many duplicated points per group for some of the groups.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM