R Intermediate Language drawings: FIG polyline

FIG polyline
data base using the installation Orange data set, for example, which contains the orange trees age and fifth annual ring data

 head(Orange)
Grouped Data: circumference ~ age | Tree
  Tree  age circumference
1    1  118            30
2    1  484            58
3    1  664            87
4    1 1004           115
5    1 1231           120
6    1 1372           142

Examples

#创建散点图和折线图代码示例
opar <- par(no.readonly = TRUE)
par(mfrow=c(1,2))
t1 <- subset(Orange, Tree==1)
plot(t1$age,t1$circumference,
     xlab="Age (days)",
     ylab="Circumference (mm)",
     main = "Orange Tree 1 Growth")
plot(t1$age,t1$circumference,type="b",
     xlab="Age (days)",
     ylab="Circumference (mm)",
     main = "Orange Tree 1 Growth")
par(opar)

Here Insert Picture Description
Line graph type selected by the type parameter
optional parameter type as follows
p: Only points
l: Line only
o: solid dots and lines (i.e., lines overlaid on the point)
does not draw point wire connection point (c: b, c )
S, S stepped line
h: the histogram of vertical line
n: points and lines do not generate any (usually used for the following command creates axis)
code sample

par(mfrow=c(2,4))
t1 <- subset(Orange, Tree==1)
plot(t1$age,t1$circumference,type="p",
     xlab="x",
     ylab="y",
     main = "type=p")
plot(t1$age,t1$circumference,type="l",
     xlab="x",
     ylab="y",
     main = "type=l")
plot(t1$age,t1$circumference,type="o",
     xlab="x",
     ylab="y",
     main = "type=o")
plot(t1$age,t1$circumference,type="b",
     xlab="x",
     ylab="y",
     main = "type=b")
plot(t1$age,t1$circumference,type="c",
     xlab="x",
     ylab="y",
     main = "type=c")
plot(t1$age,t1$circumference,type="s",
     xlab="x",
     ylab="y",
     main = "type=s")
plot(t1$age,t1$circumference,type="S",
     xlab="x",
     ylab="y",
     main = "type=S")
plot(t1$age,t1$circumference,type="h",
     xlab="x",
     ylab="y",
     main = "type=h")

Here Insert Picture Description
It can be seen from the figure, type = "P" typical scattergram generated, type = "b" to generate the most common line graph, different from that between the points b and c, or whether there is available between lines there is a gap.
type = "s" and type = "S" generates stepped line, but "s" is a line sideways objects, then rises, "S" is first increased, then sideways draw lines
through type = "n" can be used to create the axis, titles, and other graphical features, and then use the lines () and other functions required to add the curve

Fifth orange tree growing graphic showing the passage of time
code examples:

#为方便起见,将因子转换为数值型
par(opar)
Orange$Tree <- as.numeric(Orange$Tree)
ntrees <- max(Orange$Tree)

#range返回给定向量的最大值及最小值
xrange <- range(Orange$age)
yrange <- range(Orange$circumference)

#创建图形
plot(xrange,yrange,
     type="n",
     xlab="Age (days)",
     ylab="Circumference (mm)")
#设置线性颜色
colors <- rainbow(ntrees)
#设置线条类型
linetype <- c(1:ntrees)
#设置点的类型
plotchar <- seq(18, 18+ ntrees,1)

for (i in 1:ntrees) {
   tree <- subset(Orange,Tree==i)
   lines(tree$age,tree$circumference,
         type="b",
         lwd=2,
         lty=linetype[i],
         col=colors[i],
         pch=plotchar[i])
}

title("Tree Growth","example of line plot")

#添加图例
legend(xrange[1],yrange[2],legend = c(1,2,3,4,5),col=colors,lty=linetype,pch=plotchar)

Here Insert Picture Description

Published 39 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42712867/article/details/100129792