Jd Baba
Jd Baba

Reputation: 6118

Clip the contour with polygon using ggplot and R

I want to create a contour and then clip the contour by the polygon and only show the contour within the polygon.

Shapefile data can be found here

Csv file can be found here

The code I used is as follows:

library("ggplot2")
library("rgdal")
library("gpclib")
library("maptools")
require(sp) 

age2100 <- read.csv("temp.csv",header=TRUE, sep=",")
shape.dir <- "C:/Users/jdbaba/Documents/R working folder/shape" # use your directory name here

lon.shape <- readOGR(shape.dir, layer = "Export_Output_4")
str(lon.shape)

lon.df <- fortify(lon.shape, region = "Id")
p <- ggplot(lon.df, aes(x = long, y = lat, group = group)) +
    geom_polygon(colour = "black", fill = "grey80", size = 1) +
    theme() 

p <- p + geom_point(data=age2100,aes(x=age2100$x,y=age2100$y,group="z"),size=0.1)
p <- p + geom_density2d(colour="red")
p

Here, I have created the map, points and the contour. I don't know whether the code I am using created the contour for variable z or not. If it is not correct can anyone suggest me ?

The sample output that I got is as follows:

enter image description here

Now, I want to clip the contour within the polygon and hide the part of contour that is outside the polygon.

I want to know how to add the labels to the contour and control the contour interval.

Please let me know if my question is not clear.

Thanks

Jdbaba

Upvotes: 3

Views: 2976

Answers (1)

Spacedman
Spacedman

Reputation: 94182

I can't reproduce your map exactly. The code you provided gives me a map with two sets of contours - one that looks like yours and one that overlaps it in the southern part of the region. I suspect this is an artefact of your group setting. Also, I can see there is an island in the southern part of what I assume is the lake.

enter image description here

I like to clean up and partition my ggplot stuff into bits, since I often find something in an early part of a ggplot call confuses something in a later part. Here's how I would map the region, draw points, and then add a density contour:

map <- function(){
  geom_polygon(data=lon.df,aes(x=long,y=lat,group=piece),colour="black",fill="grey80",size=1)
}

points <- function(){
  geom_point(data=age2100,aes(x=x,y=y),size=0.1)
}

density <- function(){
  geom_density2d(data=age2100,aes(x=x,y=y),colour="red")
}

ggplot()+map() +points() +density()

Which gives this:

enter image description here

Now that's much different to what your contour looks like, and I don't know why. Maybe your group parameter is grouping all the points with the same z?

Anyway, it seems you don't want a density plot, you want a map of your Z values over your area. This is going to need kriging or some other interpolation technique. Forget about ggplot for a while, concentrate on the numbers.

For starters, plot the points coloured by the z value. You should see this:

enter image description here

which at least will give you a good idea of what the correct contour will look like.

Anyway, this is getting into a full-on tutorial..

Upvotes: 2

Related Questions