Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Robert I. Kabacoff - R in action

.pdf
Скачиваний:
97
Добавлен:
02.06.2015
Размер:
12.13 Mб
Скачать

396

CHAPTER 16 Advanced graphics

16.4.3 latticist

The latticist package lets you explore a data set using lattice displays. It provides a graphic user interface to the graphs described in section 16.2, but it can also be used to create displays from the vcd package (see chapter 11, section 11.4). If desired, latticist and can also be integrated with playwith. For example, executing the following code

library(latticist)

mtcars$cyl <- factor(mtcars$cyl) mtcars$gear <- factor(mtcars$gear) latticist(mtcars, use.playwith=TRUE)

will bring up the interface in figure 16.13.

In addition to having the playwith functionality (point identification, annotation, zooming, panning, styles), the user can now create lattice graphs by selecting from drop-down menus and buttons. To learn more about the latticist package, see http://code.google.com/p/latticist/.

Figure 16.13 playwith window with latticist functionality. The user can create lattice and vcd graphs interactively.

Interactive graphs

397

A similar interface is available for ggplot2 graphs, through Plot Builder, a plug-in for Deducer, a popular GUI for R (see appendix A). Because it can’t be run from the R console, we won’t discuss it here. If you’re interested, visit the Deducer website at www. deducer.org.

16.4.4Interactive graphics with the iplots package

Whereas playwith and latticist allow you to interact with a single graph, the iplots package takes interaction in a different direction. This package provides interactive mosaic plots, bar plots, box plots, par-

allel plots, scatter plots, and histograms that can be linked together and color brushed. This means that you can select and identify observations using the mouse, and highlighting observations in one graph will automatically highlight the same observations in all other open graphs. You can also use the mouse to obtain information about graphic objects such as points, bars, lines, and box plots.

The iplots package is implemented through Java and the primary functions are listed in table 16.5.

To understand how iplots works, execute the code provided in listing 16.6.

Table 16.5 iplot functions

Function

Description

 

 

ibar()

Interactive bar char t

ibox()

Interactive box plot

ihist()

Interactive histogram

imap()

Interactive map

imosaic()

Interactive mosaic plot

ipcp()

Interactive parallel

 

coordinates plot

iplot()

Interactive scatter plot

 

 

Listing 16.6 iplots demonstration

library(iplots)

attach(mtcars)

cylinders <- factor(cyl) gears <- factor(gear) transmission <- factor(am) ihist(mpg)

ibar(gears) iplot(mpg, wt)

ibox(mtcars[c("mpg", "wt", "qsec", "disp", "hp")]) ipcp(mtcars[c("mpg", "wt", "qsec", "disp", "hp")]) imosaic(transmission, cylinders)

detach(mtcars)

Six windows containing graphs will open. Rearrange them on the desktop so that each is visible (each can be resized if necessary). A portion of the display is provided in figure 16.14.

Now try the following:

Click on the three-gear bar in the Barchart (gears) window. The bar will turn red. In addition, all cars with three-gear engines will be highlighted in the other graphic windows.

398

CHAPTER 16 Advanced graphics

Mouse down and drag to select a rectangular region of points in the Scatter plot (wt vs mpg) window. These points will be highlighted and the corresponding observations in every other graphics window will also turn red.

Hold down the Ctrl key and move the mouse pointer over a point, bar, box plot, or line in one of the graphs. Details about that object will appear in a pop-up window.

Right-click on any object and note the options that are offered in the context menu. For example, you can right-click on the Boxplot (mpg) window and change the graph to a parallel coordinates plot (PCP).

You can drag to select more than one object (point, bar, and so on) or use Shiftclick to select noncontiguous objects. Try selecting both the threeand five-gear bars in the Barchart (gears) window.

The functions in the iplots package allow you to explore the variable distributions and relationships among variables in subgroups of observations that you select interactively. This can provide insights that would be difficult and time-consuming to obtain in other ways. For more information on the iplots package, visit the project website at http://rosuda.org/iplots/.

Figure 16.14 An iplots demonstration created by listing 16.6. Only four of the six windows are displayed to save room. In these graphs, the user has clicked

on the three-gear bar in the bar chart window.

Summary

399

16.4.5rggobi

For our final example of interactivity, we’ll actually look beyond the R platform to the open source GGobi application (www.ggobi.org). GGobi is a comprehensive program for the visual and dynamic exploration of high-dimensional data and is freely available for Windows, Mac OS X, and Linux platforms. It offers a number of attractive features, including linked interactive scatter plots, bar charts, parallel coordinate plots, time series plots, scatter plot matrices, and 3D rotation; brushing and identification; multivariate transformation methods; and sophisticated exploratory support, including guided and manual 1D and 2D tours. Happily, the rggobi package provides a seamless interface between GGobi and R.

The first step in using GGobi is to download and install the appropriate software for your platform (www.ggobi.org/downloads/). Then install the rggobi package within R using install.packages("rggobi", depend=TRUE).

Once you’ve installed both, you can use the ggobi() function to run GGobi from within R. This gives you sophisticated interactive graphics access to all of your R data. To see this in action, execute the following code:

library(rggobi)

g <- ggobi(mtcars)

The GGobi interface will open and allow you to explore the mtcars dataset in a highly interactive fashion. To learn more, review the introduction, tutorial, manual, and video guides available on the GGobi website. A comprehensive overview is also provided in Cook and Swayne (2008).

16.5 Summary

In this chapter, we reviewed several packages that provide access to advanced graphical methods. We started with the lattice package, designed to provide a system for creating trellis graphs, followed by the ggplot2 package, based on a comprehensive grammar of graphics. Both packages are designed to provide you with a complete and comprehensive alternative to the native graphics provided with R. Each offers methods of creating attractive and meaningful visualizations of data that are difficult to generate in other ways.

We then explored several packages for dynamically interacting with graphs, including playwith, latticist, iplots, and rggobi. These packages allow you to interact directly with data in graphs, leading to a greater intimacy with your data and expanded opportunities for developing insights.

You should now have a firm grasp of the many ways that R allows you to create visual representations of data. If a picture is worth a thousand words, and R provides a thousand ways to create a picture, then R must be worth a million words (or something to that effect). These resources are a testament to the hard and selfless work of the initial R development team and the thousands of hours of work contributed by package authors.

afterword: Into the rabbit hole

We’ve covered a broad range of topics in the book, including major ones like the R development environment, data management, traditional statistical models, and statistical graphics. We’ve also covered hidden gems like resampling statistics, missing values imputation, and interactive graphics. The great (or perhaps infuriating) thing about R is that there’s always more to learn.

R is a large, robust, and evolving statistical platform and programming language. With so many new packages, frequent updates, and new directions, how can a user stay current? Happily, many websites support this active community and provide coverage of platform and package changes, new methodologies, and a wealth of tutorials. I’ve listed some of my favorite sites below.

The R Project (http://www.r-project.org/)

The official R website and your first stop for all things R. The site includes extensive documentation, including An Introduction to R, The R Language Definition, Writing R Extensions, R Data Import/Export, R Installation and Administration, and The R FAQ.

The R Journal (http://journal.r-project.org/)

A freely accessible refereed journal containing articles on the R project and contributed packages.

R Bloggers (http://www.r-bloggers.com/)

A central hub (blog aggregator) collecting content from bloggers writing about R. Contains new articles daily. I’m addicted to it.

Planet R (http://planetr.stderr.org)

Another good site-aggregator, including information from a wide range of sources. Updated daily.

400

AFTERWORD Into the rabbit hole

401

CRANberries (http://dirk.eddelbuettel.com/cranberries/)

A site that aggregates information about new and updated packages, and contains links to CRAN for each.

R Graph Gallery (http://addictedtor.free.fr/graphiques/)

A collection of innovative graphs, along with their source code.

R Graphics Manual (http://bm2.genes.nig.ac.jp/)

A collection of R graphics from all R packages, arranged by topic, package, and function. At last count, there were 35,000+ images!

Journal of Statistical Software (http://www.jstatsoft.org/)

A freely accessible refereed journal containing articles, book reviews, and code snippets on statistical computing. Contains frequent articles about R.

Revolutions (http://blog.revolution-computing.com/)

A popular, well-organized blog, dedicated to news and information about R.

CRAN Task Views (http://cran.r-project.org/web/views/)

Task views are guides to the use of R in different academic and research fields. They include a description of the packages and methods available for a given area. Currently there are 28 task views available (see table below).

CRAN Task Views

Bayesian Inference

Machine Learning & Statistical Learning

Chemometrics and Computational Physics

Medical Image Analysis

Design, Monitoring, and Analysis of Clinical Trials

Multivariate Statistics

Clinical Trial Design, Monitoring, and Analysis

Natural Language Processing

Cluster Analysis & Finite Mixture Models

Official Statistics & Sur vey Methodology

Probability Distributions

Optimization and Mathematical Programming

Computational Econometrics

Analysis of Pharmacokinetic Data

Analysis of Ecological and Environmental Data

Phylogenetics, Especially Comparative Methods

Design of Experiments (DoE)

Psychometric Models and Methods

Empirical Finance

Robust Statistical Methods

Statistical Genetics

Statistics for the Social Sciences

Graphic Displays & Dynamic Graphics

Analysis of Spatial Data

gRaphical Models in R

Sur vival Analysis

High-Per formance and Parallel Computing with R

Time Series Analysis

 

 

402 AFTERWORD Into the rabbit hole

R-Help Main R Mailing List (https://stat.ethz.ch/mailman/listinfo/r-help)

This electronic mailing list is the best place to ask questions about R. The archives are also searchable. Be sure to read the FAQ before posting questions.

Quick-R (http://www.statmethods.net)

This is my R website. It’s stocked with more than 80 brief tutorials on R topics. False modesty forbids me from saying more.

The R community is a helpful, vibrant, and exciting lot. Welcome to Wonderland.

appendix A Graphic user interfaces

You turned here first, didn’t you? By default, R provides a simple command-line interface (CLI). The user enters statements at a command-line prompt (> by default) and each command is executed one at a time. For many data analysts, the CLI is one of R’s most significant limitations.

There have been a number of attempts to create more graphical interfaces, ranging from code editors that interact with R (such as RStudio), to GUIs for specific functions or packages (such as BiplotGUI), to full-blown GUIs that allow the user to construct analyses through interactions with menus and dialog boxes (such as R Commander).

Several of the more useful code editorsare listed in table A.1.

Table A.1 Integrated development environments and syntax editors

Name

URL

 

 

Eclipse with StatET plug-in

http://www.eclipse.org and http://www.walware.de/goto/statet

ESS (Emacs Speaks

http://ess.r-project.org/

Statistics)

 

Komodo Edit

http://www.activestate.com/komodo_edit/ http://www.

with SciViews-K plug-in

sciviews.org/SciViews-K/

JGR

http://www.r forge.net/JGR/

 

 

403

404

APPENDIX A Graphic user interfaces

Table A.1 Integrated development environments and syntax editors (continued )

Name

URL

 

 

RStudio

http://www.rstudio.org

Tinn-R (Windows only)

http://www.sciviews.org/Tinn-R/

Notepad++ with NppToR

http://notepad-plus-plus.org/

(windows only)

http://sourceforge.net/projects/npptor/

 

 

The code editors in table A.1 allow the user to edit and execute R code and include syntax highlighting, statement completion, object exploration, project organization, and online help. A screenshot of RStudio is provided in figure A.1.

Several promising full-blown GUIs for R are listed in table A.2. The GUIs available for R are less comprehensive and mature than those offered by SAS or IBM SPSS, but they’re developing rapidly.

Figure A.1 RStudio IDE

 

APPENDIX A Graphic user interfaces

405

Table A.2 Comprehensive GUIs for R

 

 

 

 

 

Name

 

URL

 

 

 

 

JGR/Deducer

 

http://ifellows.ucsd.edu/pmwiki/pmwiki.php?n=Main.DeducerManual

R AnalyticFlow

 

http://www.ef-prime.com/products/ranalyticflow_en/

 

Rattle (for data mining)

 

http://rattle.togaware.com/

 

R Commander

 

http://socser v.mcmaster.ca/jfox/Misc/Rcmdr/

 

Red R

 

http://www.red-r.org/

 

Rkward

 

http://rkward.sourceforge.net/

 

 

 

 

 

My favorite GUI for introductory statistics courses is R Commander(shown in figure A.2). Finally, there are a number of applications that allow the user to create a GUI wrapper for any given R function (including user-written functions). These include the R GUI Generator (RGG) (http://rgg.r-forge.r-project.org/), and the fgui and

twiddler packages available from CRAN.

GUI projects in R are undergoing rapid change and expansion. For more information, visit the R GUI Projects page at http://www.sciviews.org/_rgui/.

Figure A.2

R Commander GUI

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]