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")
> 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])
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.