How to create multiple lines chart with unique line types in R?


Line types can be very different and they are helpful to differentiate among different variables. Mostly, it is used to plot trend data so that the trend for different variables can be visualized with a unique line. In R, we can use matplot function to create such type of multiple lines chart.

Example1

 Live Demo

M1<−matrix(rpois(50,10),ncol=5)
M1

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 6 14 13 4 9
[2,] 9 6 12 19 9
[3,] 11 3 14 17 8
[4,] 12 9 11 9 11
[5,] 8 15 17 8 5
[6,] 10 5 9 16 11
[7,] 15 7 9 11 4
[8,] 6 17 12 10 9
[9,] 14 14 8 5 15
[10,] 9 7 10 7 12

Example

matplot(M1,type="b",lty=1:5,col=1:5)

Output

Example2

 Live Demo

M2<−matrix(rnorm(50,5,1.5),ncol=5)
M2

Output

      [,1]       [,2]    [,3]    [,4]    [,5]
[1,] 5.933481 6.197999 5.749572 6.090207 5.389253
[2,] 3.564041 4.838219 4.220123 7.435189 4.425479
[3,] 2.068470 4.243865 7.337653 3.919899 4.883489
[4,] 1.862330 4.509389 6.439966 3.269006 6.712497
[5,] 4.851103 4.724440 6.567998 5.582377 5.809366
[6,] 5.216973 6.082616 4.621395 8.885286 6.102352
[7,] 6.289226 2.688063 2.518614 3.445730 8.553573
[8,] 4.485198 6.231307 6.967294 5.020253 2.025629
[9,] 7.358154 4.143956 2.578751 3.257326 7.417970
[10,] 3.905110 5.617001 6.729087 5.586298 7.172660

Example

matplot(M2,type="l",lty=1:5,col=1:5)

Output

Example3

M3<−matrix(rnorm(50,100,5),ncol=5)
M3

Output

      [,1]       [,2]       [,3]    [,4]    [,5]
[1,] 99.68460 99.93048 105.84288 105.19923 106.66900
[2,] 103.77704 102.79751 100.42100 94.37580 98.95201
[3,] 101.25451 97.85347 101.07332 102.74360 104.71752
[4,] 97.38155 93.69017 94.24398 92.20655 99.74503
[5,] 101.46322 89.13843 104.72530 99.79385 103.04346
[6,] 92.30987 103.13514 93.81382 104.57501 97.40207
[7,] 102.47811 102.96409 97.77797 99.79134 92.69327
[8,] 100.61359 109.21111 96.53750 103.63365 92.80989
[9,] 86.91855 100.66574 99.53231 100.52899 97.96549
[10,] 101.40606 103.10011 90.32861 93.61853 105.35961

Example

matplot(M2,type="l",lty=1:5,col=1:5)

Output

Updated on: 07-Nov-2020

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements