Commit 7fe7c0eb authored by Franssen, Wietse's avatar Franssen, Wietse
Browse files

Ugly implemented a ggmap plot

parent 0ea0bf35
......@@ -5,6 +5,6 @@ Version: 1.0
Date: 2015-02-16
Author: Wietse Franssen
Maintainer: Wietse Franssen <wietse.franssen@wur.nl>
Depends: R (>= 3.0.0), ncdf4, fields
Depends: R (>= 3.0.0), ncdf4, fields, RColorBrewer
Description: Some usefull R-Functions
License: GPL
......@@ -49,6 +49,110 @@ rDataStructure <-function() {
rData$Dates$end<-NA
return(rData)
}
#' Plot a simple map
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
r2ggplotDataStructure <-function(data) {
lonList = array(1, dim=c(length(data$xyCoords$x)*length(data$xyCoords$y)))
latList = array(1, dim=c(length(data$xyCoords$x)*length(data$xyCoords$y)))
i=1
for(y in 1:dim(data$xyCoords$y))
{
for(x in 1:dim(data$xyCoords$x))
{
lonList[i]=data$xyCoords$x[x];
latList[i]=data$xyCoords$y[y];
i=i+1
}
}
ggplotData<-NULL
ggplotData$lon=lonList
ggplotData$lat=latList
ggplotData$data=c(data$Data)
#ggplotFinal<-data.frame(ggplotData)
ggplotFinal<-na.omit(data.frame(ggplotData))
return(ggplotFinal)
}
#' Plot a simple map
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
ggplotje <-function(rdata) {
# theme_update(panel.background = element_rect(fill = "white", colour = NA)
# # ,panel.grid.major = element_line(colour = "grey90", size = 0.2)
# # ,panel.grid.minor = element_line(colour = "grey98", size = 0.5)
# ,panel.grid.major = element_line(colour = "white", size = 0.2)
# ,panel.grid.minor = element_line(colour = "white", size = 0.5)
# ,strip.background = element_rect(fill = "gray80", colour = "gray80")
# )
if (!is.na(rdata$Dates$start[1])) {
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]\n(" ,rdata$Dates$start[1], ")")
} else {
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]")
}
data<-r2ggplotDataStructure(rdata)
# #data<-rdata
# #P <-autoplot(mapBackGround,type = "fixed") +
# p <- ggplot(data = data, aes(x = lon, y = lat)) +
# #xlim(data$lon[1], data$lon[length(data$lon)]) +
# ggtitle(paste0("global discharge","subTitle")) + # plot title
#
# # ylim(data$lat[1], data$lat[length(data$lat)]) +
# theme_bw() +
# # scale_alpha(range=c(0,1), limits=c(0.5,1)) +
# scale_fill_gradient("MAP (mm/yr)", limits=c(260,290)) +
# theme(axis.title.x = element_text(size=16),
# axis.title.y = element_text(size=16, angle=90),
# axis.text.x = element_text(size=14),
# axis.text.y = element_text(size=14),
# panel.grid.major = element_blank(),
# panel.grid.minor = element_blank(),
# legend.position = "right",
# legend.key = element_blank())+
# coord_equal() +
# # labs(x=NULL, y=NULL) +
# xlab(expression(Longitude * ' ' * degree * E * ' ')) +
# ylab(expression(Latitude * ' ' * degree * S * ' ')) +
# #geom_vline(xintercept=seq(7.08, 7.14, by=0.01),ShowGridLines="FALSE") +
# #geom_hline(yintercept=seq(50.93, 50.96, by=0.01),"GridLinesInFront" -> True) +
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")
zp1 <- ggplot(data,
aes(x = lon, y = lat, fill = data))
zp1 <- zp1 + ggtitle(title) # plot title
zp1 <- zp1 + geom_tile(aes(x = lon, y = lat, fill = data))
zp1 <- zp1 + scale_fill_gradientn(colours = myPalette(100))
#zp1 <- zp1 + scale_x_discrete(expand = c(0, 0))
#zp1 <- zp1 + scale_x_discrete(limits=c(ggdata$lon[1], ggdata$lon[length(ggdata$lon)]))
#zp1 <- zp1 + scale_y_discrete(limits=c(ggdata$lat[1], ggdata$lat[length(ggdata$lat)]))
#zp1 <- zp1 + coord_cartesian(xlim = c(-20, 40),ylim = c(ggdata$lat[1], ggdata$lat[length(ggdata$lat)]))
#zp1 <- zp1 + ylim(ggdata$lat[1], ggdata$lat[length(ggdata$lat)])
zp1 <- zp1 + coord_equal()
zp1 <- zp1 + theme_bw()
zp1 <- zp1 + xlab(expression(Longitude * ' ' * degree * E * ' '))
zp1 <- zp1 + ylab(expression(Latitude * ' ' * degree * S * ' '))
zp1 <- zp1 + theme(legend.title=element_blank())
print(zp1)
}
countryMap <- function(lon,lat){
dum = map('worldHires',xlim = c(lon[1],lon[dim(lon)]), ylim = c(lat[1],lat[dim(lat)]), plot = FALSE)
names(dum) <- c("lon", "lat", "range", "names")
countries = data.frame(dum[c("lon","lat")])
#countries$value = mean(d$value)
countries$value = countries$lon
#countries$value = NA
return(countries)
}
ncCheck <-function(ncFile, variable) {
result<-NULL
......
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/WFRTools.R
\name{ggplotje}
\alias{ggplotje}
\title{Plot a simple map}
\usage{
ggplotje(rdata)
}
\description{
Plot a simple map
}
\author{
Wietse Franssen \email{wietse.franssen@wur.nl}
}
\keyword{internal}
......@@ -4,7 +4,8 @@
\alias{ncLoad}
\title{ncLoad}
\usage{
ncLoad(file, varName = NULL, lonlatbox = NULL, timesteps = NULL)
ncLoad(file, varName = NULL, lonlatbox = NULL, timesteps = NULL,
z = NULL)
}
\arguments{
\item{file}{Name of the NetCDF file}
......
......@@ -4,7 +4,8 @@
\alias{ncPlot}
\title{Make a image of the NetCDF data}
\usage{
ncPlot(file, varName = NULL, lonlatbox = NULL, timesteps = NULL)
ncPlot(file, varName = NULL, lonlatbox = NULL, timesteps = NULL,
z = NULL)
}
\value{
Nothing
......
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/WFRTools.R
\name{r2ggplotDataStructure}
\alias{r2ggplotDataStructure}
\title{Plot a simple map}
\usage{
r2ggplotDataStructure(data)
}
\description{
Plot a simple map
}
\author{
Wietse Franssen \email{wietse.franssen@wur.nl}
}
\keyword{internal}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment