R in Action reading notes (22) 16th advanced Graphics Advance (bottom)

Source: Internet
Author: User
Tags gtk

16.2.4 Graphics Parameters

In lattice graphics, the default graphical parameters of the lattice function are contained in a large list object, which you can get through the Trellis.par.get () function and modify it with the Trellis.par.set () function. The Show.settings () function shows the current drawing parameter settings. View the current default settings and store them in a mysettings list:

> show.settings () > Mysettings<-trellis.par.get ()

To view the default setting values for the overlay points:

> mysettings$superpose.symbol$alpha[1] 1 1 1 1 1 1 1$cex[1] 0.8 0.8 0.8 0.8 0.8 0.8 0.8$col[1] "#0080ff" "#ff00ff" "Dar Kgreen "#ff0000" "Orange" "#00ff00" "Brown" $fill [1] "#CCFFFF" "#FFCCFF" "#CCFFCC" "#FFE5CC" "#CCE6FF" "#FFFFCC" "#FFCCCC "$font [1] 1 1 1 1 1 1 1$pch[1] 1 2 3 4 5 6 7 8 9 10

You can see that each level of the grouping variable uses a hollow circle (pch = 1). After defining 7 levels, the graphical symbols

will be used for recycling. Finally, let us make the following statement:

> mysettings$superpose.symbol$pch<-c (1:10) > Trellis.par.set (mysettings) > Show.settings ()

At this point the lattice graph uses the symbol 1 (the Hollow Circle) for the first level of the grouping variable, the second using the symbol 2 (the hollow triangle), and so on. In addition, we define 10 horizontal symbols for the grouping variables, not 7. This graphic setting effect will persist until the graphics device is turned off. You can modify any of the other graphical parameters in this way.

16.2.5 page Placement

The lattice function does not recognize the par () setting, first stores the lattice graphic in an object, and then controls it with the split = or position = option in the plot () function. The split option splits the page into a matrix that specifies the number of rows and columns, and then places the graphic in the matrix. The split option is in the format:

Split=c (placement row,placement column,total number of rows,total number of columns)

Cases:

> library (Lattice) > Graph1<-histogram (~height|voice.part,data=singer,+                   main= "Heights of choral Singers by voice part ") > Graph2<-densityplot (~height,data=singer,group=voice.part,+                     plot.points=false,auto.key= List (columns=4)) > Plot (graph1,split=c (1,1,1,2)) > Plot (graph2,split=c (1,2,1,2), Newpage=false)

 

It will place the first image on top of the second picture. Specifically, the first plot () function splits the page into a column

A matrix of two rows and places the graphic in the first column, the first row (from top to bottom, from left to right). A second plot ()

function to do the same, but place the graph in the first column and the second row. Because the plot () function starts a new page by default, you need to disable the operation, so set the option NewPage = FALSE

Use the position = option to have more control over size and placement:

> Plot (graph1,position=c (0,.3,1,1)) > Plot (graph2,position=c (0,0,1,.3), Newpage=false)

Position = C (xmin, ymin, Xmax, ymax), the page's X-y coordinate system is a rectangle, the x-axis and y-axis dimensions range from 0 to 1, the origin (0, 0) in the lower-left corner of the graph. You can also change the order of the panels in the lattice graph. The Index.cond = option of the advanced drawing function sets the order in which the condition variables are horizontal. Take the Voice.part factor as an example:

> Levels (Singer$voice.part) [1] "Bass 2"    "Bass 1"    "Tenor 2"   "Tenor 1"   "Alto 2"    "Alto 1"    " Soprano 2 "[8]" Soprano 1 "

16.3 Ggplot2 Bag

The Ggplot2 package provides a drawing system based on a comprehensive and coherent syntax. It makes up for the lack of consistency in creating graphics in R

The ability to create innovative and novel graphic types. The simplest way to draw in Ggplot2 is to use the Qplot () function, the fast drawing function. The format is:

Qplot (x, y = NULL, ..., data, facets =null, margins = FALSE,

Geom = "Auto", stat = list (null), Position = list (null), xlim= C (na,na), Ylim = C (Na, na), log = "", main = Null,xlab =DEP Arse (substitute (x)), Ylab = Deparse (substitute (y)), ASP = NA)

Options: Description

Alpha: The alpha transparency of the element overlap, with a value of 0 (fully transparent) to 1 (completely opaque) between the scores

Color, shape, size, fill: Associate the level of a variable with the color, shape, or size of the symbol. For a straight line graph, the Colo will link the line color to the variable level, and for the density and box plots, fill will link the filled color to the variable. The legend will be automatically drawn

Data: Specify a Frame

Facets: Specify a condition variable to create a fence plot. An expression such as rowvar~ Colvar. To create a fence map based on a single condition variable, you can use Rowvar ~. Or. ~ Colvar

Geom: Sets the geometry that defines the shape type. The GEOM option is a single-entry or multiple-purpose character vector,

Includes "point", "Smooth", "BoxPlot", "line", "histogram", "density", "bar" and "jitter"

Main, Sub: character vector, setting title and subtitle

Method, formula: If Geom = "Smooth", a smooth fit curve and confidence interval are added by default. When the number of observations is greater than 1000, it is necessary to invoke a more efficient smoothing fitting algorithm. Methods include regression LM, generalized additive model GAM, and robust regression RLM. Formula parameter specifies the form of fit for example, to add a simple regression curve, set geom = "Smooth", method = "LM", formula = Y ~ x. Changing the expression to Y ~poly (x, 2) will result in two fitting. Note that the expression uses the letters x and y instead of the variable name for method = "Gam", be sure to remember to load the MGCV package. For method = "RML", the mass package needs to be loaded

X, y: Specifies the variables that are placed on the horizontal and vertical axes. For single-variable graphs (such as histogram), omit y

Xlab, Ylab: Character vector, set horizontal and vertical axis labels

Xlim, Ylim: two-element numeric vector specifying minimum and maximum values for the horizontal and vertical axes, respectively

Library (Ggplot2) mtcars$cylinder<-as.factor (mtcars$cyl) Qplot (Cylinder,mpg,data=mtcars,geom=c ("BoxPlot", " Jitter "), fill=cylinder,main=" box plots with superimposed data points ", xlab=" number of cylinders ", yalb=" Miles per gallon ")

Type add regression line and confidence interval band

The drive type defines the row facet, while the number of cylinders defines the column facet. The size of the data points represents the horsepower of the car

Ratings for:

Library (Ggplot2) mtcars$cyl<-factor (Mtcars$cyl,levels=c (4,6,8), Labels=c ("4cylinders", "6 cylinders", "8 Cylinders ")) Mtcars$am<-factor (Mtcars$am,levels=c (0,1), Labels=c (" Automatic "," Manual ")) Qplot (wt,mpg,data= MTCARS,FACETS=AM~CYL,SIZE=HP)

Library (GGPLOT2) data (singer,package= "lattice") Qplot (height,data=singer,geom=c ("Density"), facets=voice.part~., Fill=voice.part)

16.4 Interactive Graphics

16.4.1 Interacting with graphics: identifying points

A function that identifies and labels points in a scatter plot: Identify (). With this function, you can use the mouse to label the number of rows or rows of points selected in the scatter chart until you select Stop or right-click the graphics recognition work to stop.

Plot (Mtcars$wt,mtcatrs$mpg) identify (Mtcars#wt,mtcars$mpg,labels=row.names (mtcars))

The cursor changes from one point to a cross. Click the points on the scatter chart to label them until you select stop from the Graphics device menu, or right-click the graphic and select Stop from the right-click menu.

16.4.2 Playwith

The Playwith package provides a GTK + graphical user interface (GUI) that enables users to edit and interact with R graphics.

The Playwith () function allows the user to identify and annotate points, view an observation of all variable values, scale and rotate a graphic,

Add labels (text, arrows, lines, rectangles, headings, and labels), modify visual elements (color, text size, and so on), apply previously stored graphic styles, and output graphical results in multiple formats.

Library (Playwith) library (lattice) Playwith (Xyplot (Mpg~wt|factor (cyl) *factor (AM), data=mtcars,subscripts=true, Type=c ("R", "P")))

Playwith window. Depending on the GTK + GUI, users can edit graphics using the mouse

Unlike the Identify () function, Playwith () is valid both for the R base graph and for lattice and Ggplot2

Graphics are valid. Some of the options on the Theme (theme) menu fit well with the underlying graphic, some are better suited to ggplot2 graphics (such as callouts), and some are not valid for Ggplot2 graphics (such as recognition points).

16.4.3 latticist

Using the Latticist package, you can explore the data set through a fence map. The package not only provides a graphical user interface, but also creates new graphics from the VCD pack. If necessary, latticist can also be integrated with Playwith, for example:

Library (latticist) mtcars$cyl<-factor (mtcars$cyl) mtcars$gear<-factor (mtcars$gear) latticist (Mtcars, Use.playwith=true)

Playwith window with latticist function function. Users can create lattice and interact with VCD graphics

16.4.4 Interactive graphics for iplots Packages

The Playwith and latticist packages can only interact with a single graphic, while the Iplots package provides a different way of interacting. The package provides interactive treemap, histograms, box plots, parallel plots, scatter plots and histograms, as well as color brushes, and can be combined to draw them together. This means that you can select and identify the observation point with the mouse, and when you highlight an observation point for one of the graphs, the other open shapes will automatically highlight the same observation point. You can also use the mouse to collect information about graphical objects such as points, bars, lines, and box plots.

Function description

Ibar () Interactive bar chart

Ibox () interactive box line diagram

Ihist () Interactive histogram

IMAP () Interactive map

Imosaic () interactive mosaic diagram

IPCP () interactive parallel coordinate diagram

Iplot () Interactive scatter plot

Cases:

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)

Now try the following steps.

? Click the No. third Gear bar in the bar chart (Gears) window and the bar will turn red. In addition, the rest of the graphics window

A three-gear engine will be highlighted.

? Move the mouse down and select a rectangular area with a bit in the scatter plot (WT to MPG) window. These points will be highlighted, and the corresponding observation points in the other graphics Windows will also turn red.

? Hold down the CTRL key and move the mouse to the midpoint, bar, box line, or line of a graphic, and the details of the object will be displayed in a pop-up window.

? Right-click on any object to get some options in the right-click menu. For example, you can right-click the box Line chart (MPG) window and turn the graphic into a parallel coordinate chart (PCP).

? Drag to select more than one object (points, bars, and so on), or use the SHIFT key to select non-contiguous objects by clicking.

You can try the third and fifth gear bars in the bar chart (Gears) window.

16.4.5 Rggobi

Ggobi has many eye-catching advantages, including: interactive scatter, histogram, parallel coordinate, time series, scatter matrix and three-dimensional rotation of the integrated use of window brush and point recognition, multivariable transformation method, complex exploration platform, such as guided animation and manual 1-D and 2-D graphics. The exciting is that the Rggobi package provides a seamless interface for Ggobi and R.

R in Action reading notes (22) 16th advanced Graphics Advance (bottom)

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.