User Tools

Site Tools


start

Welcome to my website, a mindmap of the thought processes I go through each day.

Here you can find a portfolio over my research, bioinformatic projects, Python (as in a programming language) and even some scouts!

The site is built upon a wiki, so there is no clear hierarchy. Instead, follow links throughout the maze or if in doubt, consult the sitemap.

Come back regularly!

ggplot2: Indicating closed range on axis

This is an update for ggplot2 v. 2.2.1 for indicating the range of valid numbers on the axes. E.g. having a correlation, the line should not proceed outside (-1, 1).

#' Cartesian coordinates with closed range on axis line.
#' 
#' @param xlim,ylim Limits for the x and y axes.
#' @param expand If \code{TRUE}, the default, adds a small expansion factor to
#'   the limits to ensure that data and axes don't overlap. If \code{FALSE},
#'   limits are taken exactly from the data or \code{xlim}/\code{ylim}.
#' @param horizontal Which end of the horizontal line to limit to a tick. Use 'left','right', or 'both'.
#' @param vertical Which end of the vertical line should not proceed across the outer tick? Use 'top', 'bottom', or 'both'.
#' @param gap Numeric value (usually between 0 and 1) to create a small gap where horizontal and vertical axes meet.
coord_closed_cart <- function(xlim = NULL, ylim = NULL, expand = TRUE, horizontal=c('left','right','both'), vertical=c('top','bottom','both'), gap=0.05) {
  ggproto(NULL, ClosedCoordCartesian,
          limits = list(x = xlim, y = ylim),
          expand = expand,
          horizontal=match.arg(horizontal),
          vertical=match.arg(vertical),
          gap=gap
  )
}
 
 
ClosedCoordCartesian <- ggproto('ClosedCoordCartesian', `_inherit`=CoordCartesian, 
    render_axis_h = function(self, scale_details, theme) {
      l <- ggproto_parent(CoordCartesian, self)$render_axis_h(scale_details, theme)
      l <- lapply(l, function(a) {
        if (a$name == 'NULL') return(a)
 
        i <- which(grepl('line', names(a$children)))
        r <- range(as.numeric(scale_details$x.major))
        a$children[[i]]$x <- switch(self$horizontal,
                           left =  unit(c(r[1], max(1-self$gap, r[2])), 'native'),
                           right = unit(c(min(0+self$gap, r[1]), r[2]), 'native'),
                           both =  unit(r, 'native')
        )
 
        return(a)
      })
      l
    },
    render_axis_v = function(self, scale_details, theme) {
      l <- ggproto_parent(CoordCartesian, self)$render_axis_v(scale_details, theme)
      l <- lapply(l, function(a) {
        if (a$name == 'NULL') return(a)
 
        i <- which(grepl('line', names(a$children)))
        r <- range(as.numeric(scale_details$y.major))
        a$children[[i]]$y <- switch(self$vertical,
                                    bottom =  unit(c(r[1], max(1-self$gap, r[2])), 'native'),
                                    top = unit(c(min(0+self$gap, r[1]), r[2]), 'native'),
                                    both =  unit(r, 'native')
        )
 
        return(a)
      })
      l
    }
 
)
 
df <- data.frame(
  gp = factor(rep(letters[1:3], each = 10)),
  y = rnorm(30),
  cl = sample.int(3, 30, replace=TRUE),
  cl2 = sample(c('a','b','c'), 30, replace=TRUE)
)
ggplot(df, aes(gp, y)) +
  geom_point()  + coord_closed_cart(horizontal = 'left', vertical = 'top') + labs(title='hello') +
  facet_grid(cl ~ cl2)

Older entries >>

start.txt · Last modified: 2012/10/20 17:37 by Stefan