R

Plot: 2D distribution plots

Here are 4 quick ways to plot 2D distributions.  See here for more color options.

library(MASS)   #kde2d() and mvrnorm()
VV=matrix(c(10,8,8,10),2,2)
coef=mvrnorm(10000,c(2,10),VV)
par(mfrow=c(2,2), mar=c(4,4,1,1))
### 1
plot(coef[,1],coef[,2])
### 2
contour(kde2d(coef[,1],coef[,2])) 
### 3
persp(kde2d(coef[,1],coef[,2])) 
### 4 
image(kde2d(coef[,1],coef[,2], n = 100))
box()

Plot: monochromatic color

rainbow(), heat.colors(), topo.colors(), terrain.colors() [tim.colors() library(fields)] can all be a bit overwhelming use of color.  colorRampPalette() can be used for more control over the color mixture. densCols() gives color by density.  Here I give a function that makes monochromatic color. See the rows of the figure for some of the different monochromatic color bands that are possible.

Function: mode

This calculates the mode of a vector (like mean() and median()). Finds the most common element in a vector.

mode=function(vector_name)
{  as.numeric(names(sort(table(vector_name),decreasing=TRUE))[1])  }

 

Function: ordering

This function will sort a dataframe by the column number specified.
First cbind() your variables together and then sort by one column.

ordering=function(dataset, col.num)
{   index=order(dataset[,col.num])
    hold=0
    dataset=dataset[index,]  
    dataset
}

### Example:
# turtles=cbind(w,f,y)
# ordering(turtles, 3)
 

Rbinom (rbinomint)

Wraps R.rbinom for correct input and output

   double rbinomint(int a, int b, double c)
   {   Rcpp::NumericVector x = rbinom(a,b,c);
       return x(0);
   }

Rnorm (rnormdouble)

Wraps R.rnorm for correct input and output.

   double rnormdouble(int a, double b, double c)
   {   Rcpp::NumericVector x = rnorm(a,b,c);
       return x(0);
   }

Pages