is a histogramCharts most commonly used to represent continuous data. This is a bar graph that shows the frequency of measurements grouped by a certain interval and calculates the number of observations in each interval. Additionally, pitch is determined by the ratio of frequency to interval width. In this tutorial, we'll cover how to create a histogram using the R programming language.
- How to make a histogram in R? R function
- 2 Change the color of the histogram
- 3 gaps in the R histogram
- 4 Histogram with two variables in R
- 5 Adding a normal curve to the histogram
- 6 Adding density lines to the histogram
- Combine: histogram and frame in R
- Using ggplot2 to plot histograms in R
- 9 Draw a histogram
How to make a histogram in R? R function
If you're reading this, you're wonderingHow to draw a histogram in R. So, to explain the steps of building a histogram in R, we'll use the following data that represents the distance in yards after a golf ball is hit.
Distance <-c(241.1, 284.4, 220.2, 272.4, 271.1, 268.3, 291.6, 241.6, 286.1, 285.9, 259.6, 299.6, 253.1, 239.6, 277.8, 263.8, 267. 2, 272.6, 283.4, 2 34.5, 260.4, 264.2, 295.1, 276, 4, 263.1, 251.4, 264.0, 269.2, 281.0, 283.2)
You can plot a histogram in R using the following commandhistory
Function.by default, the function will createfrequency histogram.
hist(distance, main = "frequency histogram") # frequency
![Histograms in R ⚡ [create, adjust, throw, add curves...] (1) Histograms in R ⚡ [create, adjust, throw, add curves...] (1)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram.png)
However, if you set the parameterprobability
comehe says
You will getDensity histogram.
hist(distance, prob = TRUE, main = "density histogram") # gustoća
![Histograms in R ⚡ [create, adjust, throw, add curves...] (2) Histograms in R ⚡ [create, adjust, throw, add curves...] (2)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/density-histogram.png)
You can also add a grid to the histogram using the commandneto
The function is as follows:
hist(udaljenost, prob=TRUE) grid(nx=NA, ny=NULL, lty=2, col="sivo", lwd=1) hist(distance, prob=TRUE, add=TRUE, col="white")
![Histograms in R ⚡ [create, adjust, throw, add curves...] (3) Histograms in R ⚡ [create, adjust, throw, add curves...] (3)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-grid.png)
Note that you need to plot the histogram twice in order for the grid to appear below the main graph.
As of R 4.0.0, the histogram defaults to gray instead of white.
Change the color of the histogram
Now you know how to make a histogram in RYou can also customize it. So if you want to change the color of the container you can set itdepression
The parameter is your favorite color. Like other plots,Many functions can be customizedChart content such as title, axes, font size...
hist(distance, col = "light blue")
![Histograms in R ⚡ [create, adjust, throw, add curves...] (4) Histograms in R ⚡ [create, adjust, throw, add curves...] (4)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-color.png)
Gaps in the R histogram
is a histogramUseful for representing the underlying distribution of dataIf the number of containers is correctly selected. but thatChoosing the number of bins (or bin width) can be tricky:
- Several bins group too many observations.
- With multiple bins, there will be several observations in each bin, which increases the variability of the resulting graph.
HaveSeveral rules for determining the number of cuvettes. w R.Sturges's method is standardly used. If you want to change the number of containers, you can set a parameterrest
to the desired number.
par(mfrow = c(1, 3)) hist(distance, gaps = 2, main = "several containers") hist(distance, gaps = 50, main = "too many containers") hist(distance, main = "Sturges " method") par(mfrow = c(1, 1))
![Histograms in R ⚡ [create, adjust, throw, add curves...] (5) Histograms in R ⚡ [create, adjust, throw, add curves...] (5)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-breaks.png)
You can also choose the width of the histogram bin using the plugin method implemented in Wand (1995)core smoothing
The library looks like this:
# Metoda dodatka# install.packages("KernSmooth")library(KernSmooth)bin_width <- dpih(distance)nbins <- seq(min(distance) - bin_width, max(distance) + bin_width, by = bin_width)hist(distance, breaks = nbins, main = "plugins")
![Histograms in R ⚡ [create, adjust, throw, add curves...] (6) Histograms in R ⚡ [create, adjust, throw, add curves...] (6)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/plug-in-histogram.png)
Histogram with two variables in R
Setting parametersAdd to
comehe says
Allows overlaying of histograms on other charts. For example, you can createR histogram by groupswith the following code block:
set.seed(1)x <- rnorm(1000) # first group <- rnorm(1000, 1) # second grouphist(x, main = "two variables")hist(y, add = TRUE, col = rgb(1 ) ) , 0, 0, 0.5))
![Histograms in R ⚡ [create, adjust, throw, add curves...] (7) Histograms in R ⚡ [create, adjust, throw, add curves...] (7)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-two-variables.png)
TenRGB
The function sets the color in the RGB channel,A
The parameter sets the transparency. In fact, when connecting drawings, it's best to set the colors to transparent so you can see the drawings behind them.
Add a normal curve to the histogram
To draw normal curves on the histogram you can usenorma
andWire
The function is as follows:
hist(distance, probability = TRUE, main = "normal curve histogram") x <- seq(min(distance), max(distance), length = 40) f <- dnorm(x, mean = mean(distance), sd = sd(distance)) row(x, f, column = "red", lwd = 2)
![Histograms in R ⚡ [create, adjust, throw, add curves...] (8) Histograms in R ⚡ [create, adjust, throw, add curves...] (8)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-normal-curve.png)
Add density lines to the histogram
To add a density curve to the top of the histogram you can useWire
curve drawing functions anddensity
used to calculate the underlying instrumentnon-parametric (kernel) density distribution.
hist(distance, frequency = FALSE, main = "density curve") lines(density(distance), lwd = 2, col = "red")
![Histograms in R ⚡ [create, adjust, throw, add curves...] (9) Histograms in R ⚡ [create, adjust, throw, add curves...] (9)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/density-curve.png)
Bandwidth selection for fitting nonparametric densities is an area of intense research.Also note that by defaultdensity
The function uses a Gaussian kernel. For more information, call? density
.
We will add the previous code to the functionAutomatic generation of histograms with normals and density lines:
histDenNorm <- function (x, main = "") { hist(x, prob = TRUE, main = main) # histogram line(density(x), col = "blue", lwd = 2) # density x2 < - seq (min(x), max(x), duljina = 40) f <- dnorm(x2,prosjek(x),sd(x))linije(x2,f,col = "crveno",lwd = 2) # normalna legenda("topright", c("histogram", "density", "normal"), box.lty = 0, lty = 1, col = c("black", "blue", "red") , lwd = c(1,2,2))}
Now you can test the behavior of the function on sample data.
set.seed(1)# Normal data x <- rnorm(n = 5000, mean = 110, sd = 5) # Exponential data y <- rex(n = 3000, rate = 1) par(mfcol = c(1 , 2) )histDenNorm(x, main = "Histogram X")histDenNorm(y, main = "Histogram Y")par(mfcol = c(1, 1))
![Histograms in R ⚡ [create, adjust, throw, add curves...] (10) Histograms in R ⚡ [create, adjust, throw, add curves...] (10)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/hist-density-normal-function.png)
Combination: Histograms and Boxplots in R
You can add a border bubble on top of the histogrampair(new=true)
between plots.
hist(distance, probability = TRUE, ylab = "", main = "", col = rgb(1, 0, 0, alpha = 0.5), axes = FALSE) axis(1) # add horizontal axis (new = TRUE) boxplot(distance, level=TRUE, axis=FALSE, lwd=2, column=rgb(0, 0, 0, alpha=0.2))
![Histograms in R ⚡ [create, adjust, throw, add curves...] (11) Histograms in R ⚡ [create, adjust, throw, add curves...] (11)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-boxplot.png)
You can also add a normal or density curve to the previous plot.
Histogram w R z ggplot2
To create a histogramggplot2
packages you need to useCan
+geometric histogram
function and transmit data asdata frame
. from the insideAES
Specify the data frame variable name parameter.
# install.packages("ggplot2")library(ggplot2)ggplot(data.frame(distance), aes(x = distance)) + geom_histogram(color = "gray", fill = "white")
![Histograms in R ⚡ [create, adjust, throw, add curves...] (12) Histograms in R ⚡ [create, adjust, throw, add curves...] (12)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-ggplot2.png)
The chart returns a warning message that the histogram was calculated using 30 bins. This is because by defaultCan
without using the Sturgis method.
now we wantCalculate the number of containers using the Sturges methodHowhistory
the function executes and sets itrest
discussion. Note that you can also setboxing room
Argue if you want.
# Izračunajte prijelome, npr. funkcija hist() nbreaks <- Pretty(range(distance), n = nclass.Sturges(distance), min.n = 1)ggplot(data.frame(distance), aes(x = distance) ) + geom_histogram(gaps = ngaps, color = "gray", fill = "white")
![Histograms in R ⚡ [create, adjust, throw, add curves...] (13) Histograms in R ⚡ [create, adjust, throw, add curves...] (13)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-ggplot2-sturges.png)
As you can see, this is equal to the first histogram.
existggplot2
You can also add a density curve withgeometric density
Function. Also, if you want to fill the area under the curve, set the parameterpuna
your favorite color andA
Color transparency. Note that you have to install a new oneAES
interior -geometric histogram
as follows:
ggplot(data.frame(distance), aes(x=distance)) + geom_histogram(aes(y=..density..), breaks=nbreaks, color="gray", fill="white") + geom_density(fill ="crna", alfa=0,2)
![Histograms in R ⚡ [create, adjust, throw, add curves...] (14) Histograms in R ⚡ [create, adjust, throw, add curves...] (14)](https://i0.wp.com/r-coder.com/wp-content/uploads/2020/06/histogram-ggplot2-density.png)
graph histogram
Another way to create a histogram is to useConspiracy
a package (R adaptation of the JavaScriptplotly library) that creates plots in an interactive format. For example, you can run the following command:
# install.packages("plotly")library(plotly)# frequency histogramfig <-plot_ly(x = distance, type = "histogram")fig# density histogramfig <-plot_ly(x = distance, type = "histogram", histnorm = "probability")