문제

Alright, I have a question that I can't find an answer to elsewhere, so I hope you guys can help me.

I want to render the area of former Yugoslavia, and I have a dataset for that. So far I have the following, which works fine:

library(sp)
library(maptools)

greg <- readShapePoly("../GlobalData/GREG/GREG.shp", proj4string=CRS("+proj=longlat  +ellps=WGS84"))

yugo <- greg[greg$COW == 345,]

plot(yugo)

This gives me a map like this:

Map of former Yugoslavia

But I actually only want the upper left part of the map:

Yugoslavia with marked area that I want to retain

I have tried to this by plotting:

plot(yugo, xlim=c(11.8, 12.01), ylim=c(43.6,47))

This looks nice in the preview window of RStudio, but if I change the size of the output, the shown area changes as well.

Has anyone of you an idea of how to fix the shown area to the part that I marked in green in the second picture? That would be extremely nice.

도움이 되었습니까?

해결책

First, the map your are displaying does not run from long = [11.8,12.01]; more like [11.8,17.01]. But that's not your problem.

The problem is this: the plot method for SpatialPolygons fixes the aspect ratio so the map will not be distorted. So when you, say, expand the map vertically (make it taller), the map wants to get wider too, but can't. So it's clipped.

The ggplot package manages this much more cleanly:

library(rgdal)
library(ggplot2)

setwd("<directory with map>")
greg=readOGR(dsn=".",layer="GREG")
yugo <- greg[greg$COW == 345,]

yugo.df <- fortify(yugo)
ggp <- ggplot(yugo.df) 
ggp <- ggp + geom_path(aes(x=long,y=lat,group=group))
ggp <- ggp + coord_fixed()
ggp

ggp + xlim(11.8,17.01) + ylim(43.6,47)

Now try changing the size of the window that contains this map and see what happens.

EDIT [response to OP's comment]

Add this before rendering:

ggp <- ggp + theme_bw()
ggp <- ggp + theme(line = element_blank(), 
                   text = element_blank(), 
                   rect = element_blank())
ggp

You basically want to "turn off" all the default formatting. The call to theme_bw() gives you a black/white color scheme. The calls to element_blank() turn off lines, text, and the bounding rectangle.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top