OpasnetUtils/Drafts: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(→‎Answer: binoptest function for locating problem in ovariable indices)
(rcode answer split into meaningful parts)
Line 13: Line 13:
Call the objects stored by this code from another rode with this command:
Call the objects stored by this code from another rode with this command:


  objects.latest("Op_en6007", code_name = "answer")
  objects.latest("Op_en6007", code_name = "answer") # Old version that fetches all objects, depreciated and not updated.
objects.latest("Op_en6007", code_name = "diagnostics") # Functions for ovariable and model diagnostics: ovashapetest, showLoctable, binoptest
objects.latest("Op_en6007", code_name = "webropol") # Functions for operating with Webropol data
objects.latest("Op_en6007", code_name = "miscellaneous") # Functions for various tasks
objects.latest("Op_en6007", code_name = "gis") # Functions for ovariable, KML and Googl maps interactions


<rcode name="answer" embed=1 store=1>
== Rationale ==
 
=== Calculations ===
 
'''Functions for ovariable diagnostics
 
<rcode name="diagnostics" embed=1>
#This is code Op_en6007/diagnostics on page [OpasnetUtils/Drafts]]


library(OpasnetUtils)
library(OpasnetUtils)
############## Generic functions and objects are defined first.


#####################################
#####################################
Line 67: Line 76:
   return(loctable)
   return(loctable)
}
}
objects.store(binoptest, showLoctable, ovashapetest)
cat("Functions binoptest, showLoctable, ovashapetest stored.\n")
</rcode>
'''Functions for Webropol data
<rcode name="webropol" embed=1>
#This is code Op_en6007/webropol on page [OpasnetUtils/Drafts]]
library(OpasnetUtils)


