Distribution in R should be regarded as a relatively important content, and by drawing to show the distribution of data, you can more intuitively let us understand the distribution of data
Histogram
Geom_histogram (mapping = NULL, data = NULL, stat = "bin", Position = "stack", ..., binwidth = null, bins = NULL, NA.RM = F Alse, show.legend = NA, Inherit.aes = TRUE)
Density map
geom_density (mapping = NULL, data = NULL, stat = "density", Position = "identity", ..., na.rm = FALSE, show.legend = na, I Nherit.aes = TRUE)
The histogram shows the distribution of the data through a huge stack height, and the density graph shows the distribution of the data through linear bending.
Let's look at a concrete example.
Library (Ggplot2) P<-ggplot (Diamonds, AES (carat)) P+geom_histogram (bins=30)
Let's look at the effect of using a density map
P+geom_density ()
The density graph looks smoother than the histogram, and continues to look at the stack case comparison
P<-ggplot (Diamonds, AES (Carat,fill=cut)) P+geom_histogram (BINS=50)
P+geom_density (position= "stack", adjust=1/1.2)
Here in the future to avoid the various categorical variable data occlusion, the value of the parameter position set to stack, you can show a layer of the effect of stacking up
In addition, the parameter adjust can be similar to the parameter bins in the histogram for adjusting the width of the strip or rectangle
P+geom_density (position= "Fill", adjust=1/1.2)
Setting the position to fill gives you a more intuitive look at the density of each classification, but the axes ' indication effect is blurred.
Ggplot2 Geom Related Settings-distribution map