Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R plot() Graphical Types Explained

R plot() Graphical Types Explained

R Language Examples R Language Examples

The R plot() function can create many types of plots through the type parameter.

Different type options suit different data visualization needs.

Common values of the type parameter in plot():

type value Description Applicable scenarios
"p" points (default) Scatter chart
"l" Polyline Line chart
"b" points and lines Show data point positions and trends
"o" overlaid points and lines Time series
"h" Vertical bar spectrum plot
"s" step lines step data
"n" do not plot only set up the coordinate system

Example

Generate one month of temperature data

temp

<-

c

(

5

,

7

,

10

,

14

,

18

,

22

,

25

,

28

,

26

,

20

,

15

,

10

,

         

8

,

6

,

9

,

13

,

17

,

21

,

24

,

27

,

25

,

19

,

14

,

9

,

         

7

,

5

,

8

,

12

,

16

,

20

)

days

<-

1

:

30

Overlay multiple types on the same plot

plot

(

days, temp, type

=

"n"

,  

Set up the coordinate system first without plotting

     main

=

"This Month's Temperature Changes"

,

     xlab

=

"date"

, ylab

=

"Temperature (C)"

,

     ylim

=

c

(

0

,

30

)

)

Add vertical lines (type = "h")

lines

(

days, temp, type

=

"h"

,

     

col

=

"lightblue"

, lwd

=

2

)

Add points and connecting lines

lines

(

days, temp, type

=

"o"

,

     

col

=

"coral"

, lwd

=

2

, pch

=

19

, cex

=

0.6

)

Running the above code displays a temperature change plot with overlaid vertical lines and connecting lines.

R Language Examples R Language Examples