interference on R map with geojson file - r

I downloaded the GeoJSON file from this link : https://public.opendatasoft.com/explore/dataset/contours-iris/export/
and I plot it, but I have an interference polygone
interference_polygon_picture
but on the web site, it shows the polygons and there is not this polygone :
https://public.opendatasoft.com/explore/dataset/contours-iris/map/?q=gers&location=13,43.65999,0.5815&basemap=jawg.streets
I have ever tried to map with another file in shx from another website, and I have the same problem near the same town (Auch)
How can I delete it? Is it a problem with my R code?
Thank you for your help
iris.poly2 <- readOGR(dsn="contours-iris.geojson", "OGRGeoJSON")
ggplot() +
geom_polygon(data=iris.poly2, aes(x=long, y=lat, group=id), color="grey", size=0.1) +
coord_map(xlim=c(0.4627843, 0.6773605),
ylim=c(43.58996 , 43.72575) )

Solved with replacing group=id by group=group
ggplot() +
geom_polygon(data=iris.poly2, aes(x=long, y=lat, group=group), color="grey", size=0.1) +
coord_map(xlim=c(0.4627843, 0.6773605),
ylim=c(43.58996 , 43.72575) )
group=id created a link between 3 polygons => the interference polygon

Related

Connecting all dots on a ggplot2 map to a single point [in R]

I have finally managed to make a map which includes points for a range of long,lat coordinates.
I have used the following code to make the map
ggplot() +
geom_polygon(data=wmap_laea_df, aes(long,lat,group=group), fill="white")+
geom_path(data=countries_laea_df, aes(long,lat, group=group), color="light grey",
size=0.1) +
geom_point(data=loc_laea_df, aes(V1, V2, group=NULL,fill=NULL,size=V3),
color="black",alpha=I(6/10)) +
scale_size(range=c(1,2.5), guide = "legend",labs(size="Count")) +
coord_cartesian(xlim = c((xmin-buff),(xmax+buff)), ylim = c((ymin-buff),(ymax+buff))) +
theme(aspect.ratio=1)+
theme_opts
This produces the following plot
[![enter image description here][1]][1]
I am now trying to connect the dots, to a single point (the Northern most point in the Scotland). The only thing I could think of was to use the following line:
geom_line(data=loc_laea_df, aes(x=V1, y=V2), color="black", size=1)
This produces the following:
[![enter image description here][2]][2]
This however is not the outcome I had hoped for, but feel I am on the way.
Could anyone point me in the direction of how to do that?
EDIT:
For anyone seeing this later, I found a great solution described here:
R ggplot connecting one point on a map with multiple points on the same map

R - plotly - ggplot

