Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
R Data Visualization Cookbook

You're reading from   R Data Visualization Cookbook Over 80 recipes to analyze data and create stunning visualizations with R

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783989508
Length 236 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. A Simple Guide to R FREE CHAPTER 2. Basic and Interactive Plots 3. Heat Maps and Dendrograms 4. Maps 5. The Pie Chart and Its Alternatives 6. Adding the Third Dimension 7. Data in Higher Dimensions 8. Visualizing Continuous Data 9. Visualizing Text and XKCD-style Plots 10. Creating Applications in R Index

Generating a density plot


The density plot uses the kernel density estimation to generate the distribution. In this recipe, we will utilize the density() function to generate a plot. The density plots can be used to study the underlying distribution of the data.

Getting ready

We will use the quantmod package to download the stock prices for Microsoft and also calculate monthly returns:

install.packages("quantmod")
library(quantmod)

How to do it…

We will download the data in R using the getSymbols() function. Once we have the data, we can calculate the monthly returns using the monthlyReturns() function:

prices = c("MSFT")
getSymbols(prices)
msft_m = monthlyReturn(MSFT)

In order to generate a density plot, we will first estimate the kernel density using the density() function in R. Please note that we have plotted a density plot over the histogram and, hence, we need to use the lines() function. The lines() function will allow us to plot a density plot over the histogram:

msft_d = density(msft_m...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image