ggplot2 - spatial plotting Norsk statistikermøte, Halden, 11. juni 2013
description
Transcript of ggplot2 - spatial plotting Norsk statistikermøte, Halden, 11. juni 2013
www.nr.no
ggplot2 - spatial plottingNorsk statistikermøte, Halden, 11. juni 2013
André TeiglandForskningssjefSAMBA
www.nr.no
Elisabeth OrskaugThordis Thorarinsdottir
Norsk Regnesentral
Spatial visualization with qplot()► Using functions in R, one can plot basic
geographic information, for instance a shape file containing polygons for areal data.
► Areal data is data which corresponds to geographical extents with polygonal boundaries.
Section 4.1
2/20
4. ggplot2-spatial
Spatial visualization with qplot()Taking out one area from world map˃ ind = which(world[,3]==1)> qplot(long,lat,data=world[ind,],geom="path",
group=group) + xlim(range(world[,1])) + ylim(range(world[,2]))
4. ggplot2-spatial
Section 4.2
Example: world data set long lat group order region subregion1 -133.3664 58.42416 1 1 Canada <NA>2 -132.2681 57.16308 1 2 Canada <NA>3 -132.0498 56.98610 1 3 Canada <NA>4 -131.8797 56.74001 1 4 Canada <NA>5 -130.2492 56.09945 1 5 Canada <NA>6 -130.0131 55.91169 1 6 Canada <NA>
3/20
Spatial visualization with qplot()Plot map with function qplot()˃ qplot(long,lat,data=world,geom="point",
group=group)
Section 4.3
geom="point" geom="path" geom="polygon" geom="polygon", fill=lat
4/20
4. ggplot2-spatial
Spatial visualization with qplot()Zoom in on locations in the map:˃ qplot(long, lat, data=world, geom="path", group
= group) + xlim(c(0,35)) + ylim(c(50,75))
Section 4.4
5/20
4. ggplot2-spatial
Assignment
> Make a map that zooms in on Afrika, and fill the area with red color. The longer north, the darker color. (You can try to change the fill color also for instance to red. Hint: ?scale_fill_gradient )
Section 4.5
6/20
4. ggplot2-spatial
Suggestion˃ qplot(long, lat, data=world, geom="polygon", group = group,fill=lat)
+ xlim(c(-30,70)) + ylim(c(-40,50)) + scale_fill_gradient(low = "white", high = "dark red")
7/20
4. ggplot2-spatial
Spatial visualization with ggplot()Plotting map:> map = geom_path(aes(x=long, y=lat, group =
group), data=world)˃ ggplot() + map
Section 4.6
8/20
4. ggplot2-spatial
Spatial visualization with ggplot()Plotting points on a map:> ggplot() + map +
geom_point(aes(x=lon,y=lat),colour="green", data=norwayGrid)
Section 4.7
9/20
4. ggplot2-spatial
Spatial visualization with ggplot()Zoom in on locations in the map:1. ggplot() + map +
geom_point(aes(x=lon,y=lat),colour="green", data=norwayGrid) + xlim(range(norwayGrid$lon)) + ylim(range(norwayGrid$lat))
2. map = list(geom_path(aes(x=long, y=lat, group = group), colour="grey50", data=world), scale_x_continuous(limits=range(norwayGrid$lon)), scale_y_continuous(limits = range(norwayGrid$lat)) )
Section 4.8
10/20
4. ggplot2-spatial
Assignment
˃ Zoom in on Australia with ggplot() using data from data set world. Hint: use region=="Australia"
Section 4.9
11/20
4. ggplot2-spatial
Suggestion> ind = which(world$region=="Australia")˃ ggplot() + map + xlim(range(world[ind,1])) +
ylim(range(world[ind,2]))
12/20
4. ggplot2-spatial
Spatial map with time seriesPlotting precipitation over the norwegian mainland, both model and observations:Section 4.10
1) qplot(lon, lat, data=all.stats, colour=obs.mean, geom="point", xlab="", ylab="", main="Observed") + scale_colour_gradient(name="precip", limits=c(0,20), low="white", high="green") + map
2) qplot(lon, lat, data=all.stats, colour=mod.mean, geom="point", xlab="", ylab="", main="Model") + scale_colour_gradient(name="precip", limits=c(0,20), low="white", high="green") + map
13/20
4. ggplot2-spatial
Assignment
> Make a spatial map of the difference in precipitation in 3.-quartile (Q3) for observed data and model data. Choose blue color scale.
Section 4.11
14/20
4. ggplot2-spatial
Suggestion˃ qplot(lon, lat, data=all.stats, colour=abs(obs.Q3-mod.Q3),
geom="point", xlab="", ylab="", main="Difference precipitation") + scale_colour_gradient(name="precip", limits=c(0,11), low="white", high="blue") + map
15/20
4. ggplot2-spatial
Spatial map with time seriesObservations with time series in each grid point:> qplot(lon+time_r/5, lat+OBS_r/5,
data=data.grid.mth.agg, xlab="", ylab="", group=interaction(lon, lat), size=I(0.5), xlim=c(4.8, 35), ylim=c(57.5, 72)) + map
Section 4.12
16/20
4. ggplot2-spatial
Spatial map with time seriesSplit Norway in two:˃ Adjust xlim and ylim.
Section 4.13
17/20
4. ggplot2-spatial
Assignment
> Make a spatial map with seasonal time series of the observations, and split the map in two. Hint: use month_r
Section 4.14
18/20
4. ggplot2-spatial
Suggestion1) qplot(lon+month_r/2.5, lat+OBS_r/2.5, data=data.grid.mth.agg, xlab="", ylab="", group=interaction(lon, lat), size=I(0.5), xlim=c(4.5, 15), ylim=c(57.5, 66)) + map
2) qplot(lon+month_r/2.5, lat+OBS_r/2.5, data=data.grid.mth.agg, xlab="", ylab="", group=interaction(lon, lat), size=I(0.5), xlim=c(14.5, 31), ylim=c(66, 71.5)) + map
19/20
4. ggplot2-spatial
www.nr.no
André TeiglandForskningssjefSAMBA
www.nr.no
Coffee break