### webropol.convert converts a csv file from Webropol into a useful data.frame.
### webropol.convert converts a csv file from Webropol into a useful data.frame.
Line 127: Line 147:
   return(dat)
   return(dat)
}
}
objects.store(webropol.convert, merge.questions)
cat("Functions webropol.convert, merge.questions stored.\n")
</rcode>
'''Miscellaneous functions
<rcode name="miscellaneous" embed=1>
#This is code Op_en6007/webropol on page [OpasnetUtils/Drafts]]
library(OpasnetUtils)


############ Shuffles columns of a data.frame so that they match a pre-defined correlation matrix
############ Shuffles columns of a data.frame so that they match a pre-defined correlation matrix
Line 167: Line 199:
}
}
return(cat("Decisions were forgotten.\n"))
return(cat("Decisions were forgotten.\n"))
}
#################### Perus-ggplot ovariableille
oggplot <- function (
ova, # ovariable to be plotted
x, # Index for x axis
weight = NULL, # Index for y axis (default: result column)
fill = NULL, # Index for colour code
base_size = NULL, # Base size for graph font (object BS or 18 is used if not given)
turnx = FALSE, # Turn x axis labels vertically?
binwidth = NULL # Width of bins
)
{
if(is.null(base_size)) if(exists("BS")) base_size <- BS else base_size <- 18
if(exists("translate")) {
ova <- translate(ova)
x <- translate(x)
weight <- translate(weight)
fill <- translate(fill)
}
if (is.null(weight))
weight <- paste(ova@name, "Result", sep = "")
if("Iter" %in% colnames(ova@output))
ova <- oapply(ova, cols = "Iter", FUN = mean)
plo <- ggplot(ova@output, aes_string(x = x, fill = fill, weight = weight)) +
theme_gray(base_size = base_size) 
if(any(ova@output[[weight]] >= 0)) {
plo <- plo + geom_bar(
data = subset(ova@output, ova@output[[weight]] >= 0),
position = "stack",
binwidth = binwidth
)
}
if(any(ova@output[[weight]] < 0)) {
plo <- plo + geom_bar(
data = subset(ova@output, ova@output[[weight]] < 0),
position = "stack",
binwidth = binwidth
) + geom_hline(aes(yintercept = 0))
}
if (turnx)
plo <- plo + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
return(plo)
}
}


Line 235: Line 222:
#DateTime <- as.POSIXct(paste(temperature$Date, temperature$Time), format="%Y-%m-%d %H:%M:%S")
#DateTime <- as.POSIXct(paste(temperature$Date, temperature$Time), format="%Y-%m-%d %H:%M:%S")
#DateHours <- as.POSIXct(paste(price$Date, price$Hours), format="%Y-%m-%d %H:%M:%S")
#DateHours <- as.POSIXct(paste(price$Date, price$Hours), format="%Y-%m-%d %H:%M:%S")
ograph <- function( # Määritellään yleisfunktio peruskuvaajan piirtämiseen.
ovariable,
x,
y = character(),
type = character(),
other = character(),
fill = NA,
...
) {
cat("This function ograph is depreciated. Use oggplot instead.\n")
if(class(ovariable) == "ovariable")  {
if(nrow(ovariable@output) == 0) ovariable <- EvalOutput(ovariable)
data <- ovariable@output
title <- ovariable@name
if(length(y) == 0) y <- paste(title, "Result", sep = "")
} else {
data <- ovariable
title <- character()
if(length(y) == 0) y <- "Result"
}
if(length(type) == 0) {
if("Iter" %in% colnames(data)) type <- geom_boxplot() else type <- geom_bar(stat = "identity")
}
out <- ggplot(data, aes_string(x = x, y = y, fill = fill)) # Määritellään kuvan sarakkeet
out <- out + type
out <- out + theme_grey(base_size=24) # Fontin kokoa suurennetaan
out <- out + labs(
title = title,
y = paste(unique(data[[paste(title, "Yksikkö", sep = "")]]), sep = "", collapse = ", ")
)
out <- out + theme(axis.text.x = element_text(angle = 90, hjust = 1)) # X-akselin tekstit käännetään niin että mahtuvat
if(length(other) != 0) out <- out + other
return(out)
}


# fillna takes a data.frame and fills the cells with NA with each level in that column.
# fillna takes a data.frame and fills the cells with NA with each level in that column.
Line 314: Line 265:
return(variable)
return(variable)
}
MyPointKML <- function( # The function creates a KML fille from a SpatialPointsDataFrame object.
obj = NULL, # Spatial object with the data. A SpatialPointsDataFrame.
kmlname = "", # Name of the KML fille (does this show on the map?)
kmldescription = "", # Description of the KML fille (does this show on the map?)
name = NULL, # Name for each datapoint (vector with the same length as data in obj).
description = "", # Descrtion of each datapoint (vector with the same length as data in obj).
icon = "http://maps.google.com/mapfiles/kml/pal4/icon24.png", # Icon shown on pin (?)
col=NULL # I don't know what this does.
) {
cat("This function MyPointKML is depreciated. Use google.point_kml in OpasnetUtilsExt instead.\n")
    if (is.null(obj))
        return(list(header = c("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
            "<kml xmlns=\"http://earth.google.com/kml/2.2\">",
            "<Document>", paste("<name>", kmlname, "</name>",
                sep = ""), paste("<description><![CDATA[", kmldescription,
                "]]></description>", sep = "")), footer = c("</Document>",
            "</kml>")))
    if (class(obj) != "SpatialPointsDataFrame")
        stop("obj must be of class 'SpatialPointsDataFrame' [package 'sp']")
    if (is.null(name)) {
        name = c()
        for (i in 1:nrow(obj)) name <- append(name, paste("site",
            i))
    }
    if (length(name) < nrow(obj)) {
        if (length(name) > 1)
            warning("kmlPoints: length(name) does not match nrow(obj). The first name will be replicated.")
        name <- rep(name, nrow(obj))
    }
    if (length(description) < nrow(obj)) {
        if (length(description) > 1)
            warning("kmlPoints: length(description) does not match nrow(obj). The first description will be replicated.")
        description <- rep(description, nrow(obj))
    }
    if (length(icon) < nrow(obj)) {
        if (length(icon) > 1)
            warning("kmlPoints: length(icon) does not match nrow(obj). Only the first one will be used.")
        icon <- icon[1]
    }
# This is some kind of a colour definition
col2kmlcolor <- function(col)
paste(rev(sapply(
col2rgb(col, TRUE),
function(x) sprintf("%02x", x))
), collapse = "")
    kml <- kmlStyle <- ""
   
# Create the KML fille.
kmlHeader <- c("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","<kml xmlns=\"http://earth.google.com/kml/2.2\">", "<Document>")
    kmlFooter <- c("</Document>", "</kml>")
   
# Create rows to the KML fille from data in obj.
    for (i in 1:nrow(obj)) {
        point <- obj[i, ]
        pt_style <- paste("#style", ifelse(length(icon) == 1, 1, i), sep = "")
        kml <- append(kml, "<Placemark>")
        kml <- append(kml, paste(
"  <description><![CDATA[",
name[i],
": ",
description[i],
"]]></description>",
sep = ""
))
#kml <- append(kml, "<Style><IconStyle>")
#kml <- append(kml, paste("<color>", col2kmlcolor(col[i]), "</color>", sep =""))
#kml <- append(kml, paste("  <Icon><href>", icon, "</href></Icon>", sep = ""))
#kml <- append(kml, "<scale>0.300000</scale>")
#kml <- append(kml, "</IconStyle></Style>")
        kml <- append(kml, "  <Point>")
        kml <- append(kml, "    <coordinates>")
        kml <- append(kml, paste(point@coords[1], point@coords[2], sep = ","))
        kml <- append(kml, "    </coordinates>")
        kml <- append(kml, "  </Point>")
        kml <- append(kml, "</Placemark>")
    }
   
    return(paste(paste(c(kmlHeader, kmlStyle, kml, kmlFooter), sep = "", collapse = "\n"), collapse="\n", sep = ""))
}
ova2spat <- function( # This function converts an ovariable or a data.frame into a SpatialPointsDataFrame.
  dat, # An evaluated ovariable or data.frame that has coordinate indices.
  coords = c("LO", "LA"), # The names of the coordinate indices as a character vector, first x then y.
  proj4string = NULL # Projection identifier or specification as character string. See http://spatialreference.org/
  # If proj4string is NULL, longitude-latitude system is assumed.
) {
  if(class(dat) == "ovariable") temp <- dat@output else
    if(is.data.frame(dat)) temp <- dat else
      stop("object must be either evaluated ovariable or data.frame\n")
 
  # Transform coordinates into numeric format.
 
  for(i in coords) {
    temp[[i]] <- as.numeric(as.character(temp[[i]]))
  }
 
  # Define the coordinate points first, then add other ovariable output to it.
 
  if(is.null(proj4string)) {
    sp <- SpatialPoints(temp[coords], CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
    } else {
      sp <- SpatialPoints(temp[coords], CRS(proj4string))
    }
  out <- SpatialPointsDataFrame(sp, temp[!colnames(temp) %in% coords])
 
  #Transform the projection to longitude-latitude system.
  if(!is.null(proj4string)) {
    epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
    out <- spTransform(out,epsg4326String)
  }
 
  return(out)
}
# MyRmap is a function for creating static Google maps as png.
# It is based on MyMap function without the "file destination" parameter
# Requires RgoogleMaps package
MyRmap <- function (
shp, # a spatial data object
plotvar, # Name of the column that has the values to be illustrated on the map
pch = 19, # Shape of the point (19: circle)
cex = 0.3, # Size of the point
legend_title = "", # Title of the legend
legend_position = "topleft",
numbins = 8, # Number of colour bins in graph
center, # center of the map
size = c(640, 480), # size of the map. This produces the right dimensions in Opasnet.
MINIMUMSIZE = FALSE,
RETURNIMAGE = TRUE,
GRAYSCALE = FALSE,
NEWMAP = TRUE,
zoom,
verbose = 1,
...
) {
plotvar <- shp[[plotvar]]
plotclr <- brewer.pal(numbins, "Spectral")
classes <- classIntervals(plotvar, numbins, style = "quantile")
colcode <- findColours(classes, plotclr)
latR <- shp@coords[ , 2]
lonR <- shp@coords[ , 1]
#get the bounding box:
bb <- qbbox(lat = latR, lon = lonR)
if (missing(zoom))
zoom <- min(MaxZoom(latR, lonR, size))
if (missing(center)) {
lat.center <- mean(latR)
lon.center <- mean(lonR)
}
else {
lat.center <- center[1]
lon.center <- center[2]
}
if (MINIMUMSIZE) {
ll <- LatLon2XY(latR[1], lonR[1], zoom) # I think the latR and lonR are used here differently than how they
ur <- LatLon2XY(latR[2], lonR[2], zoom) # are used elsewhere. Thus, if MINIMUMSIZE = TRUE, you may see problems.
cr <- LatLon2XY(lat.center, lon.center, zoom)
ll.Rcoords <- Tile2R(ll, cr)
ur.Rcoords <- Tile2R(ur, cr)
if (verbose > 1) {
cat("ll:")
print(ll)
print(ll.Rcoords)
cat("ur:")
print(ur)
print(ur.Rcoords)
cat("cr:")
print(cr)
}
size[1] <- 2 * max(c(ceiling(abs(ll.Rcoords$X)), ceiling(abs(ur.Rcoords$X)))) + 1
size[2] <- 2 * max(c(ceiling(abs(ll.Rcoords$Y)), ceiling(abs(ur.Rcoords$Y)))) + 1
if (verbose) cat("new size: ", size, "\n")
}
MyMap <- GetMap(
center = c(lat.center, lon.center),
zoom = zoom,
size = size,
RETURNIMAGE = RETURNIMAGE,
GRAYSCALE = GRAYSCALE,
verbose = verbose,
...
)
PlotOnStaticMap(MyMap) # Plot an empty map.
PlotOnStaticMap( # Plot the data points on the map.
MyMap,
lat = latR,
lon = lonR,
pch = pch,
cex = cex,
col = colcode,
add = T
)
legend( # Plot the legend on the map.
legend_position,
legend = names(attr(colcode, "table")),
title = legend_title,
fill = attr(colcode, "palette"),
cex = 1.0,
bty = "y",
bg = "white"
)
}
MyPlotKML <- function(
shp, # a SpatialPointDataFrame object.
result = "Result", # The name of  result column in shp.
rasterization = TRUE, # Whether to rasterize the data or not.
ncols = 32, # Number or columns in the raster.
nrows = 32, # Number of rows in the raster.
fun = mean # function to aggregate data points to the raster.
) {
cat("Consider merging this function MyPolotKML with google.show_raster_on_maps in OpasnetUtilsExt.\n")
if(rasterization) {
#Create blank raster
rast <- raster()
#Set raster extent to that of point data
extent(rast) <-extent(shp)
#Choose number of columns and rows
ncol(rast) <- ncols
nrow(rast) <- nrows
#Rasterize point data
rast2 <- rasterize(shp, rast, shp[[result]], fun = fun)
}
start <- 0 # min(shp[[result]])
end <- max(shp[[result]])
steps <- approx(c(start,end),n=6)$y
colors <- rev(rainbow(length(steps), start=0, end=0.50))
# Create the colorstrip below the map.
par(mfrow=c(6,1), mar=c(3,1,0,1), cex = 1.5)
colorstrip <- function(colors, labels)
{
count <- length(colors)
image(
matrix(1:count, count, 1),
col = colors,
ylab = "",
axes = FALSE
)
axis(1,approx(c(0, 1), n=length(labels))$y, labels)
}
colorstrip(colors, steps)
#Plot data
google.show_raster_on_maps(rast2, col = colors, style = "height:500px;")
}
}


Line 958: Line 634:
}
}


orbind2 <- function( # Like orbind but the value is an ovariable.
rm(wiki_username)
o1, # ovariable whose slots are used in the value.
objects.store(list = ls())
o2, # ovariable
cat("All objects in the global namespace were stored:", ls(), "\n")
use_fillna = FALSE, # Do we use fillna to fill in the NA values in indices?
 
warn = "" # What warning is given if fillna is used?
</rcode>
 
'''Functions for GIS data
 
