How to create a step histogram using ggplot2 in R?

R ProgrammingServer Side ProgrammingProgramming

To create a step histogram using ggplot2, we can use geom="step" argument inside stat_bin function. For example, if we have a data frame that contains a single column then the step histogram can be created using the command − ggplot(df,aes(x))+stat_bin(geom="step",bins=30)

Example

 Live Demo

Consider the below data frame −

set.seed(14)
x<−rnorm(20)
df<−data.frame(x)
df

Output

x
1 −0.66184983
2 1.71895416
3 2.12166699
4 1.49715368
5 −0.03614058
6 1.23194518
7 −0.06488077
8 1.06899373
9 −0.37696531
10 1.04318309
11 −0.38282188
12 0.29942160
13 0.67423976
14 −0.29281632
15 0.48805336
16 0.88280182
17 1.86274898
18 1.61172529
19 0.13547954
20 1.08808601

Loading ggplot2 package and creating the step histogram −

Example

library(ggplot2)
ggplot(df,aes(x))+stat_bin(geom="step",bins=30)

Output

raja
Published on 08-Feb-2021 11:33:57
Advertisements