R in Action reading notes (14) Chapter 11th Intermediate Drawing: Scatter chart (high energy warning)

Source: Internet
Author: User

11th Chapter Intermediate Drawing

The contents of this chapter:

Visualization of the relationship between bivariate variables and multivariate variables

Plot scatter plots and line charts

Understanding related diagrams

Learning TreeMap and correlation diagrams

The functions used in this chapter are:

Plot

Hexbin

Ablines

Iplot

Scatterplot

Scatterplot3d

Pairs

Plot3d

Scatterplotmatrix

Scatter3d

Cpairs

Symbols

Smoothscatter

11.1 Scatter chart

Scatter plots with best-fit curves added

> Attach (Mtcars) > Plot (wt,mpg,main= "Basic scatter plot of Mpgvs.weight", xlab= "car weight (lbs/1000", ylab= "Miles Pergallon ", pch=19) >abline (LM (MPG~WT), col=" Red ", Lwd=2,lty=1) #添加最佳拟合的线性直线 >lines (lowess (wt,mpg), col=" Blue ", lwd=2,lty=2) #添加一条平滑曲线

  

The Scatterplot () function in the car package enhances many of the features of the scatter plot, which makes it easy to plot a scatter plot and

You can add fit curves, bounding box plots, and confidence ellipses, as well as draw in subsets and interactively identify points.

> Library (CAR) > Scatterplot (mpg~wt|cyl,data=mtcars,lwd=2,+ main= "scatter plot of PMG \ n vs. Weight by #cylinders", + xlab= "Car Weight (lbs/1000)", + ylab= "miles per gallon", + legend.plot=true, #左上边界添加图例 + id.method= "identify", + labels= Row.names (Mtcars), #可通过点的行名称来识别点 + boxplots= "XY")

11.1.1 Scatter graph matrix

The pairs () function creates the underlying scatter graph matrix.

> Pairs (~mpg+disp+drat+wt,data=mtcars,+ main= "Basic scatter Plot matrix")

The six scatter plots above and below the main diagonal are the same, and the option Upper.panel =null will generate only the lower triangular shape.

The Scatterplotmatrix () function in the car package can also generate a scatter graph matrix with the following optional actions:

The scatter graph matrix is plotted on the condition of a factor;

Includes linear and smooth fit curves;

Place the box plot, density map, or histogram on the main diagonal line;

Add an axis plot to the boundaries of each cell.

> Library (CAR) > Scatterplotmatrix (~mpg+disp+drat+wt,data=mtcars,spread=false,+ lty.smooth=2,main= "scatter Plot matrix via car package ")

  

The linear and smooth (loess) fitting curves are added by default, and the kernel density curve and the axis graph are added at the main diagonal. The spread = False option means that no lines exhibiting dispersion and symmetry information are added, and Lty.smooth = 2 sets the smoothing (loess) fit curve using dashed lines instead of solid lines.

Another use of the Scatterplotmatrix () function

The > Library #主对角线的核密度曲线改成了直方图, and the histogram is drawn on the condition of the number of cylinders of each vehicle. > Scatterplotmatrix (~mpg+disp+drat+wt|cyl,data=mtcars,spread=false,+ diagonal= "histogram", main= "scatter plot Matrix via Carpackage ")

  

The core density curve of the main diagonal is changed to a histogram, and the histogram is drawn on the condition of the number of cylinders of each vehicle. The graph contains a histogram in the main diagonal and a linear and smooth fit curve for other parts. In addition, subgroups (depending on the number of cylinders) are differentiated by symbol type and color by default, the regression line fits the entire sample, including the option By.groups = True to produce a fitting curve based on each subset.

The Cpairs () function in the Gclus package provides an interesting matrix variant of the scatter plot. It contains options to rearrange the position of variables in the matrix, allowing more relevant variables to be closer to the main diagonal. The function can also color-code each cell to show the correlation size between variables. First, investigate the relevance:

>cor (Mtcars[c ("mpg", "wt", "disp", "Drat")])

MPG WT disp DRAT

MPG 1.0000000-0.8676594-0.8475514 0.6811719

wt-0.8676594 1.0000000 0.8879799-0.7124406

disp-0.8475514 0.8879799 1.0000000-0.7102139

Drat 0.6811719-0.7124406-0.7102139 1.0000000

Scatter graph matrix generated by Gclus package

> Library (gclus) > Mydata<-mtcars[c (1,3,5,6)]> mydata.corr<-abs (Cor (mydata)) #相关系数的绝对值 > MyColors <-dmat.color (Mydata.corr) #获取绘图的颜色 > Myorder<-order.single (Mydata.corr) #重排对象 to make similar objects closer to > Cpairs ( MyData, myorder,panel.colors=mycolors,gap=.5,main= "variablesordered and colored by correlation")

The scatter chart matrix will be plotted, shaded, and gap options based on the new variable Order (myorder) and Color list (mycolors)

Make the spacing between the cells of the matrix a little bit larger. The most relevant variable pairs are vehicle weight and displacement, and mileage per gallon with vehicle weight (marked red and closest to the main diagonal)

11.1.2 High Density scatter plot

When the number of points overlap is very serious, it is "powerless" to use scatter plots to observe the variable relationship.

> set.seed (1234) > N<-10000> C1<-matrix (Rnorm (n,mean=0,sd=.5), ncol=2) > C2<-matrix (Rnorm (N,mean =3,sd=2), ncol=2) > Mydata<-rbind (C1,C2) > Mydata<-as.data.frame (MyData) > Names (mydata) <-c ("X", "Y" ) > with (Mydata,plot (x,y,pch=19,main= "scatter plot with 10000 observations"))

The Smoothscatter () function uses the kernel density estimate to generate a scatter plot of the point distribution using the color density estimation and a scatter plot based on the light smoothing density estimate. Here the density is more readable:

> with (MyData, Smoothscatter (x,y,main= "scatter plot with 10000observations"))

The Hexbin () function in the Hexbin package places the container of the two-dollar variable in the Hexagon cell (the graph is more intuitive than the name)

> Library (Hexbin) > with (Mydata,{bin<-hexbin (x,y,xbins=50) plot (bin,main= "hexagonalbinning with \ n 10000 Boservations ")})

The Iplot () function in the Idpmisc package can also be used to show the density of points by color (the data points at a particular point

Number

> Library (IDPMISC) > with (mydata,{iplot (x,y,main= "imagescatter plot with\n color indicating density")})

11.1.33-D Scatter plot

If you are interested in the relationship between mileage, vehicle weight and displacement, you can use the Scatterplot3d

The Scatterplot3d () function to draw their relationship. Scatterplot3d (x, Y, z) × is drawn on the horizontal axis,y is drawn on the vertical axis, andZ is drawn on the pivot axis.

> Library (Scatterplot3d) > Attach (Mtcars) > Scatterplot3d (wt,disp,mpg,main= "Basic 3d scatter plot")

Note: If error in Plot.new () is present: Figure margins too large restart Rstudio

The Satterplot3d () function provides many options, including setting graphical symbols, axes, colors, lines, gridlines,

Features such as display and angle

> Scatterplot3d (Wt,disp,mpg, pch=16,highlight.3d=true,type= "H", main= "3dscatter plot with vertical lines")

Add a regression polygon

> S3d<-scatterplot3d (wt,disp,mpg,pch=16, Highlight.3d=true, type= "H", main= "3dscatter plot with vertical lines") > fit<-lm (mpg~wt+disp) > S3d$plane3d (FIT)

Rotating three-dimensional scatter plot

Create an interactive three-dimensional scatter plot with the Plot3d () function in the RGL package. You can use the mouse to enter the graphics

Line rotation. function format: Plot3d (x, Y, z)

where x,y , and z are numeric vectors that represent each point. You can also add options such as col and size to individually control

The color and size of the dots.

> Library (RGL) > Attach (Mtcars) > Plot3d (wt,disp,mpg,col= "Red", size=5)

You can also use a similar function Scatter3d () in the RCMDR package:

> Library (RCMDR) > Attach (Mtcars) > Scatter3d (wt,disp,mpg)

The Scatter3d () function can contain a variety of regression surfaces, such as linear, two-time, smoothing, and additional types. Graphics default Add

Add a linear plane. In addition, there are options available for interactive recognition points in the function.

11.1.4 Bubble Chart

Three-dimensional scatter plots to show the relationship between the two quantitative variables. Here's another idea: create a two-dimensional scatter plot, and then use the size of a point to represent the value of the third variable. This is the bubble chart (bubble plot).

You can use the symbols () function to create a bubble chart. The function can draw a circle graph, a square in the specified (x, y) coordinates.

Charts, star charts, thermometer charts, and box-line plots. To draw a circle chart, for example:

Symbols (X,y,circle=radius)

where x,y , and radius are vectors that need to be set, representing the x,y , and circle radii, respectively.

The third variable is represented by an area rather than a radius, then the formula for the circle radius (r = A /π) changes

Change: Symbols (x,y,circle=sqrt (z/pi))Z is the third variable to be drawn.

> Attach (Mtcars) > R<-sqrt (DISP/PI) > Symbols (wt,mpg,circle=r,inches=.3, #比例因子, controls the size of the circle (default maximum circle is 1 inches) + fg= " White ", bg=" LightBlue ", main=" bubble plotwith point size\n proportional to displacement ", ylab=" Miles Pergallon ", xlab=" Weight of car (lbs/1000 ") > Text (wt,mpg,rownames (mtcars), cex=.6) #可选函数, used here to add Names > Detach (mtcars) for each car


Seeking attention and spreading friends and family

R in Action reading notes (14) Chapter 11th Intermediate Drawing: Scatter chart (high energy warning)

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.