<rcode name="gis" embed=1>
#This is code Op_en6007/gis on page [OpasnetUtils/Drafts]]
 
library(OpasnetUtils)
 
MyPointKML <- function( # The function creates a KML fille from a SpatialPointsDataFrame object.
obj = NULL, # Spatial object with the data. A SpatialPointsDataFrame.
kmlname = "", # Name of the KML fille (does this show on the map?)
kmldescription = "", # Description of the KML fille (does this show on the map?)
name = NULL, # Name for each datapoint (vector with the same length as data in obj).
description = "", # Descrtion of each datapoint (vector with the same length as data in obj).
icon = "http://maps.google.com/mapfiles/kml/pal4/icon24.png", # Icon shown on pin (?)
col=NULL # I don't know what this does.
) {
 
cat("This function MyPointKML is depreciated. Use google.point_kml in OpasnetUtilsExt instead.\n")
 
    if (is.null(obj))
        return(list(header = c("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
            "<kml xmlns=\"http://earth.google.com/kml/2.2\">",
            "<Document>", paste("<name>", kmlname, "</name>",
                sep = ""), paste("<description><![CDATA[", kmldescription,
                "]]></description>", sep = "")), footer = c("</Document>",
            "</kml>")))
    if (class(obj) != "SpatialPointsDataFrame")
        stop("obj must be of class 'SpatialPointsDataFrame' [package 'sp']")
    if (is.null(name)) {
        name = c()
        for (i in 1:nrow(obj)) name <- append(name, paste("site",
            i))
    }
    if (length(name) < nrow(obj)) {
        if (length(name) > 1)
            warning("kmlPoints: length(name) does not match nrow(obj). The first name will be replicated.")
        name <- rep(name, nrow(obj))
    }
    if (length(description) < nrow(obj)) {
        if (length(description) > 1)
            warning("kmlPoints: length(description) does not match nrow(obj). The first description will be replicated.")
        description <- rep(description, nrow(obj))
    }
    if (length(icon) < nrow(obj)) {
        if (length(icon) > 1)
            warning("kmlPoints: length(icon) does not match nrow(obj). Only the first one will be used.")
        icon <- icon[1]
    }
 
# This is some kind of a colour definition
col2kmlcolor <- function(col)
paste(rev(sapply(
col2rgb(col, TRUE),
function(x) sprintf("%02x", x))
), collapse = "")
    kml <- kmlStyle <- ""
   
# Create the KML fille.
kmlHeader <- c("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","<kml xmlns=\"http://earth.google.com/kml/2.2\">", "<Document>")
    kmlFooter <- c("</Document>", "</kml>")
   
# Create rows to the KML fille from data in obj.
    for (i in 1:nrow(obj)) {
        point <- obj[i, ]
        pt_style <- paste("#style", ifelse(length(icon) == 1, 1, i), sep = "")
        kml <- append(kml, "<Placemark>")
        kml <- append(kml, paste(
"  <description><![CDATA[",
name[i],
": ",
description[i],
"]]></description>",
sep = ""
))
#kml <- append(kml, "<Style><IconStyle>")
#kml <- append(kml, paste("<color>", col2kmlcolor(col[i]), "</color>", sep =""))
#kml <- append(kml, paste("  <Icon><href>", icon, "</href></Icon>", sep = ""))
#kml <- append(kml, "<scale>0.300000</scale>")
#kml <- append(kml, "</IconStyle></Style>")
        kml <- append(kml, "  <Point>")
        kml <- append(kml, "    <coordinates>")
        kml <- append(kml, paste(point@coords[1], point@coords[2], sep = ","))
        kml <- append(kml, "    </coordinates>")
        kml <- append(kml, "  </Point>")
        kml <- append(kml, "</Placemark>")
    }
   
    return(paste(paste(c(kmlHeader, kmlStyle, kml, kmlFooter), sep = "", collapse = "\n"), collapse="\n", sep = ""))
}
 
