Line charts are sometimes useful in order to view the pattern of a single variable against its index. Also, in some cases, bivariate connected lines are also useful. In this recipe, we will draw a line chart using ggplot2
with geom_line
.
Once again, we recall ggplotdata
for this plot. In this case, we will use the two numeric variables, which are disA
and disD
.
To produce a connected line of a single numeric variable against its observation index, we will use the following code:
# connected line ggplot(data=ggplotdata,aes(x=1:nrow(ggplotdata),y=disA))+geom_line()
The line chart produced is shown in the following figure:
The single variable's connected line looks like a time series plot, but the difference is that this plot does not have the time axis. The x
argument within aes()
specifies the values of the x-axis. In this case, the value of x-axis is just the sequence number starting from 1
to the number of observations in the dataset. So, the aes()
function internally produces coordinates in the x-y plane with x
with the index value and y
, takes the value from the disA
variable, and then connects the points in order to produce the line.
To display the data points along with a connected line, we can use the following code:
# connected line with points ggplot(data=ggplotdata,aes(x=1:nrow(ggplotdata),y=disA))+geom_line()+geom_point()
So far, we have produced a connected line of a single numeric variable. Now, in order to produce a different line with a different color—where color is based on other factor variables—we can use the col=
argument within aes()
. Here is how we can do this:
# connected line with points separated by factor variable ggplot(data=ggplotdata,aes(x=1:nrow(ggplotdata),y=disA,col=sex))+geom_line()+geom_point()
The plot will look like what is shown in the following figure:
To produce a time series plot, the only thing that needs to be taken care of is that the x-axis should be a date
variable. Here is a test case of a time series plot with the same data, but now, the x-axis is represented as a date:
ggplot(data=ggplotdata,aes(x=as.Date(1:nrow(ggplotdata),origin = "2013-12-31"),y=disA,col=sex))+geom_line()+geom_point()
In this code snippet, the as.Date(1:nrow(ggplotdata),origin = "2013-12-31")
part creates a sequence of dates starting with December 31, 2013 as the origin. So the first row of the data frame represents January 1, 2014, the second row represents January 2, 2014, and so on. The plot will be as follows:
18.226.164.53