View source: R/stat_central_tendency.R
stat_central_tendency | R Documentation |
Add central tendency measures (mean, median, mode) to density and histogram plots created using ggplots.
Note that, normally, the mode is used for categorical data where we wish to know which is the most common category. Therefore, we can have have two or more values that share the highest frequency. This might be problematic for continuous variable.
For continuous variable, we can consider using mean or median as the measures of the central tendency.
stat_central_tendency( mapping = NULL, data = NULL, geom = c("line", "point"), position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, type = c("mean", "median", "mode"), ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data, either as a
|
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values. |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
type |
the type of central tendency measure to be used. Possible values
include: |
... |
other arguments to pass to |
ggdensity
# Simple density plot data("mtcars") ggdensity(mtcars, x = "mpg", fill = "red") + scale_x_continuous(limits = c(-1, 50)) + stat_central_tendency(type = "mean", linetype = "dashed") # Color by groups data(iris) ggdensity(iris, "Sepal.Length", color = "Species") + stat_central_tendency(aes(color = Species), type = "median", linetype = 2) # Use geom = "point" for central tendency data(iris) ggdensity(iris, "Sepal.Length", color = "Species") + stat_central_tendency( aes(color = Species), type = "median", geom = "point", size = 4 ) # Facet ggdensity(iris, "Sepal.Length", facet.by = "Species") + stat_central_tendency(type = "mean", color = "red", linetype = 2) + stat_central_tendency(type = "median", color = "blue", linetype = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.