View source: R/geom_categorical_model.R
geom_categorical_model | R Documentation |
geom_categorical_model()
fits a regression model using the categorical
x axis as the explanatory variable, and visualizes the model's fitted values
as piecewise horizontal line segments. Confidence interval bands can be
included in the visualization of the model. Like geom_parallel_slopes()
,
this function has the same nature as geom_smooth()
from
the ggplot2 package, but provides functionality that geom_smooth()
currently doesn't have. When using a categorical predictor variable,
the intercept corresponds to the mean for the baseline group, while
coefficients for the non-baseline groups are offsets from this baseline.
Thus in the visualization the baseline for comparison group's median is
marked with a solid line, whereas all offset groups' medians are marked with dashed lines.
geom_categorical_model( mapping = NULL, data = NULL, position = "identity", ..., se = TRUE, level = 0.95, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
se |
Display confidence interval around model lines? |
level |
Level of confidence interval to use (0.95 by default). |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom_parallel_slopes()
library(dplyr) library(ggplot2) p <- ggplot(mpg, aes(x = drv, y = hwy)) + geom_point() + geom_categorical_model() p # In the above visualization, the solid line corresponds to the mean of 19.2 # for the baseline group "4", whereas the dashed lines correspond to the # means of 28.19 and 21.02 for the non-baseline groups "f" and "r" respectively. # In the corresponding regression table however the coefficients for "f" and "r" # are presented as offsets from the mean for "4": model <- lm(hwy ~ drv, data = mpg) get_regression_table(model) # You can use different colors for each categorical level p %+% aes(color = drv) # But mapping the color aesthetic doesn't change the model that is fit p %+% aes(color = class)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.