Thursday, January 24, 2013

USING COLOR IN R

USING COLOR IN R



REVISED: Friday, March 1, 2013




Using color in R.

I.  INTRODUCTION TO COLOR IN R

When you invoke the R function colors( ) you obtain a list of the names of 657 colors you can use in any R plotting function. The first 8 colors of the 657 colors are as follows:

> colors()
  [1] "white"               
  [2] "aliceblue"           
  [3] "antiquewhite"        
  [4] "antiquewhite1"       
  [5] "antiquewhite2"       
  [6] "antiquewhite3"       
  [7] "antiquewhite4"       
  [8] "aquamarine"          

II. grDevices PACKAGE

The grDevices package comes with R. Use the library( ) function to load the grDevices package into R as follows:

> library(grDevices)

A. colorRamp( )

> str(colorRamp)
function (colors, bias = 1, 
space = c("rgb", "Lab"), interpolate = c("linear", "spline"))

colorRamp( ) takes a palette of colors and returns a function that takes values between 0 and 1, which indicate the extremes of the color palette. 

> str(gray)
function (level)  

> gray(1)
[1] "#FFFFFF"   # Shades of gray, ignore # it's hexadecimal.

B. colorRampPalette( )

> str(colorRampPalette)
function (colors, ...)  

colorRampPalette( ) takes a palette of colors and returns a function that takes integer arguments and returns a vector of colors interpolating the palette; e.g., heat.colors( ) or topo.colors( ).

> str(heat.colors)
function (n, alpha = 1)

> str(topo.colors)
function (n, alpha = 1) 

III. RColorBrewer PACKAGE

The RColorBrewer package is available on CRAN.

After you download the RColorBrewer package from CRAN, use the library( ) function to load the RColorBrewer package into R as follows:

> library(RColorBrewer)

The RColorBrewer package provides three types of palettes: sequential, diverging, and qualitative.

A. SEQUENTIAL PALETTES

Sequential palettes are used for data that is ordered from low to high.

B. DIVERGING PALETTES

Diverging palettes are used for data that diverges or deviates from a mean; e.g., going negative or positive from a center point.
 
C. QUALITATIVE PALETTES

Qualitative palettes are used for data that is not ordered from low to high. 

IV. R smoothScatter( ) FUNCTION

smoothScatter( ) function comes with R and is used to plot a histogram using color.

> str(smoothScatter)
function (x, y = NULL, 
    nbin = 128, bandwidth, 
    colramp = colorRampPalette(c("white", 
        blues9)), nrpoints = 100, 
    pch = ".", cex = 1, 
    col = "black", transformation = function(x) x^0.25, 
    postPlotHook = box, 
    xlab = NULL, ylab = NULL, 
    xlim, ylim, xaxs = par("xaxs"), 
    yaxs = par("yaxs"), 
    ...)  

High density plot area of the histogram is shown as a dark color and lower density plot area is shown as a lighter color.

V. rgb( ) FUNCTION

> str(rgb)
function (red, green, 
    blue, alpha, names = NULL, 
    maxColorValue = 1)  

The rgb( ) function can be used to create any color using red, green, and blue proportions. Color transparency can be added by using the rgb( ) function alpha parameter; zero being the most transparent and one being not tranparent. Transparency works wonders with high density scatterplots.

VI. colorspace PACKAGE

The colorspace package is available on CRAN.

> library("colorspace")

Provides mapping between assorted color spaces including RGB.

Have fun using color in R.


Elcric Otto Circle







-->




-->




-->














How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment