R language drawing function use manual _r

Source: Internet
Author: User
R language Drawing function Daquan
One, Plot () function: Scatter plot
----#plot是用来画散点图的 ~ To the left is the dependent variable;
Plot (cars$dist~cars$speed), #y ~x
main= "Relationship between car distance & Speed", #表标题
Xlab = "Speed (miles per hour)", #X坐标轴标题
Ylab= "Distance travelled (Miles)", #Y坐标轴标题
Xlim=c (0,30), #设置X轴范围从0到30
Ylim=c (0,140), #设置Y轴范围从0到140
Xaxs= "I", #设置X轴风格internal
Yaxs= "I", #设置X轴风格internal
Col= "Red", #设置 "scatter" color is red
pch=19, #设置散点的形状为实心圆点

Second, draw the line chart
Sales<-read.csv ("Dailysales.csv", Header=true)

Plot (Sales$units~as. Date (Sales$date, "%d/%m/%y"))
#指定散点图类型为 "L" represents a drawing line chart
Type= "L"
Main= "Unitsales in the month of January 2010"
xlab= "Date"
ylab= "Number of units sold", col= "Blue"
Instances running on R:
#plot是用来画散点图的 ~ to the left is the dependent variable, the right is the independent variable
Plot (cars$dist~cars$speed,xlab= "Speed", Ylab = "Ditance", xaxs= "I", col= "Red", pch=19,main= "Relation Distance & Speed ")
Sales<-read.csv ("E:\\r_workspace\\dailysales.csv", Header=true)
Plot (Sales$units~as. Date (Sales$date, "%d/%m/%y"), type= "L"
, main= "Unitsales in the month of January", xlab= "Date"
, ylab= "Number of units sold", col= "Blue")


The lines () function---it is also a line drawing, but lines () is dependent on the scatter graph
Lines (Sales$units~as. Date (Sales$date, "%d/%m/%y"), col= "Red")

Note---High level mapping function and low level mapping function
1 high-level mapping function: can be independent drawing, such as plot ()
2 Low-level mapping function: You must first run a high-level mapping function drawing, and then add the picture on the existing diagram above
Instances running on R:
#lines ()
Plot (Sales$units~as. Date (Sales$date, "%d/%m/%y"))
Lines (Sales$units~as. Date (Sales$date, "%d/%m/%y"), col= "Red")



Third, scatter plot:
Visually displaying the distribution of sample data in a coordinate system;
Generally can draw 2-3 variable (dimension) sample data, the higher dimension can use the auxiliary identification method, for example: area, color, text and so on.
#绘制散点图y ~x
Plot (Cars$dist~cars$speed)
#绘画散点图也可以增加颜色设置
Plot (cars$dist~cars$speed,col= "Red")
#绑定数据集
Attach (Cars)
Plot (speed~dist)
Detach (Cars)
Par (Mfrow=c (1,3))
#type = "L" will turn the scatter plot into a line graph, and he connects each point to the picture
#xaxs = "I"---internal
Plot (dist~speed,main= "Relationshio about Dist and speed--i",
Xlab= "Speed (Meil)", ylab= "Dist", type= "L", col= "Red", xaxs= "I", xaxs= "I")

Plot (dist~speed,main= "Relationshio about Dist and speed-i",
Xlab= "Speed (Meil)", ylab= "Dist",
Col= "Red", xaxs= "I", xaxs= "I")
#xaxs = "R"--regular
Plot (dist~speed,main= "Relationshio about Dist and Speed--r",
Xlab= "Speed (Meil)", ylab= "Dist", col= "Red", xaxs= "R", yaxs= "R")

#一下的三种会报同样的错误------Error in Plot.window (...): Axis style "E" unimplemented
#xaxs = "S"--standard
Plot (dist~speed,main= "Relationshio about Dist and speed--s",
Xlab= "Speed (Meil)", ylab= "Dist", col= "Red", xaxs= "s", yaxs= "s")
#xaxs = "E"--extended
Plot (dist~speed,main= "Relationshio about Dist and speed--e",
Xlab= "Speed (Meil)", ylab= "Dist", col= "Red", xaxs= "E")
#xaxs = "D"--direct
Plot (dist~speed,main= "Relationshio about Dist and speed--d",
Xlab= "Speed (Meil)", ylab= "Dist", col= "Red", xaxs= "D")

#用lines把散点图画成线图
#先画出散点图
Plot (cars$dist~cars$speed,pch=19)
#用lines把散点联系起来
Lines (cars$dist~cars$speed,col= "red")
#加上属性pch = 19 Change the shape of a scatter to a solid dot
Lines (cars$dist~cars$speed,col= "red")

Column chart and Barplot () function
#从工作目录中读取文件
Sales<-read.csv ("G:\\r_workspace\\sales.csv")
Sales
#画柱形图 y,x
Barplot (sales$producta,names.arg=sales$city,col= "BLACK")
#加一个参数让柱状图横着画horiz =true; The map will turn 90 degrees
Barplot (sales$producta,names.arg=sales$city,horiz=true,col= "BLACK")
#设置一个版面可以看两个图
Par (Mfrow=c (1:2))
#彩色柱状图的画法
#legend主要用来产生右上角的图例图
#col =heat.colors (5): Heat can produce 5 vectors, that is, 5 different colors
Barplot (As.matrix (Sales[,2:4)), Beside=true,legend=sales$city,
Col=heat.colors (5), border= "white", main= "beside")
#去掉beside之后看效果
Barplot (As.matrix (Sales[,2:4)), Legend=sales$city,
Col=heat.colors (5), border= "white", main= "No beside")
> #看一下heat. Effect of colors
> col=heat.colors (5)
> Col
[1] "#FF0000FF" "#FF5500FF" "#FFAA00FF" "#FFFF00FF" "#FFFF80FF"

The difference between a histogram and a column chart
hist (Rnorm ())
An essential difference:
The ordinate of a column chart is his value (the value of the observed value itself).
The ordinate of the histogram represents the frequency (the number of observations)

Six, density drawing method:
Plot (Density (rnorm (1000))

Seven, Box diagram (Box line diagram)
The thick horizontal line in the box is the median (50% of the observations are larger than him, and 50% of the observations are smaller than him),
The upper box box is four-digit (25% of the observed value is larger than his, 75% of the observation is smaller than him); The box boxes are below four-digit (75% is bigger than him, 25% is smaller than him);
In the outermost two horizontal line there is called the tentacles, above a horizontal line is called the maximum value, the following one horizontal is called the minimum value, outside the box chart is outliers, (isolated point)
Judged according to some criteria (generally based on the deviation of the average number of standard deviations), after the threshold is considered to be outliers
Metals<-read.csv ("Metals.csv", Header=true)
#箱型图
BoxPlot (metals,xlab= "Metals", ylab= "atmospheric concentration in ng per cubic metre",
main= "Atmospheric concentration in London")
#从工作空间导入数据集
Drugs<-read.csv ("G:\\r_workspace\\drug.csv", Header=true)
Drugs
BoxPlot (drugs,xlab= "x", ylab= "Y")
Drug<-drugs[2:4]
Drug
BoxPlot (drug,xlab= "Drug", ylab= "density")

Eight, the thermal attempt
#热力图
Heatmap (As.matrix (Mtcars), Rowv=na,colv=na,
Col=heat.colors (256), scale= "column",
Margins=c (2,8), main= "Car characteristics by Model")

#基因热力图
Genes<-read.csv ("Genes.csv", header=t)
Rownames (genes) <-colnames (genes)
Image (X=1,ncol (genes), y=1:nrow (genes), z=t (As.matrix (Genes)), axes=false,xlab= "", ylab= "",
main= "Gene correlation Matrix")
Axis (1,at=1:ncol (genes), labels=colnames (genes), col= "white",
las=2,cex.axis=0.8)
Axis (1,at=1:ncol (genes), labels=rownames (genes), col= "white",
las=1,cex.axis=0.8)



> install.packages ("SP")
Error in install.packages:could the not find function "R" (>= 3.)

Install the package on the original Rgui


Scatter map Array (3x4); Axb--bxa understood to be different, there are 6 kinds
#对角线上是某个变量自己和自己的散点图 (do not make plans without meaning),
Pairs (Iris[,1:4])

#pch用实心圆
Plot (iris[,1:4],main= "relationships between characteristics of iris flowers"
, pch=19,col= "Blue", cex=0.9)
#在一张画板上画多个散点图
#画板上可以放两行三列的图
Par (mfrow=c (2,3))
Plot (rnorm), col= "Blue", main= "Plotno.1")
Plot (rnorm), col= "Blue", main= "plotno.2")
Plot (rnorm), col= "Blue", main= "plotno.3")
Plot (rnorm), col= "Blue", main= "Plotno.4")
Plot (rnorm), col= "Blue", main= "plotno.5")
Plot (rnorm), col= "Blue", main= "plotno.6")

#一个画板上指定三个图片1行3列
Market<-read.csv ("Dailymartket.csv", Header=true)
Par (Mfrow=c (3,1))
Plot (Market$revenue~as. Date (Market$date, "%d/%m/%y"), type= "L", main= "Revenue",
Xlab= "Date", ylab= "US dollars", col= "Blue")
Plot (Market$revenue~as. Date (Market$date, "%d/%m/%y"), type= "L", main= "profits",
Xlab= "Date", ylab= "US dollars", col= "Red")
Plot (Market$revenue~as. Date (Market$date, "%d/%m/%y"), type= "L", main= "Customer visits",
Xlab= "Date", ylab= "US dollars", col= "Black")


Some meticulous work of drawing:

1: #增加图例说明
Rain<-read.csv ("City.csv", Header=true)
#先画出散点图
Plot (rain$tokyo,type= "L", col= "Red", Ylim=c (0,300),
main= "Monthly rainly in major cities",
Xlab= "Month of Year", ylab= "rainfall (mm)",
lwd=2)
#在利用lines将点连接起来画成线图
Lines (rain$newyork,type= "L", col= "Blue", lwd=2)
Lines (rain$london,type= "L", col= "Blue", lwd=2)
Lines (rain$berlin,type= "L", col= "Blue", lwd=2)
#增加图例说明
#样式一
# "TopRight": put it in the upper right corner
#lty =1,lwd=2: The style of painting
Legend ("TopRight",
Legend=c ("Tokyo", "NewYork", "London", "Berlin"),
Col=c ("Red", "Blue", "green", "orange"), lty=1,lwd=2)

#样式二
# "Top": On the roof
#lty =1,lwd=2: The style of painting
Legend ("Top",
Legend=c ("Tokyo", "NewYork", "London", "Berlin"),
Ncol=4,cex=0.8,bty= "N",
Col=c ("Red", "Blue", "green", "orange"),
lty=1,lwd=2)


























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.