Commit 2d3dc436 authored by Franssen, Wietse's avatar Franssen, Wietse
Browse files

some ggplotje improvements

parent 22e3e545
......@@ -119,29 +119,74 @@ r2ggplotDataStructure <-function(data, timestep = 1) {
#' @author Wietse Franssen \email{wietse.franssen@@wur.nl}
#' @keywords internal
#' @export
ggplotje <-function(rdata,lon = NULL, lat = NULL,title=NULL ) {
ggplotjeold <-function(rdata,lon = NULL, lat = NULL,title=NULL ) {
if (is.null(title)) {
if (!is.na(rdata$Dates$start[1])) {
# title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]\n(" ,rdata$Dates$start[1], ")")
if (!is.na(rdata$Dates$start[1])) {
# title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]\n(" ,rdata$Dates$start[1], ")")
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]\n(" ,as.character(format( as.Date(rdata$Dates$start[1]),format="%d %B %Y")), ")")
} else {
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]")
}
}
data<-r2ggplotDataStructure(rdata)
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")
# myPalette <- colorRampPalette(rev(rainbow(100)), space="Lab")
zp1 <- ggplot(data,
aes(x = lon, y = lat, fill = data)) +
ggtitle(title) + # plot title
geom_tile(aes(x = lon, y = lat, fill = data)) +
scale_fill_gradientn(colours = myPalette(100)) +
# scale_fill_gradientn(colours = myPalette(100), limits=c(-1,1)) +
#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)])
coord_equal() +
theme_bw() +
xlab(expression(Longitude * ' ' * degree * E * ' ')) +
ylab(expression(Latitude * ' ' * degree * N * ' ')) +
theme(legend.title=element_blank())
# xlab(expression(Longitude * ' ' * degree * E * ' ')) +
# ylab(expression(Latitude * ' ' * degree * S * ' ')) +
if (!is.null(lon) && !is.null(lat)) {
zp1 <- zp1 + scale_shape_identity() + geom_point( colour="black", size = 10, aes_string(x = lon, y = lat, shape=3))
zp1 <- zp1 + geom_point( fill="red",colour="black", size = 6, aes_string(x = lon, y = lat, shape=18))
}
print(zp1)
return(zp1)
}
ggplotje <-function(rdata,lon = NULL, lat = NULL,title=NULL, barLimits=NULL, barDiff=FALSE ) {
if (is.null(title)) {
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]\n(" ,as.character(format( as.Date(rdata$Dates$start[1]),format="%d %B %Y")), ")")
} else {
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]")
if (!is.na(rdata$Dates$start[1])) {
# title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]\n(" ,rdata$Dates$start[1], ")")
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]\n(" ,as.character(format( as.Date(rdata$Dates$start[1]),format="%d %B %Y")), ")")
} else {
title=paste0(rdata$Variable$longName, " [",rdata$Variable$units,"]")
}
}
if (!is.null(barLimits)) {
rdata$Data[rdata$Data <= barLimits[1]]<- barLimits[1]
rdata$Data[rdata$Data > barLimits[2]]<-barLimits[2]
}
data<-r2ggplotDataStructure(rdata)
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")
# myPalette <- colorRampPalette(rev(rainbow(100)), space="Lab")
# myPalette <- colorRampPalette(rev(rainbow(100)), space="Lab")
zp1 <- ggplot(data,
aes(x = lon, y = lat, fill = data)) +
ggtitle(title) + # plot title
geom_tile(aes(x = lon, y = lat, fill = data)) +
scale_fill_gradientn(colours = myPalette(100)) +
# scale_fill_gradientn(colours = myPalette(100), limits=c(-1,1)) +
#zp1 <- zp1 + scale_x_discrete(expand = c(0, 0))
#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)]))
......@@ -152,8 +197,22 @@ ggplotje <-function(rdata,lon = NULL, lat = NULL,title=NULL ) {
ylab(expression(Latitude * ' ' * degree * N * ' ')) +
theme(legend.title=element_blank())
# xlab(expression(Longitude * ' ' * degree * E * ' ')) +
# ylab(expression(Latitude * ' ' * degree * S * ' ')) +
# xlab(expression(Longitude * ' ' * degree * E * ' ')) +
# ylab(expression(Latitude * ' ' * degree * S * ' ')) +
# library(scales)
if (is.null(barLimits)) {
if (barDiff==TRUE) {
zp1 <- zp1 + scale_fill_gradient2(low=muted("red"), high=muted("blue"))
} else {
zp1 <- zp1 + scale_fill_gradientn(colours = myPalette(100))
}
} else {
if (barDiff==TRUE) {
zp1 <- zp1 + scale_fill_gradient2(limits=barLimits, low=muted("red"), high=muted("blue"))
} else {
zp1 <- zp1 + scale_fill_gradientn(colours = myPalette(100),limits=barLimits)
}
}
if (!is.null(lon) && !is.null(lat)) {
zp1 <- zp1 + scale_shape_identity() + geom_point( colour="black", size = 10, aes_string(x = lon, y = lat, shape=3))
......
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/WFRTools.R
\name{ggplotje}
\alias{ggplotje}
\name{ggplotjeold}
\alias{ggplotjeold}
\title{Plot a simple map}
\usage{
ggplotje(rdata, lon = NULL, lat = NULL, title = NULL)
ggplotjeold(rdata, lon = NULL, lat = NULL, title = NULL)
}
\arguments{
\item{file}{Name of the NetCDF file}
......
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