R code for tplot, kmplot, jmplot, dsplot, and show.colors()

R code for other miscellaneous functions.

tplot()

tplot() is an alternative to boxplot(). The individual data can be shown (either in the foreground or background) with jittering if necessary.

Example

This example shows what options are available with tplot(). This may not be a good way to show these data (unnecessary color varieties...)
col and pch may be specified at the individual level or group level. (In this example, col is specified at the individual level, and pch is specified for groups.)
colors of boxes and box borders are specified by boxcol and boxborder.
boxplot.pars is passed to boxplot().

set.seed(100)
age <- rnorm(80, rep(c(26,36), c(70,10)), 4)
sex <- factor( sample(c('Female','Male'), 80, TRUE) )
group <- paste('Group ', sample(1:4, 40, prob=c(2,5,4,1), replace=TRUE), sep='')
d <- data.frame(age, sex, group)

tplot( age ~ group, data=d, 
    las=1, cex=1, cex.axis=1, bty='L', 
    show.n=TRUE, dist=.25, jit=.05, type=c('db','db','db','d'),
    group.pch=TRUE, pch=c(15,17,19,8), 
    col=c('darkred','darkblue')[c(sex)],
    boxcol=c('lightsteelblue1','lightyellow1',grey(.9), 0), boxborder=grey(.8),
    boxplot.pars=list(notch=TRUE, boxwex=.5)
    )

Example of tplot

kmplot()

  • Kaplan-Meier survival plot with at risk table
  • plot Kaplan-Meier curves with 'numbers at risk' at bottom margin.
    • can specify at which time n at risk is displayed
    • can specify line type / line width / line color for each line
    • can change the order of appearance in the n at risk table
    • n.at.risk numbers are right-justified!
    • simple=TRUE suppresses the 'at risk' table.

Example

require(survival)

kma <- survfit( Surv(time, status) ~ rx + adhere, data=colon )

kmplot(kma, mark='', simple=FALSE,
	xaxis.at=c(0,.5,1:9)*365, xaxis.lab=c(0,.5,1:9), # n.risk.at
	lty.surv=c(1,2), lwd.surv=1, col.surv=c(1,1,2,2,4,4), # survival.curves
	col.ci=0, # confidence intervals not plotted
	group.names=c('Obs ','Obs tumor adh','Lev','Lev tumor adh','Lev+5FU ','Lev+5FU tumor adh'),
	group.order=c(5,3,1,6,4,2), # order of appearance in the n.risk.at table and legend.
	extra.left.margin=6, label.n.at.risk=FALSE, draw.lines=TRUE, 
	cex.axis=.8, xlab='Years', ylab='Survival Probability', # labels
	grid=TRUE, lty.grid=1, lwd.grid=1, col.grid=grey(.9), 
	legend=TRUE, loc.legend='bottomleft',
	cex.lab=.8, xaxs='r', bty='L', las=1, tcl=-.2  # other parameters passed to plot()
) 
title(main='Chemotherapy for stage B/C colon cancer', adj=.1, font.main=1, line=0.5, cex.main=1)

jmplot()

  • Joint distribution and marginal distributions on its margins.
  • requires tplot() [See above.]

Example

 set.seed(274)
 x <- rnorm(100, 20, 5)
 y <- rexp(100)
 levels <- as.factor(sample(c("Male","Female"), 100, TRUE))
 jmplot(x, y, levels, col=levels, las=1, type='db', main='Some scores', jit=.02)

dsplot()

  • Discrete Scatter Plot for Integer-valued variables
  • This creates a scatter plot (sort of) for discrete bivariate data. An alternative to sunflower plots.

Example

set.seed(6)
x <- round(rnorm(400, 100, 4))
y <- round(rnorm(400, 200, 4))
sex <- sample(c('Female','Male'), 400, T)
dsplot(y ~ x, pch=19, col=1+(sex=='Female'), cex=.6, bkgr=T, 
   xlab='measurement 1', ylab='measurement 2', bty='L')
legend( 'bottomright', pch=19, col=1:2, legend=c('Male','Female'), cex=.8 )

showColors.png

Show colors

In R, there are 657 named colors. The following funciton, show.colors(), shows these colors and corresponding numbers.
Find the color you like in the plot. You can find the name of the color by running >colors()[number].

show.colors <- function(){
   par(mfrow=c(1,1))
   #   par(mai=c(.4,.4,.4,.4), oma=c(.2,0,0,.2))
   x <- 22 ; y <- 30
   plot(c(-1,x),c(-1,y), xlab='', ylab='', type='n', xaxt='n', yaxt='n', bty='n')
   for(i in 1:x){for(j in 1:y){
         k <- y*(i-1)+j ; co <- colors()[k]
         rect(i-1, j-1, i, j, col=co, border=grey(.5) )}}

   text(rep(-.5,y),(1:y)-.5, 1:y, cex=1.2-.016*y)
   text((1:x)-.5, rep(-.5,x), y*(0:(x-1)), cex=1.2-.022*x)
   title('col=colors()[n]')
}

> colors()[465]
[1] "mediumorchid3"

showColors.png

Topic attachments
I Attachment Action Size Date Who Comment
Beatlesongs072108.csvcsv Beatlesongs072108.csv manage 10.3 K 10 Oct 2008 - 17:18 TatsukiKoyama  
BeatlesongsPoster.pdfpdf BeatlesongsPoster.pdf manage 66.8 K 10 Oct 2008 - 17:11 TatsukiKoyama  
BeatlesongsPoster2.pdfpdf BeatlesongsPoster2.pdf manage 66.9 K 10 Oct 2008 - 18:05 TatsukiKoyama  
Poster3.pdfpdf Poster3.pdf manage 69.2 K 22 Nov 2016 - 16:31 TatsukiKoyama  
RFunctions0.RR RFunctions0.R manage 27.0 K 19 May 2017 - 12:09 TatsukiKoyama  
RFunctions071817.RR RFunctions071817.R manage 11.1 K 18 Jul 2017 - 03:07 TatsukiKoyama  
RFunctions1.RR RFunctions1.R manage 27.0 K 25 Aug 2017 - 15:07 TatsukiKoyama  
TatsukiRcodeFormatSum.RR TatsukiRcodeFormatSum.R manage 3.4 K 11 May 2017 - 00:05 TatsukiKoyama  
TatsukiRcodeMisc.RR TatsukiRcodeMisc.R manage 5.7 K 11 May 2017 - 00:05 TatsukiKoyama  
TextBox.RR TextBox.R manage 13.2 K 11 Apr 2014 - 11:49 TatsukiKoyama  
TextBoxExample.rr TextBoxExample.r manage 2.2 K 10 Oct 2008 - 16:33 TatsukiKoyama  
USEFUL.RR USEFUL.R manage 32.7 K 11 May 2017 - 00:05 TatsukiKoyama  
USEFUL.rr USEFUL.r manage 32.7 K 11 Nov 2008 - 09:20 TatsukiKoyama  
ap21.csvcsv ap21.csv manage 10.9 K 03 Aug 2011 - 12:11 TatsukiKoyama needed in computation on 3+3 designs: http://data.vanderbilt.edu/~koyamat/brew/t33.html
ap31.csvcsv ap31.csv manage 60.9 K 03 Aug 2011 - 12:11 TatsukiKoyama  
ap32.csvcsv ap32.csv manage 90.8 K 03 Aug 2011 - 12:12 TatsukiKoyama  
ap41.csvcsv ap41.csv manage 264.1 K 03 Aug 2011 - 12:13 TatsukiKoyama  
ap42.csvcsv ap42.csv manage 325.0 K 03 Aug 2011 - 12:13 TatsukiKoyama  
ap51.csvcsv ap51.csv manage 1017.9 K 03 Aug 2011 - 12:13 TatsukiKoyama  
ap52.csvcsv ap52.csv manage 1122.6 K 03 Aug 2011 - 12:13 TatsukiKoyama  
ap53.csvcsv ap53.csv manage 1996.2 K 03 Aug 2011 - 12:13 TatsukiKoyama  
ap61.csvcsv ap61.csv manage 3622.9 K 03 Aug 2011 - 12:13 TatsukiKoyama  
ap62.csvcsv ap62.csv manage 3713.3 K 03 Aug 2011 - 12:14 TatsukiKoyama  
ap63.csvcsv ap63.csv manage 5637.9 K 03 Aug 2011 - 12:14 TatsukiKoyama  
beatlesongs2012.RR beatlesongs2012.R manage 14.1 K 03 Mar 2012 - 23:58 TatsukiKoyama  
beatlesongs2012.csvcsv beatlesongs2012.csv manage 15.4 K 03 Mar 2012 - 20:48 TatsukiKoyama  
dspExample.pngpng dspExample.png manage 7.1 K 06 Jan 2011 - 14:56 TatsukiKoyama  
jmplotExample1.pngpng jmplotExample1.png manage 8.9 K 07 Dec 2011 - 16:48 TatsukiKoyama  
kmplotExample.pngpng kmplotExample.png manage 11.6 K 17 Dec 2011 - 16:41 TatsukiKoyama  
koyama.csscss koyama.css manage 0.3 K 03 Jan 2012 - 13:26 TatsukiKoyama  
tplotExample.pngpng tplotExample.png manage 6.1 K 07 Dec 2011 - 13:04 TatsukiKoyama  
Topic revision: r151 - 25 Aug 2017, TatsukiKoyama
 

This site is powered by FoswikiCopyright © 2013 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback