geom_line
Given a data frame with a numerical variable x
and a numerical variable y
representing the value for each observation it is possible to create a line chart in ggplot with geom_line
as follows.
Adding points
If you add geom_point
to the plot a point will be added for each observation.
Line as an arrow
If your data represents a trend or a path you can end the line with an arrow, passing the arrow
funtion to the arrow
argument. Type ?arrow
for additional arguments.
geom_step
First horizontal
An alternative geom is geom_step
, which will create a stairstep plot where the first line is horizontal by default.
First vertical
To set the first line vertical pass the string "vh"
to the direction
argument of the geom.
Step half-way between values
Other possible option is setting "mid"
, for step half-way between adjacent X-axis values.
The color
, lwd
and linetype
arguments can be used to modify the color, width and the type of the lines, respectively.
Note that the line type can be set with numbers (0 to 6), with texts or with string patterns.
See also