ova2spat <- function( # This function converts an ovariable or a data.frame into a SpatialPointsDataFrame.
  dat, # An evaluated ovariable or data.frame that has coordinate indices.
  coords = c("LO", "LA"), # The names of the coordinate indices as a character vector, first x then y.
  proj4string = NULL # Projection identifier or specification as character string. See http://spatialreference.org/
  # If proj4string is NULL, longitude-latitude system is assumed.
) {
  if(class(dat) == "ovariable") temp <- dat@output else
    if(is.data.frame(dat)) temp <- dat else
      stop("object must be either evaluated ovariable or data.frame\n")
 
  # Transform coordinates into numeric format.
 
  for(i in coords) {
    temp[[i]] <- as.numeric(as.character(temp[[i]]))
  }
 
  # Define the coordinate points first, then add other ovariable output to it.
 
  if(is.null(proj4string)) {
    sp <- SpatialPoints(temp[coords], CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
    } else {
      sp <- SpatialPoints(temp[coords], CRS(proj4string))
    }
  out <- SpatialPointsDataFrame(sp, temp[!colnames(temp) %in% coords])
 
  #Transform the projection to longitude-latitude system.
  if(!is.null(proj4string)) {
    epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
    out <- spTransform(out,epsg4326String)
  }
 
  return(out)
}
 
 
# MyRmap is a function for creating static Google maps as png.
# It is based on MyMap function without the "file destination" parameter
# Requires RgoogleMaps package
 
MyRmap <- function (
shp, # a spatial data object
plotvar, # Name of the column that has the values to be illustrated on the map
pch = 19, # Shape of the point (19: circle)
cex = 0.3, # Size of the point
legend_title = "", # Title of the legend
legend_position = "topleft",
numbins = 8, # Number of colour bins in graph
center, # center of the map
size = c(640, 480), # size of the map. This produces the right dimensions in Opasnet.
MINIMUMSIZE = FALSE,
RETURNIMAGE = TRUE,
GRAYSCALE = FALSE,
NEWMAP = TRUE,
zoom,
verbose = 1,
...
) {
) {
x <- unkeep(o1 * 1, prevresults = TRUE, sources = TRUE)
plotvar <- shp[[plotvar]]
y <- unkeep(o2 * 1, prevresults = TRUE, sources = TRUE)
plotclr <- brewer.pal(numbins, "Spectral")
xmarg <- colnames(x@output)[x@marginal]
classes <- classIntervals(plotvar, numbins, style = "quantile")
ymarg <- colnames(y@output)[y@marginal]
colcode <- findColours(classes, plotclr)
for(i in xmarg) x@output[[i]] <- as.factor(x@output[[i]])
latR <- shp@coords[ , 2]
for(i in ymarg) y@output[[i]] <- as.factor(y@output[[i]])
lonR <- shp@coords[ , 1]
out <- o1
 
out@output <- orbind(x, y)
#get the bounding box:
 
bb <- qbbox(lat = latR, lon = lonR)


if(use_fillna) {
if (missing(zoom))
b <- character()
zoom <- min(MaxZoom(latR, lonR, size))
for(i in colnames(out@output)[out@marginal]) {if(any(is.na(out@output[[i]]))) b <- c(b, i)}
if (missing(center)) {
if(length(b) > 0) {
lat.center <- mean(latR)
out@output <- fillna(out@output, b)
lon.center <- mean(lonR)
warning(warn, "\nMissing values had to be filled by function fillna in indices: ", b, "\n")
}
else {
lat.center <- center[1]
lon.center <- center[2]
}
if (MINIMUMSIZE) {
ll <- LatLon2XY(latR[1], lonR[1], zoom) # I think the latR and lonR are used here differently than how they
ur <- LatLon2XY(latR[2], lonR[2], zoom) # are used elsewhere. Thus, if MINIMUMSIZE = TRUE, you may see problems.
cr <- LatLon2XY(lat.center, lon.center, zoom)
ll.Rcoords <- Tile2R(ll, cr)
ur.Rcoords <- Tile2R(ur, cr)
if (verbose > 1) {
cat("ll:")
print(ll)
print(ll.Rcoords)
cat("ur:")
print(ur)
print(ur.Rcoords)
cat("cr:")
print(cr)
}
}
size[1] <- 2 * max(c(ceiling(abs(ll.Rcoords$X)), ceiling(abs(ur.Rcoords$X)))) + 1
size[2] <- 2 * max(c(ceiling(abs(ll.Rcoords$Y)), ceiling(abs(ur.Rcoords$Y)))) + 1
if (verbose) cat("new size: ", size, "\n")
}
}


colnames(out@output)[colnames(out@output) == "Result"] <- paste(o1@name, "Result", sep = "")
MyMap <- GetMap(
out@marginal <- colnames(out@output) %in% c(xmarg, ymarg)
center = c(lat.center, lon.center),
zoom = zoom,
size = size,
RETURNIMAGE = RETURNIMAGE,  
GRAYSCALE = GRAYSCALE,  
verbose = verbose,
...
)
 
PlotOnStaticMap(MyMap) # Plot an empty map.


return(out)
PlotOnStaticMap( # Plot the data points on the map.
MyMap,
lat = latR,
lon = lonR,
pch = pch,
cex = cex,
col = colcode,
add = T
)
 
legend( # Plot the legend on the map.
legend_position,
legend = names(attr(colcode, "table")),
title = legend_title,
fill = attr(colcode, "palette"),
cex = 1.0,
bty = "y",
bg = "white"
)
}
}


