X14 - Ejercicios

François Rebaudo, IRD francois.rebaudo@ird.fr

Marzo 2019 ; PUCE-Quito-Ecuador http://myrbooksp.netlify.com/

CC BY-NC-ND 3.0

noggplot

noggplot función

noggplot función

noggplot <- function(x, y, listOptions = list(
  legend = FALSE,
  title = "", 
  col = "", 
  legendTitle = ""), ...){
  # -------------------------------------------
  if(listOptions[["legend"]] == FALSE){
    par(mar = c(4, 4, 2, 2))
  }else{
    par(mar = c(4, 4, 2, 8))
  }
  if(listOptions[["col"]][1] == ""){
    myCol = 1
  }else{
    myCol = listOptions[["col"]]
  }
  plot(x, y, pch = 16, axes = FALSE, col = myCol, 
    panel.first = {
    rect(par("usr")[1], 
      par("usr")[3], 
      par("usr")[2], 
      par("usr")[4], 
      col = "lightgray",  border = NA)
    abline(v = 
      floor(
        min(x, na.rm = TRUE)):ceiling(
        max(x, na.rm = TRUE)), 
      col = "white", lwd = 2, lty = 1)
    abline(h = 
      floor(
        min(y, na.rm = TRUE)):ceiling(
        max(y, na.rm = TRUE)), 
      col = "white", lwd = 2, lty = 1)
    grid(col = "white", lwd = 1, lty = 1)
  }, ...)
  title(listOptions[["title"]], adj = 0, line = 0.5)
  axis(1, col = NA, col.ticks = 1, cex.axis = 0.9)
  axis(2, col = NA, col.ticks = 1, las = 1, cex.axis = 0.9)
  if(listOptions[["legend"]] == TRUE){
    if(listOptions[["legendTitle"]] == ""){
      legendTitle = 1
    }else{
      legendTitle = listOptions[["legendTitle"]]
    }
    par(xpd = TRUE)
    legend(max(x, na.rm = TRUE)*1.025, 
      mean(y, na.rm = TRUE), 
      legend = levels(myCol), bty = "n", 
      pch = 16, col = as.numeric(unique(myCol)), 
      title = legendTitle)
  }
}

noggplot función

noggplot(x = iris$Sepal.Length, y = iris$Sepal.Width, 
  listOptions = list(
    legend = TRUE, 
    title = "plot with noggplot function :-)", 
    col = iris$Species, legendTitle = "Species"))

noggplot función

noggplot función

noggplot(x = iris$Sepal.Length, y = iris$Sepal.Width, 
  listOptions = list(
    legend = TRUE, 
    title = "plot with noggplot function :-)", 
    col = iris$Species, legendTitle = "Species"), 
  xlab = "Eje x", ylab = "Eje y")

noggplot función

noggplot función

noggplot(x = iris$Sepal.Length, y = iris$Sepal.Width, 
  listOptions = list(
    legend = FALSE, 
    title = "plot with noggplot function :-)", 
    col = ""), 
  xlab = "Eje x", ylab = "Eje y")

noggplot función

noggplot función

noggplot(x = rnorm(100), y = rnorm(100), 
  listOptions = list(
    legend = TRUE, 
    title = "La leyenda esta mal... y los colores no me gustan", 
    col = factor(
      sample(
        c("A", "B", "C", "D", "E"), 
        size = 100, 
        replace = TRUE)
      ), 
    legendTitle = "Colores"), 
  xlab = "Eje x", ylab = "Eje y")

noggplot función

noggplot función

  • Escribir la documentación de la función usando roxigen2.
pkgCheck <- function(packages){
    for(x in packages){
        try(if (!require(x, character.only = TRUE)){
            install.packages(x, dependencies = TRUE)
            if(!require(x, character.only = TRUE)) {
                stop()
            }
        })
    }
}
pkgCheck("roxygen2")
## Loading required package: roxygen2

noggplot función

#' Add together two numbers
#'
#' @param x A number
#' @param y A number
#' @return The sum of \code{x} and \code{y}
#' @examples
#' add(1, 1)
#' add(10, 1)
add <- function(x, y) {
  x + y
}

noggplot función

#' Generic X-Y plotting with ggplot theme
#' 
#' `noggplot` wraps the `plot` function to make
#'   a plot which looks like the ggplot plot. It
#'   was made for teaching purposes as part of an 
#'   R learning module.
#' 
#' @param x the coordinates of points in the plot. 
#' @param y the y coordinates of points in the plot.
#' @param ... Arguments to be passed to methods (see plot).
#' @examples
#' noggplot(x = iris$Sepal.Length, y = iris$Sepal.Width, 
#'   listOptions = list(
#'     legend = FALSE, 
#'     title = "plot with noggplot function :-)", 
#'     col = ""))
noggplot <- function(x, y, listOptions = list(
  legend = FALSE,
  title = "", 
  col = "", 
  legendTitle = ""), ...){
  # ...
}

noggplot función

  • mejorar la función noggplot…
  • integrar regressiones linelaes…
  • y todas las opciones de ggplot2…
  • y hacer un paquete noggplot y publicarlo en el CRAN ;-)