Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the graphics realized in R language?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the graphics realized in R language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the graphics realized in R language"?

Bar chart-Bar Plot

The main function of a bar chart is to show the distribution (frequency) of Categorical variables in the form of horizontal or vertical bars. R provides the barplot () function to support the creation of bar charts in its simplest form: barplot (height). Where height is a vector or matrix, if it is a vector, as shown on the left, the matrix depicts a stacked histogram (beside=FALSE) or group histogram (beside=TRUE):

[hint] if the Categorical variable is factor or ordered factor, then directly use the plot function to produce the same effect as barplot

The following code creates a more complete bar chart:

Barplot (m, main= "Sales byRegion", col=c ("red", "green", "grey", "blue"), xlab= "region", ylab= "Sales")

The spine () function produces a 100% stacked bar chart similar to that in × × S:

Pie chart

In addition to bar / bar charts, pie charts are another type of graph that is often used to depict the distribution of variables of type Categorical. Although bar charts are popular, many statisticians, including the authors of this book, do not recommend pie charts, but recommend bar charts or dot charts, because it is easier for people to judge length than area. The pie chart is created using the following function:

Pie (x, labels)

X is a non-negative numerical vector to indicate the size of each slice of the pie chart, and lables is a character vector indicating the label of each slice.

The problem with the pie chart is that it is difficult to compare the size between different slices in the pie chart, unless there is a numerical label on each slice, so you have the following fan chart:

Histogram

The histogram is used to show the distribution of continuous variables. it divides the continuous data into small grids on the x-axis and displays the frequency for each grid on the Y-axis. R uses the following function to create a histogram:

Hist (XGrammy frequre trust, false, breaks=value, col= "red", xlab= "annotation in x axis", main= "title of the hist")

Where x is a continuous numerical vector, if freq represents a histogram based on frequency for TRUE, otherwise it creates a histogram based on frequency; breaks indicates that the histogram will create several small boxes on the X axis:

In addition, after executing the hist function to draw a histogram, we can call other drawing functions (such as lines) to add a line to the histogram, as shown in the figure above, the kernel density estimation (KDE) of the dataset is depicted:

Nuclear density diagram

Kernel density estimation is an effective method to evaluate the probability density function of continuous random variables. We have seen previously that the probability density function of a continuous random variable is expressed in the form of a histogram, and the kernel density estimation describes the probability density in a smoother way. The form of its function is

Density (x)

The following figure shows the kernel density estimation of the mpg variable in mtcars using plot (density (mtcars$mpg)). This example is drawn directly with plot, and plot creates a new graph, using the lines () function if you want to attach to an existing one:

Another important function of kernel density estimation is to compare the probability distribution of a variable between different groups, which is provided by the sm.density.compare () function of sm packet. The following functions and graphs are used to view the probability density function of mpg in cyl groups:

Sm.density.compare (mpg, cyl, xlab= "Miles PerGallon")

Box drawing

The box chart describes the distribution of a continuous variable by graphing five characteristic quantities of a continuous variable (minimum, first quartile, median, third quartile and maximum). Besides the first time, it may also show outliers (numbers other than plus or minus 1.5 times IQR).

For example, boxplot (mtcars$mpg) displays the box diagram below. We can use boxplot.stats (mtcars$mpg) to print the values of five eigenvalues.

As we see in the kernel density map, the grouping comparison is made using the box diagram between different groups. Its function form is: boxplot (formula,data=dataframe), where data specifies the data frame object on which the box diagram is based, formula is the formula shaped like ytree A, y refers to the variable described in the box diagram, and A refers to the grouping of variables y based on this column (or multiple columns of aquib) to do the box diagram. The following figure is based on cyl grouping mpg: boxplot (mpg~cyl,data=mtcars). We can see that the mpg data of a six-cylinder car is more symmetrical:

Dot graph

Http://lectures.molgen.mpg.de/Pairwise/DotPlots/index.html

Dot diagrams provide a way to draw a large number of labeled values on a simple horizontal axis. R provides the dotchart () function to achieve this capability. The simplest way to call it is:

Dotchart (x, labels=)

Where x is a numeric vector and labels is a label for each numeric value indicated by a character vector. The following figure shows the point diagram we created using dotchart (mpg,labels=row.names (mtcars), cex=0.7):

In addition to the simple form above, a more interesting representation of the dot graph is that we can sort it first and then use the groups option to instruct each point to be grouped by a factor variable, as shown in the following figure:

At this point, I believe you have a deeper understanding of "what are the graphics realized in R language?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report