I found the following example for plotting a map with Canadian cities: https://plot.ly/ggplot2/maps/
The R code reads as follows:
library(plotly)
Sys.setenv("plotly_username"="XXXXXXXXX")
Sys.setenv("plotly_api_key"="YYYYYYYYY")
data(canada.cities, package="maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
borders(regions="canada", name="borders") +
coord_equal() +
geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")
ggplotly()
I would like to modify the example in a way that the name of the city shows up when hovering with the mouse over the relevant point in the map.
How would I need to modify the above example to implement this?
This ended up being a bug, so thanks for reporting! I just issued a fix here so try re-installing (devtools::install_github("ropensci/plotly")) and re-running:
data(canada.cities, package="maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
borders(regions="canada", name="borders") +
coord_equal() +
geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")
ggplotly()
Here's a screenshot, with the custom hover text!

Map in ggplot2 visualization/displaying bug?

As you can see below, there is a weird displaying problem on the maps I made using ggplots. The same problem seems to happen with any projection.
Here is the code:
Only the packages maps and ggplot2 are needed
mapWorld <- borders("world", colour="gray50", fill="black")
ggplot() + mapWorld +
coord_map("mercator") +
ylim(-90,90)
Apparently the problem is caused by the polygons that cross the 0 coordinate, the place in which the world merges. R dont knows how to close those polygons and projects them all around the world.
This method recreates the polygons and prevents them from crossing the 0 coordinate (xlim and ylim). It works with any kind of projection.
require(ggplot2)
require(PBSmapping)
require(data.table)
mapWorld <- map_data("world")
setnames(mapWorld, c("X","Y","PID","POS","region","subregion"))
worldmap = clipPolys(mapWorld, xlim=xlim,ylim=ylim, keepExtra=TRUE)
ggplot() + geom_polygon(data = mapWorld, aes(X,Y,group=PID))
why you need to use?
ggplot() + mapWorld +
coord_map("mercator") +
ylim(-90,90)
if u use just
ggplot() + mapWorld
It works perfectly

Fix antarctica on a ggplot world map?

I want to plot a simple world map with gpplot, but when I do, antarctica gets cut off, because the coordinates don't wrap around, so the path goes back across the whole map, instead of going out the edges. For example:
world_data <- map_data("world")
ggplot() + scale_y_continuous(limits=c(-90,90), expand=c(0,0)) +
scale_x_continuous(expand=c(0,0)) +
theme(axis.ticks=element_blank(), axis.title=element_blank(),
axis.text=element_blank()) +
geom_polygon(data=world_data, mapping=aes(x=long, y=lat, group=group), fill='grey')
Produces:
But the sounthern most part of antarctica is missing - it should look like this:
Is there a simple way to fix this problem?
The wrld_simpl data file from the maptools package seems to have more reliable map data, including data for Antarctica that goes all the way to -90 degrees latitude. For example:
library(maptools)
data(wrld_simpl)
ggplot() +
geom_polygon(data=wrld_simpl,
aes(x=long, y=lat, group=group), fill='grey20') +
coord_cartesian(xlim=c(-180,180), ylim=c(-90,90)) +
scale_x_continuous(breaks=seq(-180,180,20)) +
scale_y_continuous(breaks=seq(-90,90,10))
Hi #eipi10: your code does not work well when setting coord_map(). Antarctica looks weird.
ggplot() +
geom_polygon(data=fortify(wrld_simpl),
aes(x=long, y=lat, group=group), fill='grey20') +
coord_map(xlim=c(-180, 180), ylim=c(-90, 90)) +
scale_x_continuous(breaks=seq(-180, 180, 20)) +
scale_y_continuous(breaks=seq(-90, 90, 10))
Actually I found that most built-in world maps in R packages such as mapdata, maptools and maps don't work correctly with coord_map(). Highly appreciate it if someone can figure it out.

Choropleth map in ggplot with polygons that have holes

I'm trying to draw a choropleth map of Germany showing poverty rate by state (inspired by this question).
The problem is that some of the states (Berlin, for example) are completely surrounded by other states (Brandenburg), and I'm having trouble getting ggplot to recognize the "hole" in Brandenburg.
The data for this example is here.
library(rgdal)
library(ggplot2)
library(RColorBrewer)
map <- readOGR(dsn=".", layer="germany3")
pov <- read.csv("gerpoverty.csv")
mrg.df <- data.frame(id=rownames(map#data),ID_1=map#data$ID_1)
mrg.df <- merge(mrg.df,pov, by="ID_1")
map.df <- fortify(map)
map.df <- merge(map.df,mrg.df[,c("id","poverty")], by="id")
ggplot(map.df, aes(x=long, y=lat, group=group)) +
geom_polygon(aes(fill=poverty))+
geom_path(colour="grey50")+
scale_fill_gradientn(colours=brewer.pal(5,"OrRd"))+
labs(x="",y="")+ theme_bw()+
coord_fixed()
Notice how the colors for Berlin and Brandenburg (in the northeast) are identical. They shouldn't be - Berlin's poverty rate is much lower than Brandenburg. It appears that ggplot is rendering the Berlin polygon and then rendering the Brandenburg polygon over it, without the hole.
If I change the call to geom_polygon(...) as suggested here, I can fix the Berlin/Brandenburg problem, but now the three northernmost states are rendered incorrectly.
ggplot(map.df, aes(x=long, y=lat, group=group)) +
geom_polygon(aes(group=poverty, fill=poverty))+
geom_path(colour="grey50")+
scale_fill_gradientn(colours=brewer.pal(5,"OrRd"))+
labs(x="",y="")+ theme_bw()+
coord_fixed()
What am I doing wrong??
This is just an expansion on #Ista's answer, which does not require that one knows which states (Berlin, Bremen) need to be rendered last.
This approach takes advantage of the fact that fortify(...) generates a column, hole which identifies whether a group of coordinates are a hole. So this renders all regions (id's) with any holes before (e.g. underneath) the regions without holes.
Many thanks to #Ista, without whose answer I could not have come up with this (believe me, I spent many hours trying...)
ggplot(map.df, aes(x=long, y=lat, group=group)) +
geom_polygon(data=map.df[map.df$id %in% map.df[map.df$hole,]$id,],aes(fill=poverty))+
geom_polygon(data=map.df[!map.df$id %in% map.df[map.df$hole,]$id,],aes(fill=poverty))+
geom_path(colour="grey50")+
scale_fill_gradientn(colours=brewer.pal(5,"OrRd"))+
labs(x="",y="")+ theme_bw()+
coord_fixed()
You can plot the island polygons in a separate layer, following the example on the ggplot2 wiki. I've modified your merging steps to make this easier:
mrg.df <- data.frame(id=rownames(map#data),ID_1=map#data$ID_1)
mrg.df <- merge(mrg.df,pov, by="ID_1")
map.df <- fortify(map)
map.df <- merge(map.df,mrg.df, by="id")
ggplot(map.df, aes(x=long, y=lat, group=group)) +
geom_polygon(aes(fill=poverty), color = "grey50", data =subset(map.df, !Id1 %in% c("Berlin", "Bremen")))+
geom_polygon(aes(fill=poverty), color = "grey50", data =subset(map.df, Id1 %in% c("Berlin", "Bremen")))+
scale_fill_gradientn(colours=brewer.pal(5,"OrRd"))+
labs(x="",y="")+ theme_bw()+
coord_fixed()
As an unsolicited act of evangelism, I encourage you to consider something like
library(ggmap)
qmap("germany", zoom = 6) +
geom_polygon(aes(x=long, y=lat, group=group, fill=poverty),
color = "grey50", alpha = .7,
data =subset(map.df, !Id1 %in% c("Berlin", "Bremen")))+
geom_polygon(aes(x=long, y=lat, group=group, fill=poverty),
color = "grey50", alpha= .7,
data =subset(map.df, Id1 %in% c("Berlin", "Bremen")))+
scale_fill_gradientn(colours=brewer.pal(5,"OrRd"))
to provide context and familiar reference points.
Just to add another small improvement to #Ista's and #jhoward's answers (thanks a lot for your help!).
The modification of #jhoward could be easily wrapped in a small function like this
gghole <- function(fort){
poly <- fort[fort$id %in% fort[fort$hole,]$id,]
hole <- fort[!fort$id %in% fort[fort$hole,]$id,]
out <- list(poly,hole)
names(out) <- c('poly','hole')
return(out)
}
# input has to be a fortified data.frame
Then, one doesn't need to recall every time how to extract holes info. The code would look like
ggplot(map.df, aes(x=long, y=lat, group=group)) +
geom_polygon(data=gghole(map.df)[[1]],aes(fill=poverty),colour="grey50")+
geom_polygon(data=gghole(map.df)[[2]],aes(fill=poverty),colour="grey50")+
# (optionally). Call by name
# geom_polygon(data=gghole(map.df)$poly,aes(fill=poverty),colour="grey50")+
# geom_polygon(data=gghole(map.df)$hole,aes(fill=poverty),colour="grey50")+
scale_fill_gradientn(colours=brewer.pal(5,"OrRd"))+
labs(x="",y="")+ theme_bw()+
coord_fixed()
Alternatively you could create that map using rworldmap.
library(rworldmap)
library(RColorBrewer)
library(rgdal)
map <- readOGR(dsn=".", layer="germany3")
pov <- read.csv("gerpoverty.csv")
#join data to the map
sPDF <- joinData2Map(pov,nameMap='map',nameJoinIDMap='VARNAME_1',nameJoinColumnData='Id1')
#default map
#mapPolys(sPDF,nameColumnToPlot='poverty')
colours=brewer.pal(5,"OrRd")
mapParams <- mapPolys( sPDF
,nameColumnToPlot='poverty'
,catMethod="pretty"
,numCats=5
,colourPalette=colours
,addLegend=FALSE )
do.call( addMapLegend, c( mapParams
, legendLabels="all"
, legendWidth=0.5
))
#to test state names
#text(pov$x,pov$y,labels=pov$Id1)

Resources