Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R packaging plus rangeaxis function. #1

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
man/.Rhistory
674 changes: 674 additions & 0 deletions COPYING

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Package: fancyaxis
Version: 0.5
Date: 2012-06-10
Title: Functions for drawing scatter-plots and axes inspired by E. Tufte's redesigns.
Author: Steven J. Murdoch \url{http://www.cl.cam.ac.uk/users/sjm217/}
Maintainer: Peter N. Steinmetz <[email protected]>
Description: A set of functions for plotting scatterplots and creating
axes which are inspired by E. Tufte's ""The Visual Display of
Quantitative Information"
License: GPL version 3 or newer
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export(fancyaxis,axisstripchart,rangeaxis,minimalrug,clippedjitter)
106 changes: 106 additions & 0 deletions R/axisstripchart.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
## Draw a stripchart on an axis, showing marginal frequency

## Copyright (C) 2005 Steven J. Murdoch <http://www.cl.cam.ac.uk/users/sjm217/>
## Permissions at bottom.

## This is very much a work in progress and still of alpha
## quality. See the example file for usage. It currently does not
## deal with logarithmic scales properly and needs manual tweaking
## of several values to suit different data and output device
## resolution. Comments and suggestions are appreciated. My
## contact details can be found here:
## http://www.cl.cam.ac.uk/users/sjm217/#contact
##
## The design of the graph is based on a scatterplot presented in
## "The Visual Display of Quantitative Information", Edward Tufte.
## Thanks to Paul Murrell for assistance with handling the log axes
##
## From $Id: fancyaxis.R 6927 2009-03-08 12:49:17Z sjm217 $

axisstripchart <- function(x, side, sshift=0.3) {
# x: the data from which the plots are to be produced.
# side: as in axis()

# Find out the properties of the side we are doing
parside <-
if (side==1){
# Does the outside of the plot have larger or smaller vales
flip <- 1
# Are we on the yaxis
yaxis <- FALSE
# Relevant index of par("usr")
3
}
else if (side==2) {
flip <- 1
yaxis <- TRUE
1
}
else if (side==3) {
flip <- -1
yaxis <- FALSE
4
}
else if (side==4) {
flip <- -1
yaxis <- TRUE
2
}

# Axis position
base<-par("usr")[parside]

# Width and height in user units
plotwidth <- diff(par("usr")[1:2])
plotheight <- diff(par("usr")[3:4])

# Shift for the q2 and q3 axis from the base (in inches)
shift <- par("pin")[1]*0.003*flip
# Gap for the median
gap <- par("pin")[1]*0.003
# Shift for the mean pointer away from the axis
meanshift <- par("cin")[1]*0.5*flip
# Shift away from the q2 and q3 axis for the stripchart
stripshift <- par("cin")[1]*sshift*flip

# Scale lengths so both axes are equal on output device
if (yaxis) {
shift <- shift/par("pin")[1]*plotwidth
meanshift <- meanshift/par("pin")[1]*plotwidth
stripshift <- stripshift/par("pin")[1]*plotwidth
gap <- gap/par("pin")[2]*plotheight
} else {
shift <- shift/par("pin")[2]*plotheight
meanshift <- meanshift/par("pin")[2]*plotheight
stripshift <- stripshift/par("pin")[2]*plotheight
gap <- gap/par("pin")[1]*plotwidth
}

# If vertical, stripchart assumes offset is a factor of character
# width, if horizontal, character height (bug?). So correct for this
if (yaxis)
offset=flip*par("cin")[2]/par("cin")[1]
else
offset=flip

# Don't clip the chart
oldxpd <- par(xpd = TRUE)
on.exit(par(oldxpd))

stripchart(x, method="stack", vertical=yaxis, offset=offset, pch=15,
cex=0.2, add=TRUE, at=base+shift+stripshift, col="red")
}

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
72 changes: 72 additions & 0 deletions R/clippedjitter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## Copyright (C) 2005 Steven J. Murdoch <http://www.cl.cam.ac.uk/users/sjm217/>
## Permissions at bottom.
##
## This is very much a work in progress and still of alpha
## quality. See the example file for usage. It currently does not
## deal with logarithmic scales properly and needs manual tweaking
## of several values to suit different data and output device
## resolution. Comments and suggestions are appreciated. My
## contact details can be found here:
## http://www.cl.cam.ac.uk/users/sjm217/#contact
##
## The design of the graph is based on a scatterplot presented in
## "The Visual Display of Quantitative Information", Edward Tufte.
## Thanks to Paul Murrell for assistance with handling the log axes
##
## From $Id: fancyaxis.R 6927 2009-03-08 12:49:17Z sjm217 $

# Add a small amount of noise to a numeric vector, preserving minimum
# and minimum
clippedjitter <- function(x, ...) {
# x: numeric to which jitter should be added.
# ...: parameters passed to jitter()

mi <- min(x)
ma <- max(x)

len=length(x)

# Find a position for the min and max
mipos <- ((1:len)[x==mi])[1]
mapos <- ((1:len)[x==ma])[1]

# The standard jittered data
xj <- jitter(x, ...)

# Find the elements which are outside the limits
under <- xj<mi
over <- xj>ma

# Find the distance away from the limit
dunder <- mi-xj[under]
dover <- xj[over]-ma

# Reflect over the limit
repunder <- dunder+mi
repover <- ma-dover

# Replace out of limit values with the reflected ones
xj[under] <- repunder
xj[over] <- repover

# Replace a jittered min/max with the original min/max
xj[mipos] <- mi
xj[mapos] <- ma

# Return updated array
xj
}

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 changes: 19 additions & 21 deletions examples.R → R/examples.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
## fancyaxis: Draw axis which shows minimum, maximum, quartiles
## and mean
## Examples for fancyaxis package.
##
## Copyright (C) 2005 Steven J. Murdoch <http://www.cl.cam.ac.uk/users/sjm217/>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## Permissions at bottom.

## This is very much a work in progress and still of alpha
## quality. See the example file for usage. It currently does not
Expand All @@ -29,9 +15,7 @@
## "The Visual Display of Quantitative Information", Edward Tufte.
## Thanks to Paul Murrell for assistance with handling the log axes
##
## $Id$

source("fancyaxis.R")
## $Id: examples.R 1024 2005-06-16 13:36:05Z sjm217 $

opendevice <- function() {
# Background colour
Expand All @@ -44,10 +28,10 @@ opendevice <- function() {

#png(file="/tmp/faithful.png",width=380,height=380,bg=par("bg"))

postscript(file="/tmp/faithful.ps",paper="A4",bg=par("bg"))
#postscript(file="/tmp/faithful.ps",paper="A4",bg=par("bg"))

# A4 paper
#pdf(file="/tmp/faithful.pdf", width=297/25.4, height=210/25.4, bg=par("bg"))
pdf(file="faithful.pdf", width=297/25.4, height=210/25.4, bg=par("bg"))

# Wider paper, used for the small multiple graph
#pdf(file="/tmp/faithful.pdf", width=297/25.4*1.5, height=210/25.4/2, bg=par("bg"))
Expand Down Expand Up @@ -409,3 +393,17 @@ lagplot <- function() {

closedevice()
}

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Loading