• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Creating a Line Chart in R

Data Science

This lesson is part 15 of 29 in the course Data Visualization with R

In R, we can create a line plot using the same plot() function by adding a plot type of “l”. This will plot the (x,y) paired observations and connect them with lines.

plot(x,y,type="l")

Let’s generate our own data for this lesson. We will use the rnorm() function to generate a set of 100 random numbers that follow a normal distribution. These random numbers will be plotted on y-axis. x-axis will be a sequence of numbers from 1 to 100.

> x <- c(1:100)
> rand_data <- rnorm(100, mean = 0)
> data <- data.frame(x, rand_data)
> plot(data$x, data$rand_data, type = "l", xlab="x", ylab = "Data")

The resulting line plot is displayed below:

Line Chart

Line Type

You can change the appearance of the line by using the lty parameter.

  • lty=”solid” or lty=1 (default)
  • lty=”dashed” or lty=2
  • lty=”dotted” or lty=3
  • lty=”dotdash” or lty=4
  • lty=”longdash” or lty=5
  • lty=”twodash” or lty=6
  • lty=”blank” or lty=0

Line Width

The line width can be changed using the lwd parameter. The default width is 1. So, you can plot a thicker line using a higher number.

Line Color

The line color can be changed using the col parameter.

Below we replot the line plot with a dotted line, thickness of 2, and in orange color.

> plot(data$x, data$rand_data, type = "l", xlab="x", ylab = "Data",lty=3,lwd=2,col="orange")
Line Chart

Exercise

We know that numerical data generally conforms to a normal probability distribution characterized by a bell curve. In this exercise you are asked to create a bell curve using the normal data.

  • Use the rnorm() function to generate random numbers that follow a normal distribution (mean 0 and standard deviation of 1). Generate upto 5000 numbers. Store these numbers in a variable x.
  • Plot the density of these random numbers using plot(density(x)) This will plot the bell curve.
  • Try different values for mean and standard deviation and observe how the graph changes.

Note: You can also compute the Gaussian density using the dnorm() function, for example dnorm(x, mean = 0, sd = 1).

Previous Lesson

‹ Creating a Bar Chart in R

Next Lesson

Plotting Multiple Datasets on One Chart in R ›

Join Our Facebook Group - Finance, Risk and Data Science

Posts You May Like

How to Improve your Financial Health

CFA® Exam Overview and Guidelines (Updated for 2021)

Changing Themes (Look and Feel) in ggplot2 in R

Coordinates in ggplot2 in R

Facets for ggplot2 Charts in R (Faceting Layer)

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • Overview of Data Visualization
  • When to Use Bar Chart, Column Chart, and Area Chart
  • What is Line Chart and When to Use It
  • What are Pie Chart and Donut Chart and When to Use Them
  • How to Read Scatter Chart and Bubble Chart
  • What is a Box Plot and How to Read It
  • Understanding Japanese Candlestick Charts and OHLC Charts
  • Understanding Treemap, Heatmap and Other Map Charts
  • Visualization in Data Science
  • Graphic Systems in R
  • Accessing Built-in Datasets in R
  • How to Create a Scatter Plot in R
  • Create a Scatter Plot in R with Multiple Groups
  • Creating a Bar Chart in R
  • Creating a Line Chart in R
  • Plotting Multiple Datasets on One Chart in R
  • Adding Details and Features to R Plots
  • Introduction to ggplot2
  • Grammar of Graphics in ggplot
  • Data Import and Basic Manipulation in R – German Credit Dataset
  • Create ggplot Graph with German Credit Data in R
  • Splitting Plots with Facets in ggplots
  • ggplot2 – Chart Aesthetics and Position Adjustments in R
  • Creating a Line Chart in ggplot 2 in R
  • Add a Statistical Layer on Line Chart in ggplot2
  • stat_summary for Statistical Summary in ggplot2 R
  • Facets for ggplot2 Charts in R (Faceting Layer)
  • Coordinates in ggplot2 in R
  • Changing Themes (Look and Feel) in ggplot2 in R

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us