Histogram ggplot()
; Boxplot ggplot()
November 8, 2024
ggplot()
- Histogramgeom_histogram()
Histograms are used to visualize the distribution of a numeric variable.
Histograms divide data into bins and count the number of observations in each bin.
geom_histogram()
geom_histogram()
geom_histogram()
creates a histogram.
x
aesthetic to the variable.geom_histogram()
with bins
bins
: Specifies the number of binsgeom_histogram()
with binwidth
binwidth
: Specifies the width of each binbins
option or the binwidth
option.geom_histogram()
fill
: Fills the bars with a specific color.color
: Adds an outline of a specific color to the bars.ggplot()
- Boxplotgeom_boxplot()
geom_boxplot()
geom_boxplot()
creates a boxplot;
x
and y
aestheticsgeom_boxplot()
geom_boxplot()
# 1. `show.legend = FALSE` turns off
# the legend information
# 2. `scale_fill_colorblind()` or
# `scale_fill_tableau()`
# applies a color-blind friendly
# palette to the `fill` aesthetic
# To use the scale_fill_tableau():
library(ggthemes)
ggplot(data = mpg,
mapping =
aes(x = hwy,
y = class,
fill = class)) +
geom_boxplot(
show.legend = FALSE) +
scale_fill_tableau()
fill
: Maps a variable to the fill color of the boxes.scale_fill_tableau()
: A color-blind friendly palette to the fill
aesthetic.geom_boxplot()
fct_reorder(CATEGORICAL, NUMERICAL)
fct_reorder(CATEGORICAL, NUMERICAL)
: Reorders the categories of the CATEGORICAL by the median of the NUMERICAL.