is a histogram**Charts 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 wondering**How 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 command`history`

Function.**by default**, the function will create**frequency histogram**.

`hist(distance, main = "frequency histogram") # frequency`

However, if you set the parameter`probability`

come`he says`

You will get**Density histogram**.

`hist(distance, prob = TRUE, main = "density histogram") # gustoća`

You can also add a grid to the histogram using the command`neto`

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")`

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 R**You can also customize it**. So if you want to change the color of the container you can set it`depression`

The parameter is your favorite color. Like other plots,**Many functions can be customized**Chart content such as title, axes, font size...

`hist(distance, col = "light blue")`

## Gaps in the R histogram

is a histogram**Useful for representing the underlying distribution of data**If the number of containers is correctly selected. but that**Choosing 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.

Have**Several 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 parameter`rest`

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))`

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")`

## Histogram with two variables in R

Setting parameters`Add to`

come`he says`

Allows overlaying of histograms on other charts. For example, you can create**R histogram by groups**with 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))`

Ten`RGB`

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 use`norma`

and`Wire`

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)`

## Add density lines to the histogram

To add a density curve to the top of the histogram you can use`Wire`

curve drawing functions and`density`

used to calculate the underlying instrument**non-parametric (kernel) density distribution**.

`hist(distance, frequency = FALSE, main = "density curve") lines(density(distance), lwd = 2, col = "red")`

Bandwidth selection for fitting nonparametric densities is an area of intense research.**Also note that by default densityThe function uses a Gaussian kernel**. For more information, call

`? density`

.We will add the previous code to the function**Automatic 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))`

## Combination: Histograms and Boxplots in R

You can add a border bubble on top of the histogram`pair(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))`

You can also add a normal or density curve to the previous plot.

## Histogram w R z ggplot2

To create a histogram`ggplot2`

packages you need to use`Can`

+`geometric histogram`

function and transmit data as`data frame`

. from the inside`AES`

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")`

The chart returns a warning message that the histogram was calculated using 30 bins. This is because by default`Can`

**without using the Sturgis method**.

now we want**Calculate the number of containers using the Sturges method**How`history`

the function executes and sets it`rest`

discussion. Note that you can also set`boxing 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")`

As you can see, this is equal to the first histogram.

exist`ggplot2`

You can also add a density curve with`geometric density`

Function. Also, if you want to fill the area under the curve, set the parameter`puna`

your favorite color and`A`

Color transparency. Note that you have to install a new one`AES`

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)`

## graph histogram

Another way to create a histogram is to use`Conspiracy`

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")`