---
title: "Line chart with R and ggplot2"
descriptionMeta: "This post is a step by step introduction to line chart with R and ggplot2. It provides several reproducible examples with explanation and R code."
descriptionTop: "This post is a step by step introduction to [line chart](line-plot.html) with R and [ggplot2](ggplot2-package.html). It provides several reproducible examples with explanation and R code."
sectionText: "Line chart Section"
sectionLink: "line-plot.html"
DataToVizText: "About line chart"
DataToVizLink: "data-to-viz.com/graph/line.html"
url: "line-chart-ggplot2"
output:
  html_document:
      self_contained: false    
      mathjax: default
      lib_dir: libs
      template: template_rgg.html
      css: style.css
      toc: TRUE
      toc_float: TRUE
      toc_depth: 2
      df_print: "paged"
---


```{r global options, include = FALSE}
knitr::opts_chunk$set( warning=FALSE, message=FALSE)
```

<div class="container">




# Basic line chart with `ggplot2` and `geom_line()`
***

<div class = "row">

<div class = "col-md-6  col-sm-12 align-self-center">

A [line chart](line-plot.html) or [line graph](line-plot.html) displays the evolution of one or several numeric variables. Data points are usually connected by straight line segments. You read an extensive definition [here](https://www.data-to-viz.com/graph/line.html).

The input data frame requires at least 2 columns: 

- An *ordered* numeric variable for the X axis
- Another numeric variable for the Y axis

Once the data is read by ggplot2 and those 2 variables are specified in the `x` and `y` arguments of the `aes()`, just call the `geom_line()` function.

</div>


<div class = "col-md-6  col-sm-12">

```{r thecode, echo=FALSE, out.width = "100%", fig.height=7}
# Libraries
library(ggplot2)

# create data
xValue <- 1:10
yValue <- cumsum(rnorm(10))
data <- data.frame(xValue,yValue)

# Plot
ggplot(data, aes(x=xValue, y=yValue)) +
  geom_line()
```


</div>
</div>



```{r thecode, eval=FALSE}
```










# Customize the line chart
***

<div class = "row">

<div class = "col-md-6  col-sm-12 align-self-center">

Several options are available to customize the line chart appearance:

- Add a title with `ggtitle()`. 
- Change line style with arguments like `shape`, `size`, `color` and more.
- Custom the general theme with the `theme_ipsum()` function of the `hrbrthemes` package.

More generally, visit the [ggplot2 section] for more ggplot2 related stuff.

</div>


<div class = "col-md-6  col-sm-12">

```{r thecode2, echo=FALSE, out.width = "100%", fig.height=7}
# Libraries
library(ggplot2)
library(hrbrthemes)

# create data
xValue <- 1:10
yValue <- cumsum(rnorm(10))
data <- data.frame(xValue,yValue)

# Plot
ggplot(data, aes(x=xValue, y=yValue)) +
  geom_line( color="#69b3a2", size=2, alpha=0.9, linetype=2) +
  theme_ipsum() +
  ggtitle("Evolution of something")
```


</div>
</div>



```{r thecode2, eval=FALSE}
```













<!-- Close container -->
</div>




```{r, echo=FALSE}
htmltools::includeHTML("htmlChunkRelatedCorrelation.html")
```
