Plotting 3d in R

Embed Size (px)

Citation preview

  • 8/2/2019 Plotting 3d in R

    1/19

    R graph gallery

    Compilation by Eric Lecoutre

    December 12, 2003

    Contents

    3D Bivariate Normal Density . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23D Scatterplot - Cloud, regression plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43D Wireframe - For surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Agreement plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Conditional Plot 1 - coplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Fourfold Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    Hexagon Binning Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Mosaicplot - Associations in a contingency table . . . . . . . . . . . . . . . . . . . . . . . . . . 14Parallel Plot - Comparing groups with few subjects . . . . . . . . . . . . . . . . . . . . . . . . . 15Ternary Plot - Biplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Tukeys Hanging Rootogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Violin Plot - Boxplot showing density, aka vase boxplot . . . . . . . . . . . . . . . . . . . . . . 19

    1

  • 8/2/2019 Plotting 3d in R

    2/19

  • 8/2/2019 Plotting 3d in R

    3/19

    term1*exp(term2*(term3+term4-term5))

    } # setting up the function of the multivariate normal density

    #

    z

  • 8/2/2019 Plotting 3d in R

    4/19

    3D Scatterplot - Cloud, regression plan

    variables: 3 QTlibrary: lattice or scatterplot3dfunction: cloud scatterplot3d,

    Sample graph

    o

    o

    oo

    o

    o

    o

    o

    o

    o

    o

    oo

    o

    oo

    o

    o

    o

    o

    o

    o

    o

    o

    o

    oooo

    oo

    o

    o

    o

    oo

    o

    o

    o

    oo

    o

    o

    o o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    oo

    o

    o

    oo

    o

    o

    o

    o

    oo

    o

    ooo

    o

    o

    o

    o

    o

    o

    oo o

    o

    o

    o

    ooo

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    oo

    o

    o

    o

    oo

    oo

    o

    o

    o

    o

    o

    o

    o

    oo

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    o

    ooo

    o

    ooo

    o

    oo

    o

    Petal.Length

    Petal.Width

    Sepal.Length

    Iris Dataooo

    setosaversicolorvirginica

    Sample code

    data(iris)

    cloud(Sepal.Length ~ Petal.Length * Petal.Width, data = iris,

    groups = Species, screen = list(z = 20, x = -70),

    perspective = FALSE,

    key = list(title = "Iris Data", x = .15, y=.85, corner = c(0,1),

    border = TRUE, points = Rows(trellis.par.get("superpose.symbol"), 1:3),

    text = list(levels(iris$Species))))

    %$%

    4

  • 8/2/2019 Plotting 3d in R

    5/19

    Sample graph

    scatterplot3d 5

    8 10 12 14 16 18 20 22

    10

    20

    30

    40

    50

    60

    70

    80

    60

    65

    70

    75

    80

    85

    90

    Girth

    Height

    Volume

    Sample code

    data(trees)

    s3d

  • 8/2/2019 Plotting 3d in R

    6/19

    3D Wireframe - For surfaces

    variables: 3 QTlibrary: latticefunction: wireframe

    Sample graph

    x

    y

    z

    1

    0.5

    0

    0.5

    1

    Sample code

    x

  • 8/2/2019 Plotting 3d in R

    7/19

    Agreement plot

    variables: one confusion matrixlibrary: vcdfunction: agreementplot

    Description

    Representation of a k k confusion matrix, where the observed and expected diagonal elements arerepresented by superposed black and white rectangles, respectively.

    Agreement chart allows to quickly see where two judges do disagree.

    Sample graph

    Agreement Chart

    Husband

    Wife

    Never FunN

    everFun

    Fairly Often

    FairlyOften

    Very Often

    VeryOfte

    n

    Always fun

    Alwaysfun

    Sample code

    library(vcd)

    7

  • 8/2/2019 Plotting 3d in R

    8/19

    data(SexualFun)

    agreementplot(t(SexualFun))

    8

  • 8/2/2019 Plotting 3d in R

    9/19

    Conditional Plot 1 - coplot

    variables: 3 QL or QTlibrary: -function: coplot

    Description

    Sample graph

    10

    30

    50

    70

    0 10 20 30 40 50

    10

    30

    50

    70

    0 10 20 30 40 50

    10

    30

    50

    70

    1:54

    breaks

    A

    B

    Given : wool

    L

    M

    H

    Given:tension

    Sample code

    data(warpbreaks)

    ## given two factors

    coplot(breaks ~ 1:54 | wool * tension, data = warpbreaks,

    col = "red", bg = "pink", pch = 21,

    bar.bg = c(fac = "light blue"))

    9

  • 8/2/2019 Plotting 3d in R

    10/19

    Fourfold Display

    data: 2x2xk contingency tableslibrary: vcdfunction: fourfoldplot

    Description

    The fourfold display depicts frequencies by quarter circles, whose radius is proportional to nij , so thearea is proportional to the cell count . The cell frequencies are usually scaled to equate the marginaltotals, and so that the ratio of diagonally opposite segments depicts the odds ratio. Confidence rings forthe observed odd ratio allow a visual test of the hypothesis H0 : = 1 corresponding to no association.They have the property that the rings for adjacent quadrants overlap iff the observed counts are consistentwith the null hypothesis.

    Sample graph

    Sex: Male

    Admit?:Yes

    Sex: Female

    Admit?:No

    Department: A

    512

    89

    313

    19

    Sex: Male

    Admit?:

    Yes

    Sex: Female

    Admit?:No

    Department: B

    353

    17

    207

    8

    Sex: Male

    Admit?:Yes

    Sex: Female

    Admit?:No

    Department: C

    120

    202

    205

    391

    Sex: Male

    Admit?:

    Yes

    Sex: Female

    Admit?:No

    Department: D

    138

    131

    279

    244

    10

  • 8/2/2019 Plotting 3d in R

    11/19

    Sample code

    library("vcd")

    # Load data

    data(UCBAdmissions)

    x

  • 8/2/2019 Plotting 3d in R

    12/19

  • 8/2/2019 Plotting 3d in R

    13/19

    ry

  • 8/2/2019 Plotting 3d in R

    14/19

    Mosaicplot - Associations in a contingency table

    variables: QL ( 2)library: -function: mosaicplot

    Description

    Mosaicplot graph represents a contingency table, each cell corresponding to a piece of the plot, whichsize is proportional to cell entry.

    Extended mosaic displays show the standardized residuals of a loglinear model of the counts from bythe color and outline of the mosaics tiles. (Standardized residuals are often referred to a standard normaldistribution.) Negative residuals are drawn in shaded of red and with broken outlines; positive ones aredrawn in blue with solid outlines.

    Thus, mosaicplot are perfect to visualize associations within a table and to detect cells which createdependancies.

    Sample graph

    Standardized

    Residuals:

    4

    Hair

    Ey

    e

    Black Brown Red Blond

    Brown

    Blue

    Hazel

    reen

    Male Female Male Female MaleFemale Male Female

    Sample code

    data(HairEyeColor)

    mosaicplot(HairEyeColor, shade = TRUE)

    14

  • 8/2/2019 Plotting 3d in R

    15/19

    Parallel Plot - Comparing groups with few subjects

    variables: 1 QL and at least 2 other variableslibrary: lattice or MASSfunction: parallel

    Description

    Sample graph

    SepalLength

    SepalWidth

    PetalLength

    Min Max

    setosa versicolor

    SepalLength

    SepalWidth

    PetalLength

    virginica

    Three

    Varieties

    of

    Iris

    Sample code

    data(iris)

    parallel(~iris[1:3]|Species, data = iris,

    layout=c(2,2), pscales = 0,

    varnames = c("Sepal\nLength", "Sepal\nWidth", "Petal\nLength"),

    page = function(...) {

    grid.text(x = seq(.6, .8, len = 4),

    y = seq(.9, .6, len = 4),

    label = c("Three", "Varieties", "of", "Iris"),

    gp = gpar(fontsize=20))

    })

    15

  • 8/2/2019 Plotting 3d in R

    16/19

    Sample graph

    Petal L. Petal W. Sepal W. Sepal L.

    Sample code

    data(iris3)

    ir

  • 8/2/2019 Plotting 3d in R

    17/19

    Ternary Plot - Biplot

    variables: 3 QT and 1 QLlibrary: ade4function: triangle.plot

    Description

    Graphs for a dataframe with 3 columns of positive or null values triangle.plot is a scatterplot trian-gle.biplot is a paired scatterplots

    Sample graph

    0 0.8

    pri

    0.50.2 sec 0.7

    0.3

    ter

    0.134

    0.36

    0.506

    0 0.8

    pri

    0.40.2 sec 0.6

    0.4

    ter

    Belgium

    Denmark

    Spain

    France

    Greece

    Ireland Italy

    Luxembourg

    Netherlands

    Portugal

    Germany

    United_Kingdom

    0 0.8

    pri

    0.50.2 sec 0.7

    0.3

    ter

    12

    3

    4

    5

    6 7

    8

    9

    10

    11

    12

    0 0.8

    pri

    0.50.2 sec 0.7

    0.3

    ter

    12

    3

    4

    5

    6 7

    8

    9

    10

    11

    12

    1314

    15

    16

    17

    18 19

    20

    21

    22

    23

    24

    Principal axis

    Sample code

    data (euro123)

    par(mfrow = c(2,2))

    triangle.plot(euro123$in78, clab = 0, cpoi = 2, addmean = TRUE,

    show = FALSE)triangle.plot(euro123$in86, label = row.names(euro123$in78), clab = 0.8)

    triangle.biplot(euro123$in78, euro123$in86)

    triangle.plot(rbind.data.frame(euro123$in78, euro123$in86), clab = 1,

    addaxes = TRUE, sub = "Principal axis", csub = 2, possub = "topright")

    par(mfrow = c(1,1))

    17

  • 8/2/2019 Plotting 3d in R

    18/19

    Tukeys Hanging Rootogram

    variables: 1 QLlibrary: vcdfunction: rootogram

    Description

    Discrete frequency distributions are often graphed as histograms, with a theoretical fitted distributionsuperimposed. It is hard to compare the observed and fitted frequencies visually, because (a) we mustassess deviations against a curvilinear relation, and (b) the largest frequencies dominate the display.

    The hanging rootogram (Tukey, 1977) solves these problems by (a) shifting the histogram bars tocoincide with the fitted curve, so that deviations may be judged by deviations from a horizontal line, and(b) plotting on a square-root scale, so that smaller frequencies are emphasized. Featured example showsmore clearly that the observed frequencies differ systematically from those predicted under a Poissonmodel.

    Sample graph

    0 1 2 3 4 5 6

    Number of Occurrences

    sqrt(Frequency)

    0

    2

    4

    6

    8

    10

    Sample code

    library("vcd")

    # create data

    madison=table(rep(0:6,c(156,63,29,8,4,1,1)))

    # fit a poisson distribution

    madisonPoisson=goodfit(madison,"poisson")

    rootogram(madisonPoisson)

    18

  • 8/2/2019 Plotting 3d in R

    19/19

    Violin Plot - Boxplot showing density, aka vase boxplot

    variables: 1 QT and optional QL for groupslibrary: simpleR - Using R for Introductory Statisticsauthor: John Verzani, College of Staten Island - http://www.math.csi.cuny.edu/ verzani/featured in: Simple Rfunction: simple.violinplot

    Description

    Violin plot are similar to boxplot except that they show the density of the data, estimated by kernelmethod.

    Sample graph

    A B C D E F G H

    0

    50

    100

    15

    0

    Sample code

    # library(Simple) - required library which comes with Simple R# http://www.math.csi.cuny.edu/Statistics/R/simpleR

    data(OrchardSprays)

    simple.violinplot(decrease ~ treatment, data = OrchardSprays,col="bisque",border="black")

    19