R in Action reading notes (21) 16th advanced Graphics Step (top)

Source: Internet
Author: User

Four graphics systems in the 16.1 R

The underlying graphics functions are automatically called, and the calls to the grid and lattice functions must be loaded with the appropriate packages (such as the Library (lattice)). To invoke the Ggplot2 function you need to download and install the package (Install.packages ("Ggplot2")) and load it before first use (library (GGPLOT2)).

16.2 Lattice bag

The lattice package provides a comprehensive graphical system for visualization of univariate and multivariate data. In the condition of one or more other variables, the fence graphic shows the distribution of a variable or its relationship to other variables.

> library (Lattice) > Histogram (~height|voice.part,data=singer,main= "distributionof Heights by Voice pitch", Xlab = "Height (inches)")

The lattice package provides a rich array of functions for generating single-variable graphs (point, kernel density, histogram, column and box plots), bivariate graphs (scatter plots, strip plots, and parallel box plots) and multivariable graphs (three-dimensional and scatter graph matrices). Various advanced drawing functions are subject to the following format:

Graph_function (formula,data=,options)

Graph_function is a function.

FORMULA specifies the variables and condition variables to be displayed.

Data to specify a frame.

Options are comma-delimited parameters used to modify the contents, placement, and labeling of the graphic.

Lattice common options for advanced drawing functions

Lattice Drawing Example:

> Gear<-factor (Gear,levels=c (3,4,5), Labels=c ("3 Gears", "4 Gears", "5 Gears")) > Cyl<-factor (Cyl,levels=c ( 4,6,8), Labels=c ("4 Cylinders", "6 cylinders", "8 Cylinders")) > Densityplot (~mpg,main= "Density plot", xlab= "miles per Gallon ")

> Densityplot (~mpg | cyl,main= "Density plot by number of cylinders", xlab= "miles per gallon")

> Bwplot (cyl~mpg|gear,main= "box plots by cylinders and gears", xlab= "Carweight", ylab= "cylinders")

> Xyplot (mpg~wt|cyl*gear,main= "Scatter plots by cylinders and gears", xlab= "car weight", ylab= "miles per gallon")

> Cloud (mpg~wt*qsec|cyl,main= "D scatter plots by cylinders")

> Dotplot (cyl~mpg|gear,main= "dot plots by number of gears and cylinders", xlab= "miles per gallon"

> Splom (Mtcars[c (1,4,5,6)],main= "scatter plot matrix for mtcars data")

16.2.1 Condition variables

> Myshingle<-equal.count (x,number=#,overlap=proportion)

The continuous variable x is divided into the # interval, with an overlap of proportion, an equal number of observations within each numeric range, and a variable myshingle (or class shingle). outputting or drawing the object (such as plot (Myshingle)) will show the tile interval. Once a continuous variable is converted to a tile, you can use it as a condition variable.

> Displacement<-equal.count (mtcars$disp,number=3,overlap=0) > Xyplot (mpg~wt|displacement,data=mtcars,+        main= "miles per gallon vs weight by engine displacement", +        xlab= "weight", ylab= "miles per gallon", +        layout= C (3,1), aspect=1.5)

16.2.2 Panel functions

Each advanced drawing function invokes a default function to draw the panel. These default functions obey the following naming conventions: Panel.graph_function, where graph_function is the horizontal drawing function. such as: Xyplot (Mpg~wt|displacement,data=mtcars) can also be written as: Xyplot (Mpg~wt|displacement,data=mtcars,panel=panel.xyplot). Xyplot of custom panel functions:

>displacement<-equal.count (mtcars$disp,number=3,overlap=0) > mypanel<-function (x, y) {+ Panel.xyplot y,pch=19) + panel.rug (x, y) + Panel.grid (h=-1,v=-1) + panel.lmline (x,y,col= "Red", wd=1,lty=2) +}>xyplot (mpg~wt| displacement,data=mtcars,+ layout=c (3,1), + aspect=1.5,+ main= "miles per gallon vs Weightby engine displacement", + xlab= " Weight ", ylab=" miles per gallon ", + Pannel=mypanel)

Xyplot for customizing Panel functions and additional options

> library (Lattice) >mtcars$transmission<-factor (Mtcars$am,levels=c (0,1), + labels=c ("Automatic", "Manual") ) > Panel.smoother<-function (x, y) {+ Panel.grid (h=-1,v=-1) + panel.xyplot (x, y) + panel.loess (x, y) + panel.abline (h =mean (y), lwd=2,lty=2,col= "green") +}> Xyplot (mpg~disp|transmission,data=mtcars,+ scales=list (cex=.8,col= "Red") , + panel=panel.smoother,+ xlab= "displacement", ylab= "miles per gallon", + main= "mpg vs Displacement bytransmission typr", + sub= "Dotted lines is group means", aspect=1)

16.2.3 Grouping variables

When a lattice graphic expression contains a conditional variable, a panel is generated at each level of the variable. If you want to superimpose the results together, you can set the variable to a grouping variable (grouping variable).

> library (Lattice) >mtcars$transmission<-factor (Mtcars$am,levels=c (0,1), + labels=c ("Automatic", "Manual") ) > Densityplot (~mpg,data=mtcars,+ group=transmission,+ main= "mpg distributuion bytransmission type", + xlab= "miles Per gallon ", + auto.key=true)

For more control over the legend, use the key = option

> library (Lattice) > Mtcars$transmission<-factor (mtcars$am,levels=c (0,1), + labels=c ("Automatic", "Manual") ) >colors=c ("Red", "Blue") #设定颜色, line and point types > Lines=c > Points=c (16,17) >key.trans<-list (title= " Transmission ", #自定义图例 + space=" Bottom ", columns=2,+ text=list (Levels (mtcars$transmission)), + points=list (pch=points, col=colors), + lines=list (col=colors,lty=lines), + cex.title=1,cex=.9) >densityplot (~mpg,data=mtcars,+ group= transmission,+ main= "mpg distributuion bytransmission type", + xlab= "miles per gallon", + pch=points,lty=lines,col= Colors, #自定义密度图 + lwd=2,jitter=.005,+ Key=key.trans)

Xyplot that contain grouping variables and condition variables, and custom legends

> library (Lattice) >colors= "Darkgreen" > Symbols<-c (1:12) > Linetype<-c (1:3) >>key.species <-list (title= "Plant", + space= "right", + Text=list (Levels (co2$plant)), + points=list (pch=symbols,col=colors)) > >xyplot (uptake~conc| type*treatment,data=co2,+ group=plant,+ type= "o", + pch=symbols,col=colors,lty=linetype,+ main= "carbon dioxide uptake \ningrass plants ", + ylab=expression (paste (" uptake ", + bgroup (" (", Italic (Frac (" Umol "," M "^2),") ")), + xlab= Expression (Paste ("concenteation", + bgroup ("(", Italic (Frac), ")")), + ml,l "sub= Grass: Echinochloacrus-gailli ", + key=key.species)

R in Action reading notes (21) 16th advanced Graphics Step (top)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.