R language - data visualization

content

Common plotting functions:

Low-level plotting functions, adding elements to existing graphics:

Advanced plotting functions:

1. Draw bar graph barplot()

        2. Draw a pie chart pie()

        3. Draw a histogram hist()

        4. Draw a scatter plot plot()

        5. Draw a line graph plot()


Common plotting functions:

main: main title

xlim, ylim: the range of the coordinate axis

xlab, ylab: labels for the axes

type: graph type: "p": point, "l": line, "b" and "o": point-to-point line, "h": vertical line, "s": stepped

bg: background color

cex: size of symbols and text

col: the color of symbols and text

font: text font: 1: normal, 2: italic, 3: bold, 4: bold italic

lty: Line type of the connection: 1: Solid line, 2: Dotted line, 3: Dotted line, 4: Dotted line, 5: Long dotted line, 6: Double dotted line

lwd: the width of the line

pch: type of symbol: integer from 1 to 25

mar: the margin of the figure: vector c(bottom, left, top, right)

mfcol: vector c(nr,,nc), divide the drawing window into nr rows and ncne columns, use each sub-window in column order

mfrow: same as above, but use subwindows in row order

Low-level plotting functions, adding elements to existing graphics:

point(x,y): add a point at (x,y)(x,y)

lines(x,y): add lines at (x,y)(x,y)

text(x,y,labels): add the label specified by labels at (x,y)(x,y)

abline(a,y): Add a straight line with slope bb and intercept aa

abline(h=y): Add a horizontal line at the ordinate yy

abline(v=x): Add a vertical line at the abscissa xx

abline(lm.obj): add the regression line determined by lm.obj

legend(x,y,labels): Add the legend specified by legend at (x,y)(x,y)

title(): add title

box(): Use the box() function to generate a box plot

Advanced plotting functions:


1. Draw bar graph barplot()

barplot(height, names.arg = NULL, beside = FALSE,horiz = FALSE, density = NULL, angle = 45,col = NULL, border = par("fg"),main = NULL, sub = NULL, xlab = NULL, ylab = NULL,xlim = NULL, ylim = NULL,las=NULL ...)

The meanings of the main parameters are as follows:

height: A vector or matrix that is used to form the value of each bar in the bar graph. (data)

names.arg: Text labels at the bottom of the bar. (give a type label name)

beside: Logical value, when FALSE draws a stacked graph, when TRUE draws a grouped graph.

horiz: Logical value, when FALSE, draw a vertical bar graph, when TRUE, draw a horizontal bar graph.

density: a vector value. When this value is specified, the bars are filled with slashes. That is, the density of slashes per inch.

angle: The angle of the hatch line given in counter-clockwise direction. The default is 45 degrees.

col: The fill color of the bar.

border: The border color of the bar, if set to TRUE, the border color will be the same as the hatched line color.

main: Used to specify the main title of the plot.

sub: Used to specify the subtitle of the plot.

xlab, ylab : Used to specify labels for the x- and y-axes.

xlim, ylim: used to specify the value range of the x-axis and y-axis, respectively.

las: It means that the labels on the horizontal axis are written horizontally, and the labels on the vertical axis are written vertically; las=1 means that the labels on the horizontal axis and the vertical axis are written horizontally; las=2, it means that the labels on the horizontal axis are written vertically, and the labels on the vertical axis are written vertically. Labels are written horizontally; las=3, which means that the labels on the horizontal and vertical axes are all written vertically.

The data for the following test comes from a package---ggplot2, which is installed below:

chooseCRANmirror() #Select the mirror Chain
utils:::menuInstallPkgs() #Select ggplot2

 E.g:

barplot(mpg$displ [1:5],names.arg=c("BMW","Audi","Czech","Wuling Hongguang","Jeep"),beside=F,horiz="F",density =c(10),angle=45,col=rainbow(6),border="green",main="Car Sales",sub="Ranking",xlab="Car Type",ylab="Turnover /billion",las=1)


2. Draw a pie chart pie()

Function prototype:

pie(x, labels = names(x), edges = 200, radius = 0.8,
    clockwise = FALSE, init.angle = if(clockwise) 90 else 0,
    density = NULL, angle = 45, col = NULL, border = NULL,
    lty = NULL, main = NULL, ...)

Main parameter analysis:

x: a formal array, such as C(10, 30), each value in the array determines the size of the pie slice (data)

labels: the description of a pie chart sector, the proportion of the sector to be displayed in R, (the label of each sector)

edges: It is very bitter to translate, I feel that this parameter determines the sharpness of the border, the default is 200 (something like pi)

radius: the radius of the pie chart, the default is 0.8

clockwise: FALSE,-- Determines whether the slice is drawn clockwise or counterclockwise (i.e. the mathematically positive direction)

density: NULL, -- fill in a number, the shade density of the sector (slash fills the sector)

angle: 45 (slash fill angle)

col: NULL, color fill

border: The border color of the sector. If set to TRUE, the border color will be the same as the fill color.

lty: line shape of the pie chart border 1: solid line, 2: dotted line, 3: dotted line, 4: dotted line, 5: long dotted line, 6: double dotted line

main: Used to specify the main title of the plot.

sub: Used to specify the subtitle of the plot.

E.g:

pie(x=c(8,5,3,4),labels=c("Tang Bohu","Zhu Zhishan","Wen Zhengming","Liu Zhiqing"),edges=500,radius=1

,clockwise=TRUE,density=NULL,angle=45,col=rainbow(9),border=FALSE,lty=NULL,main=

"Popularity List", sub="Popularity List")


3. Draw a histogram hist()

Function prototype:

hist(
mapping = NULL, # mapping
data = NULL, # dataset
stat = "bin", # histogram type
position = "stack", # position
 ..., # parameters of other geom functions
binwidth = NULL, # histogram Spacing
bins = NULL, # The number of histograms, similar to binwidth, but with different logic settings
na.rm = FALSE, # Logical parameter, true value closes the missing value error
orientation = NA, # direction
show.legend = NA, # Logical parameter, whether to display the legend of this layer, NA is the default
inherit.aes = TRUE # Logical parameter, whether to overlay this layer and the default geometric elements
 )

Main parameter analysis:

hist(mpg$hwy,breaks=7,freq=FALSE)

breaks: number of groups

freq: Indicates whether to display the frequency (yes) or frequency (no) of the sample values

lines(density(mpg$hwy))

density(): Returns the kernel density estimate for the variable "hwy"

lines(): Indicates adding a curve to an existing layer

Plot the kernel density:

plot(density(mpg$hwy))

hist(mpg$hwy,breaks=7,freq=FALSE)

lines(density(mpg$hwy))

plot(density(mpg$hwy))

box()


4. Draw a scatter plot plot()


5. Draw a line graph plot()

Guess you like

Origin blog.csdn.net/qq_21402983/article/details/123856699