rm(wiki_username)
MyPlotKML <- function(
objects.store(list = ls())
shp, # a SpatialPointDataFrame object.
cat("All objects in the global namespace were stored:", ls(), "\n")
result = "Result", # The name of  result column in shp.
rasterization = TRUE, # Whether to rasterize the data or not.
ncols = 32, # Number or columns in the raster.
nrows = 32, # Number of rows in the raster.
fun = mean # function to aggregate data points to the raster.
) {
 
cat("Consider merging this function MyPolotKML with google.show_raster_on_maps in OpasnetUtilsExt.\n")
 
if(rasterization) {
#Create blank raster
rast <- raster()
 
#Set raster extent to that of point data
extent(rast) <-extent(shp)
 
#Choose number of columns and rows
ncol(rast) <- ncols
nrow(rast) <- nrows
 
#Rasterize point data
rast2 <- rasterize(shp, rast, shp[[result]], fun = fun)
 
}
 
start <- 0 # min(shp[[result]])
end <- max(shp[[result]])
steps <- approx(c(start,end),n=6)$y
colors <- rev(rainbow(length(steps), start=0, end=0.50))
 
# Create the colorstrip below the map.
 
par(mfrow=c(6,1), mar=c(3,1,0,1), cex = 1.5)
 
colorstrip <- function(colors, labels)
{
count <- length(colors)
image(
matrix(1:count, count, 1),
col = colors,
ylab = "",  
axes = FALSE
)
axis(1,approx(c(0, 1), n=length(labels))$y, labels)
}
 
colorstrip(colors, steps)
 
#Plot data
 
google.show_raster_on_maps(rast2, col = colors, style = "height:500px;")
}


objects.store(MyPointKML, ova2spat, MyRMap, MyPlotKML)
cat("Functions MyPointKML, ova2spat, MyRMap, MyPlotKML stored.\n")
</rcode>
</rcode>



Revision as of 08:26, 31 May 2017



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer") # Old version that fetches all objects, depreciated and not updated.
objects.latest("Op_en6007", code_name = "diagnostics") # Functions for ovariable and model diagnostics: ovashapetest, showLoctable, binoptest
objects.latest("Op_en6007", code_name = "webropol") # Functions for operating with Webropol data
objects.latest("Op_en6007", code_name = "miscellaneous") # Functions for various tasks
objects.latest("Op_en6007", code_name = "gis") # Functions for ovariable, KML and Googl maps interactions

Rationale

Calculations

Functions for ovariable diagnostics

+ Show code

Functions for Webropol data

+ Show code

Miscellaneous functions

+ Show code

Functions for GIS data

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>