Learn R Programming

fdth (version 1.2-1)

plot.fdt: Plot fdt.default and fdt.multiple objects

Description

S3 methods for fdt.default and fdt.multiple objects. It is possible to plot histograms and polygons (absolute, relative and cumulative).

Usage

## S3 methods "plot"(x, type=c('fh', 'fp', 'rfh', 'rfp', 'rfph', 'rfpp', 'd', 'cdh', 'cdp', 'cfh', 'cfp', 'cfph', 'cfpp'), v=FALSE, v.round=2, v.pos=3, xlab="Class limits", xlas=0, ylab=NULL, col="gray", xlim=NULL, ylim=NULL, main=NULL, x.round=2, ...)
"plot"(x, type=c('fh', 'fp', 'rfh', 'rfp', 'rfph', 'rfpp', 'd', 'cdh', 'cdp', 'cfh', 'cfp', 'cfph', 'cfpp'), v=FALSE, v.round=2, v.pos=3, xlab="Class limits", xlas=0, ylab=NULL, col="gray", xlim=NULL, ylim=NULL, main=NULL, main.vars=TRUE, x.round=2, ...)
"plot"(x, type=c('fb', 'fp', 'fd', 'rfb', 'rfp', 'rfd', 'rfpb', 'rfpp', 'rfpd', 'cfb', 'cfp', 'cfd', 'cfpb', 'cfpp', 'cfpd', 'pa'), v=FALSE, v.round=2, v.pos=3, xlab=NULL, xlas=0, ylab=NULL, y2lab=NULL, y2cfp=seq(0, 100, 25), col=gray(.4), xlim=NULL, ylim=NULL, main=NULL, box=FALSE, ...)
"plot"(x, type=c('fb', 'fp', 'fd', 'rfb', 'rfp', 'rfd', 'rfpb', 'rfpp', 'rfpd', 'cfb', 'cfp', 'cfd', 'cfpb', 'cfpp', 'cfpd', 'pa'), v=FALSE, v.round=2, v.pos=3, xlab=NULL, xlas=0, ylab=NULL, y2lab=NULL, y2cfp=seq(0, 100, 25), col=gray(.4), xlim=NULL, ylim=NULL, main=NULL, main.vars=TRUE, box=FALSE, ...)

Arguments

x
A fdt object.
type
The type of the plot: fb: Absolute frequency barplot, fh: Absolute frequency histogram, fp: Absolute frequency polygon, fd: Absolute frequency dotchart,

rfb: Relative frequency barplot, rfh: Relative frequency histogram, rfp: Relative frequency polygon, rfd: Relative frequency dotchart,

rfpb: Relative frequency (%) barplot, rfph: Relative frequency (%) histogram, rfpp: Relative frequency (%) polygon, rfpd: Relative frequency (%) dotchart,

d: Density, cdh: Cumulative density histogram, cdp: Cumulative density polygon,

cfb: Cumulative frequency barplot, cfh: Cumulative frequency histogram, cfp: Cumulative frequency polygon, cfd: Cumulative frequency dotchart,

cdpb: Cumulative frequency (%) barplot, cdph: Cumulative frequency (%) histogram, cfpp: Cumulative frequency (%) polygon, cfpd: Cumulative frequency (%) dotchart.

pa: Pareto chart.

v
Logical flag: should the values be added to the plot?
v.round
If v=TRUE, it rounds the values to the specified number of decimal places (default 0).
v.pos
If v=TRUE, a position specifier for the text. Values of 1, 2, 3 and 4, respectively indicate positions below, to the left of, above and to the right of the coordinates (default 3).
xlab
A label for the x axis.
xlas
An integer which controls the orientation of the x axis labels: 0: parallel to the axes, 2: perpendicular to the axes.
ylab
A label for the y axis.
y2lab
A label for the y2 axis.
y2cfp
A cumulative percent frequency for the y2 axis. The default is seq(0, 100, 25).
col
A vector of colors.
xlim
The x limits of the plot.
ylim
The y limits of the plot.
main
Title of the plot(s). This option has priority over main.vars, i.e, if any value is informed, the variable names will not be used as title of the plot(s). For fdt.multiple, the value should be a vector of characters, in this case, the R's recycling rule will be used.
main.vars
Logical flag: should the variables names be added as title of each plot (default TRUE)?
x.round
A numeric value to round the x ticks: 0: parallel to the axes, 1: horizontal, 2: perpendicular to the axes, 3: vertical.
box
...
...
Optional plotting parameters.

Details

The result is a single histogram or polygon (absolute, relative or cumulative) for fdt.default or a set of histograms or polygon (absolute, relative or cumulative) for fdt.multiple objects. Both default and multiple try to compute the maximum number of histograms that will fit on one page, then it draws a matrix of histograms. More than one graphical device may be opened to show all histograms. The result is a single barplot, polygon, dotchar (absolute, relative or cumulative) and Pareto chart for fdt_cat.default or a set of the same graphs for fdt_cat.multiple objects. Both default and multiple try to compute the maximum number of histograms that will fit on one page, then it draws a matrix of graphs lited above. More than one graphical device may be opened to show all graphs.

See Also

hist.data.frame provided by Hmisc package.

Examples

Run this code
library(fdth)

#================================
# Vectors: univariated numerical
#================================
x <- rnorm(n=1e3,
           mean=5,
           sd=1)

(d <- fdt(x))

# Histograms
plot(d)  # Absolute frequency histogram

plot(d,
     main='My title')

plot(d,
     x.round=3,
     col='darkgreen')

plot(d,
     xlas=2)

plot(d,
     x.round=3,
     xlas=2,
     xlab=NULL)

plot(d, 
     v=TRUE, 
     cex=.8,
     x.round=3,
     xlas=2,
     xlab=NULL,
     col=rainbow(11))

plot(d,
     type='fh')    # Absolute frequency histogram

plot(d,
     type='rfh')   # Relative frequency histogram

plot(d,
     type='rfph')  # Relative frequency (%) histogram

plot(d,
     type='cdh')   # Cumulative density histogram

plot(d,
     type='cfh')   # Cumulative frequency histogram

plot(d,
     type='cfph')  # Cumulative frequency (%) histogram

# Poligons
plot(d,
     type='fp')    # Absolute frequency polygon

plot(d,
     type='rfp')   # Relative frequency polygon

plot(d,
     type='rfpp')  # Relative frequency (%) polygon

plot(d,
     type='cdp')   # Cumulative density polygon

plot(d,
     type='cfp')   # Cumulative frequency polygon

plot(d,
     type='cfpp')  # Cumulative frequency (%) polygon

# Density
plot(d,
     type='d')     # Density

# Theoretical curve and fdt
x <- rnorm(1e5,
           mean=5,
           sd=1)

plot(fdt(x,
         k=100), 
     type='d', 
     col=heat.colors(100))

curve(dnorm(x,
            mean=5,
            sd=1),
      col='darkgreen',
      add=TRUE,
      lwd=2)


#==================================
# Vectors: univariated categorical
#==================================
x <- sample(letters[1:5],
            1e3,
            rep=TRUE)

(dc <- fdt_cat(x))

# Barplot: the default
plot(dc)

# Barplot
plot(dc,
     type='fb')

# Polygon
plot(dc,
     type='fp')

# Dotchart
plot(dc,
     type='fd')

# Pareto chart
plot(dc,
     type='pa')

#=============================================
# Data.frames: multivariated with categorical
#=============================================
mdf <- data.frame(X1=rep(LETTERS[1:4], 25),
                  X2=as.factor(rep(1:10, 10)),
                  Y1=c(NA, NA, rnorm(96, 10, 1), NA, NA),
                  Y2=rnorm(100, 60, 4),
                  Y3=rnorm(100, 50, 4),
                  Y4=rnorm(100, 40, 4))

# Histograms
(d <- fdt(mdf))

plot(d,
     v=TRUE,
     cex=.8)

plot(d,
     col='darkgreen',
     ylim=c(0, 40))

plot(d,
     col=rainbow(8), 
     ylim=c(0, 40),
     main=LETTERS[1:4])

plot(d,
     type='fh')

plot(d,
     type='rfh')

plot(d,
     type='rfph')

plot(d,
     type='cdh')

plot(d,
     type='cfh')

plot(d,
     type='cfph')

# Poligons
plot(d,
     v=TRUE,
     type='fp')

plot(d,
     type='rfp')

plot(d,
     type='rfpp')

plot(d,
     type='cdp')

plot(d,
     type='cfp')

plot(d,
     type='cfpp') 

# Density
plot(d,
     type='d')     

levels(mdf$X1)

plot(fdt(mdf,
         k=5,
         by='X1'),
     ylim=c(0, 12))

levels(mdf$X2)

plot(fdt(mdf,
         breaks='FD', 
         by='X2'))

plot(fdt(mdf,
         k=5,
         by='X2'))  # It is difficult to compare

plot(fdt(mdf,
         k=5,
         by='X2'),
     ylim=c(0, 8))  # Easy

plot(fdt(iris,
         k=5))

plot(fdt(iris,
         k=5),
     col=rainbow(5))

plot(fdt(iris, 
         k=5,
         by='Species'),
     v=TRUE)

d <- fdt(iris,
         k=10)

plot(d)

plot(d, 
     type='d')

# Categorical data 
(dc <- fdt_cat(mdf))
plot(dc)

plot(dc,
     type='fd',
     pch=19)

#=========================
# Matrices: multivariated
#=========================
plot(fdt(state.x77))

plot(fdt(volcano))

Run the code above in your browser using DataLab