- R Graphs Cookbook Second Edition
- Jaynal Abedin Hrishi V. Mittal
- 300字
- 2021-08-05 17:30:28
Creating histograms and density plots
In this recipe, you will learn how to make histograms and density plots, which are useful to look at the distribution of values in a dataset.
How to do it...
The simplest way to demonstrate the use of a histogram is to show a normal distribution:
hist(rnorm(1000))

Another example of a histogram is one that shows a skewed distribution:
hist(islands)

How it works...
The hist()
function is also a function of R's base graphics library. It takes only one compulsory argument, that is, the variable whose distribution of values we wish to visualize.
In the first example, we passed the rnorm()
function as the variable. The rnorm(1000)
function generates a vector of 1000 random numbers with a normal distribution. As you can see in the histogram, it's a bell-shaped curve.
In the second example, we passed the built-in islands dataset (which gives the areas of the world's major landmasses) as the argument to hist()
. As you can see from these histograms, islands has a distribution skewed heavily towards the lower value range of 0 to 2000 square miles.
There's more...
As you might have noticed in the preceding examples, the default setting for histograms is to display the frequency or number of occurrences of values in a particular range on the y axis. We can also display probabilities instead of frequencies by setting the prob
(for probability) argument to TRUE
or the freq
(for frequency) argument to FALSE
.
Now, let's make a density plot for the same function, rnorm()
. To do so, we need to use the density()
function and pass it as our first argument to plot()
as follows:
plot(density(rnorm(1000)))

See also
We cover more details such as setting the breaks, density, formatting of bars, and other advanced recipes in Chapter 7, Creating Histograms.
- 零點起飛學(xué)Xilinx FPG
- 用“芯”探核:龍芯派開發(fā)實戰(zhàn)
- 計算機組裝與系統(tǒng)配置
- Mastering Delphi Programming:A Complete Reference Guide
- Linux運維之道(第2版)
- The Applied AI and Natural Language Processing Workshop
- 電腦組裝、維護、維修全能一本通(全彩版)
- 嵌入式系統(tǒng)設(shè)計教程
- Arduino BLINK Blueprints
- Blender Quick Start Guide
- RISC-V處理器與片上系統(tǒng)設(shè)計:基于FPGA與云平臺的實驗教程
- Neural Network Programming with Java(Second Edition)
- 無蘋果不生活:OS X Mountain Lion 隨身寶典
- Blender Game Engine:Beginner's Guide
- Wireframing Essentials