In this tutorial, let us first begin by understanding the basics using the plot() function in R. The R language is well known for its beautiful graphics with a rich set of functions to build and format any kind of graphs and the plot() function family one that helps us build those.
在本教程中,首先让我们首先了解使用R中的plot()函数的基础。R语言以其精美的图形而著称,它具有丰富的函数集,可用于构建和格式化任何种类的图形以及plot()功能家族之一,可以帮助我们构建这些家族。
The plot() function in R isn’t a single defined function but a placeholder for a family of related functions. The exact function being called will depend upon the parameters used. At its simplest, plot() function simply plots two vectors against each other.
R中的plot()函数不是单个定义的函数,而是一系列相关函数的占位符。 确切的函数调用将取决于所使用的参数。 最简单的说,plot()函数只是简单地绘制两个向量 。
plot(c(1,2,3,4,5),c(1,4,9,16,25))
This gives a simple plot for y = x^2.
这给出了y = x ^ 2的简单图。
![sSquare plot() function in R](https://i-blog.csdnimg.cn/blog_migrate/18643ba0a15642bcb703a2b66d84bdfd.png)
使用R中的plot()函数更改图形外观 (Changing Graph Appearance with the plot() function in R )
The plot() function in R can be customized in multiple ways to create more complex and eye-catching plots as we will see.
可以通过多种方式自定义R中的plot()函数,以创建更复杂且引人注目的图。
- The shape of the markers: The plot markers are by default small, empty circles. These are also known as plot characters – denoted by pch. You can change these by adding a new pch value in the plot function. Pch values 0 to 25 are valid and give several different symbols on the graph. Pch 0 is for a square, 1 is for a circle, 3 is for a triangle, 4 is for a cross and so on. 标记的形状:默认情况下,绘图标记是小的空圆圈。 这些也称为绘图字符-用pch表示。 您可以通过在绘图功能中添加新的pch值来更改它们。 Pch值0到25是有效的,并在图形上给出几个不同的符号。 Pch 0表示正方形,1表示圆形,3表示三角形,4表示十字,依此类推。
- Size of the plot markers: This aspect of a graph can be controlled using the cex parameter. The cex parameter can be set to 0.5 if you want the markers to be 50% smaller and 1.5 if you want them to be 50% larger. 绘图标记的大小 :可以使用cex参数控制图形的这一方面。 如果希望将标记缩小50%,可以将cex参数设置为0.5;如果希望将标记放大 50%,则可以将cex参数设置为1.5。
- Color of the plot markers: The symbols can be assigned one or many colors. These colors can be selected from a list provided by R under the
colors()
function. 绘图标记的颜色:可以为符号分配一种或多种颜色。 这些颜色可以从R在colors()
函数下提供的列表中选择。 - Connecting the points with lines: Many times, it is necessary to connect the displayed points with different kinds of lines. This can be done using the type attribute of the plot function. The type attribute set to ‘p’ refers to only points and ‘l’ to only a line. Similarly, values ‘b’ and ‘o’ are for lines connecting points and overlaying points respectively. To get a histogram like display the ‘h’ option is used and ‘s’ is used for a step option. 用线连接点:很多时候,需要用不同种类的线连接显示的点。 可以使用plot函数的type属性来完成。 设置为'p'的type属性仅指点,而'l'仅指线。 类似地,值“ b”和“ o”分别用于连接点和覆盖点的线。 要获得类似于显示的直方图,可以使用“ h”选项,而将“ s”用作步进选项。
- Varying the lines: The line type can be specified by the lty parameter (range 0 to 6) and line width is set using an lwd parameter. 换行:可以通过lty参数(范围为0到6)指定线的类型,并使用lwd参数设置线宽 。
Let us now try constructing a few graphs with what we learned so far.
现在,让我们尝试使用到目前为止所学的内容来构建一些图形。
We will begin by generating a sine wave plot. Let x be a sequence vector of values from -pi to pi with 0.1 intervals and y contains the respective sine values of x. Now try plotting y against x.
我们将从生成正弦波图开始。 令x为从-pi到pi的值的序列向量,间隔为0.1,并且y包含x的相应正弦值。 现在尝试将y与x作图。
x=seq(-pi,pi,0.1)
y=sin(x)
plot(x,y)
![Sine wave from -pi to pi - plot() function in R](https://i-blog.csdnimg.cn/blog_migrate/165bdf7d91aec920ca3d9f1c65e41759.png)
Let us now try changing the symbols and colors.
现在让我们尝试更改符号和颜色。
plot(x,y,pch=c(4,5,6),col=c('red','blue','violet','green'))
We are now enabling the compiler to select from 3 different symbols and 4 different colors for marking the graph. Let us see how it turned out.
现在,我们使编译器可以从3种不同的符号和4种不同的颜色中进行选择,以标记图形。 让我们看看结果如何。
![Colorful Sine - plot() function in R](https://i-blog.csdnimg.cn/blog_migrate/800c430ec39efa2bc6a690255fed1ff8.png)
R also allows combining multiple graphs into a single image for our viewing convenience using the par() function. We only need to set the space before calling the plot function in our graph.
R还允许使用par()函数将多个图形组合为一个图像,以方便我们查看。 我们只需要在调用图形中的plot函数之前设置空间即可。
#Set a plotting window with one row and two columns.
par(mfrow=c(1,2))
plot(x,y,type='l')
plot(x,y,pch=c(4,5,6),col=c('red','blue','violet','green'))
![Combining Plots In R using the plot() function in R](https://i-blog.csdnimg.cn/blog_migrate/17259463a08c010a355c71c9fefee8ed.png)
A few more graphs using various options from above are illustrated below.
下面显示了一些使用上述各种选项的图形。
#Set space for 2 rows and 3 columns.
par(mfrow=c(2,3))
#Plot out the graphs using various options.
plot(x,cos(x),col=c('blue','orange'),type='o',pch=19,lwd=2,cex=1.5)
plot(x,x*2,col='red',type='l')
plot(x,x^2/3+4.2, col='violet',type='o',lwd=2,lty=1)
plot(c(1,3,5,7,9,11),c(2,7,5,10,8,10),type='o',lty=3,col='pink',lwd=4)
plot(x<-seq(1,10,0.5),50*x/(x+2),col=c('green','dark green'),type='h')
plot(x,log(x),col='orange',type='s')
The resulting graph looks like this.
生成的图形如下所示。
![Multiple Plots In R](https://i-blog.csdnimg.cn/blog_migrate/e06b556d5e18472adc3615b93600cd51.png)
在R中使用plot()函数向图添加更多信息 (Adding More Information to Graphs with plot() Function in R)
Graphs look more complete when there are notes and information that explain them. These include a title for the chart and axes, a legend of the graph. Sometimes even labeling the data points will be necessary. Let us look at how we add these to the graphs in R.
当有注释和解释它们的信息时,图形看起来更加完整。 其中包括图表标题和坐标轴,图例。 有时甚至需要标记数据点。 让我们看看如何将它们添加到R中的图形中。
- The main title is added using the main option in the plot function. The font, color, and size can be customized using the font.main, col.main and cex.main respectively. 使用绘图功能中的main选项添加主标题。 可以分别使用font.main , col.main和cex.main来定制字体,颜色和大小。
- The titles for the axes are provided using xlab and ylab attributes. These can be customized using font.lab, col.lab and cex.lab like above. 使用xlab和ylab属性提供了轴的标题。 可以使用上述的font.lab , col.lab和cex.lab进行自定义。
- You can also add some extra text inside the plot using the text attribute, specifying the text to use and the coordinates to display. 您还可以使用text属性在绘图内添加一些额外的文本 ,指定要使用的文本和要显示的坐标。
- The text attribute can also be used to label the data points. The text, in this case, is a vector of labels instead of a string. 文本属性也可以用于标记数据点。 在这种情况下,文本是标签的向量,而不是字符串。
- The legend can be added to a graph using the R’s legend() function. Legend takes as input the coordinates, text and the symbols to be interpreted. 可以使用R的legend()函数将图例添加到图形中。 图例输入要解释的坐标,文本和符号。
Let us look at examples illustrating these.
让我们看看说明这些的例子。
#Displaying the title with color
plot(c(1,3,5,7,9,11),c(2,7,5,10,8,10),type='o',lty=3, col='pink',lwd=4,main="This is a graph",col.main='blue')
![Graph With A Title](https://i-blog.csdnimg.cn/blog_migrate/ebe35c7ec121e7bf8911caf84062f333.png)
#Same graph with xlabel and ylabel added.
> plot(c(1,3,5,7,9,11),c(2,7,5,10,8,10),type='o',lt=3,col='pink',lwd=4,main="This is a graph",col.main='blue',xlab="Time",ylab="Performance")
![Graph With Title And Labels](https://i-blog.csdnimg.cn/blog_migrate/27b04989317a4ea3b1416b14ae7ea3bc.png)
Let us add a label to each of the data points in the graph using a text attribute.
让我们使用文本属性将标签添加到图形中的每个数据点。
labelset <-c('one','three','five','seven','nine','eleven')
x1<- c(1,3,5,7,9,11)
y1 <- c(2,7,5,10,8,10)
plot(x1,y1,type='o',lty=3,col='pink',lwd=4,main="This is a graph",col.main='blue',xlab="Time",ylab="Performance")
text(x1+0.5,y1,labelset,col='red')
![Graph With Data Labels](https://i-blog.csdnimg.cn/blog_migrate/c15f4336bfe3f4b09700d5d5222c0111.png)
Finally, let us add a legend to the above graph using the legend() function.
最后,让我们使用legend()函数向上述图形添加图例。
> legend('topleft',inset=0.05,"Performace",lty=3,col='pink',lwd=4)
The position can be specified by either x and y coordinates or using a position like ‘topleft’ or ‘bottomright’. Inset refers to moving the legend box a little to the inside of the graph. The resulting graph now has a legend.
可以通过x和y坐标或“ topleft”或“ bottomright”之类的位置来指定位置。 插图是将图例框稍微移到图形内部。 生成的图形现在具有图例。
![Graph With Legend](https://i-blog.csdnimg.cn/blog_migrate/20be693316e50f8fd9ef06c54819f2bf.png)
叠加图 (Overlaying Graphs)
R also allows two graphs to be displayed on top of each other instead of creating a new window for every graph. This is done by calling a lines() function for the second graph rather than plot() again. These are most useful when performing comparisons of metrics or among different sets of values. Let us look at an example.
R还允许两个图显示在彼此的顶部,而不是为每个图创建一个新窗口。 这是通过为第二个图形而不是plot()调用lines()函数来完成的。 在执行指标比较或不同值集之间时,这些功能最为有用。 让我们来看一个例子。
x=seq(2,10,0.1)
y1=x^2
y2=x^3
plot(x,y1,type='l',col='red')
lines(x,y2,col='green')
legend('bottomright',inset=0.05,c("Squares","Cubes"),lty=1,col=c("red","green"),title="Graph type")
![Overlaid Graph With Legend](https://i-blog.csdnimg.cn/blog_migrate/35216359d756a86466af8ecfa5c0b71c.png)
在图上添加线 (Adding Lines to a Plot)
Straight lines can be added to an existing plot using the simple abline()
function. The abline() function takes 4 arguments, a, b, h, and v. The variables a and b represent the slope and intercept. H represents the y points for horizontal lines and v represents the x points for vertical lines.
可以使用简单的abline()
函数将直线添加到现有绘图中。 abline()函数采用a,b,h和v这4个参数。变量a和b表示斜率和截距。 H代表水平线的y点,v代表垂直线的x点。
Let us look at an example to make this clear. Try executing these three statements after building the above graph for squares and cubes.
让我们看一个例子来阐明这一点。 在为正方形和立方体构建以上图形之后,尝试执行这三个语句。
abline(a=4,b=5,col='blue')
abline(h=c(4,6,8),col="dark green",lty=2)
abline(v=c(4,6,8),col="dark green",lty=2)
![Graph With Lines](https://i-blog.csdnimg.cn/blog_migrate/0e19c6a33589ebd2dfcaae4a04dfcc80.png)
The first blue line is built with the slope and intercept specified. The next sets of three horizontal and vertical lines are drawn at the specified x and y values in the dotted line style as mentioned by lty=2.
第一条蓝线是使用指定的斜率和截距建立的。 如lty = 2所述,下一组三条水平线和垂直线以虚线样式在指定的x和y值处绘制。
This covers the basics of plot function in R. When combined with other packages like ggplot2, R builds the most presentable and dynamic graphics as we will see in further tutorials.
这涵盖了R中plot函数的基础。当与ggplot2之类的其他软件包结合使用时,R可以构建最具代表性和动态的图形,我们将在进一步的教程中看到。