且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用plot绘制图形

更新时间:2022-06-12 05:37:29

plot()是最常用 R绘图函数,是一个泛型函数(允许参数类型可变),它产生的图形依赖于第一个参数的类型或者类.
> set.seed(1 ); x <- sample(c(1:50),10)
> set.seed(2); y <- sample(c(1:50),10)
> xt <- ts(x)
> xy <- cbind(x, y)
> f <- as.factor(c(rep('a',3),rep('b',5),rep('c',2)))
> x
 [1] 14 19 28 43 10 41 42 29 27  3
> y
 [1] 10 35 28  8 44 43  6 36 20 23
> xt
Time Series:
Start = 1 
End = 10 
Frequency = 1 
 [1] 14 19 28 43 10 41 42 29 27  3
> xy
       x  y
 [1,] 14 10
 [2,] 19 35
 [3,] 28 28
 [4,] 43  8
 [5,] 10 44
 [6,] 41 43
 [7,] 42  6
 [8,] 29 36
 [9,] 27 20
[10,]  3 23
> f
 [1] a a a b b b b b c c
Levels: a b c


x是向量, 按x元素位置绘制点图, 横轴为元素位置, 纵轴为x的值.
> plot(x)
使用plot绘制图形

xt是时序, 绘制时间序列的线图
> plot(xt)
使用plot绘制图形

f是因子, 绘制每个水平柱状图.
plot(f)
使用plot绘制图形

绘制x为横轴, y为纵轴的点图
> plot(x,y)
> plot(xy)
使用plot绘制图形

绘制横轴为factor, 众轴为向量.  
纵轴上画盒须图, 中间是平均值, 两端分别是最大最小值, 盒子的两端分别代表中位数分开的两个区间的平均值.
> plot(f,y)
使用plot绘制图形

如果把向量放第一个参数, 因子放第二个参数, 绘制的是y为横轴, f转换为numeric的点图.
> as.numeric(f)
 [1] 1 1 1 2 2 2 2 2 3 3
> y
 [1] 10 35 28  8 44 43  6 36 20 23
plot(y,f)
使用plot绘制图形

[参考]
> help(plot)

plot                 package:graphics                  R Documentation

Generic X-Y Plotting

Description:

     Generic function for plotting of R objects.  For more details
     about the graphical parameter arguments, see ‘par’.

     For simple scatter plots, ‘plot.default’ will be used.  However,
     there are ‘plot’ methods for many R objects, including
     ‘function’s, ‘data.frame’s, ‘density’ objects, etc.  Use
     ‘methods(plot)’ and the documentation for these.

Usage:

     plot(x, y, ...)
     
Arguments:

       x: the coordinates of points in the plot. Alternatively, a
          single plotting structure, function or _any R object with a
          ‘plot’ method_ can be provided.

       y: the y coordinates of points in the plot, _optional_ if ‘x’ is
          an appropriate structure.

     ...: Arguments to be passed to methods, such as graphical
          parameters (see ‘par’).  Many methods will accept the
          following arguments:

          ‘type’ what type of plot should be drawn.  Possible types are

                ? ‘"p"’ for *p*oints,

                ? ‘"l"’ for *l*ines,

                ? ‘"b"’ for *b*oth,

                ? ‘"c"’ for the lines part alone of ‘"b"’,

                ? ‘"o"’ for both ‘*o*verplotted’,

                ? ‘"h"’ for ‘*h*istogram’ like (or ‘high-density’)
                  vertical lines,

                ? ‘"s"’ for stair *s*teps,

                ? ‘"S"’ for other *s*teps, see ‘Details’ below,

                ? ‘"n"’ for no plotting.

              All other ‘type’s give a warning or an error; using,
              e.g., ‘type = "punkte"’ being equivalent to ‘type = "p"’
              for S compatibility.  Note that some methods, e.g.
              ‘plot.factor’, do not accept this.

          ‘main’ an overall title for the plot: see ‘title’.

          ‘sub’ a sub title for the plot: see ‘title’.

          ‘xlab’ a title for the x axis: see ‘title’.

          ‘ylab’ a title for the y axis: see ‘title’.

          ‘asp’ the y/x aspect ratio, see ‘plot.window’.

Details:

     The two step types differ in their x-y preference: Going from
     (x1,y1) to (x2,y2) with x1 < x2, ‘type = "s"’ moves first
     horizontal, then vertical, whereas ‘type = "S"’ moves the other
     way around.

See Also:

     ‘plot.default’, ‘plot.formula’ and other methods; ‘points’,
     ‘lines’, ‘par’.

     For X-Y-Z plotting see ‘contour’, ‘persp’ and ‘image’.

Examples:

     require(stats)
     plot(cars)
     lines(lowess(cars))
     
     plot(sin, -pi, 2*pi) # see ?plot.function
     
     ## Discrete Distribution Plot:
     plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
          main = "rpois(100, lambda = 5)")
     
     ## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:
     plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")
     points(x, cex = .5, col = "dark red")

> methods(plot)
 [1] plot.acf*           plot.data.frame*    plot.decomposed.ts*
 [4] plot.default        plot.dendrogram*    plot.density*      
 [7] plot.ecdf           plot.factor*        plot.formula*      
[10] plot.function       plot.hclust*        plot.histogram*    
[13] plot.HoltWinters*   plot.isoreg*        plot.lm*           
[16] plot.medpolish*     plot.mlm*           plot.ppr*          
[19] plot.prcomp*        plot.princomp*      plot.profile.nls*  
[22] plot.spec*          plot.stepfun        plot.stl*          
[25] plot.table*         plot.ts             plot.tskernel*     
[28] plot.TukeyHSD*     

   Non-visible functions are asterisked