| plot_freq | R Documentation |
Creates a frequency plot showing the frequency of every observed value, optionaly by group. Most frequent values are labeled by default.
plot_freq(
formula,
y2 = NULL,
data = NULL,
freq = TRUE,
order = NULL,
col = "dodgerblue",
lwd = 9,
width = NULL,
value.labels = "auto",
ticks.max = 30,
show.x.value = "auto",
show.legend = TRUE,
legend.title = NULL,
col.text = NULL,
...
)
formula |
Two possible uses (similar to
|
y2 |
optional second variable when contrasting two variables |
data |
An optional data frame containing the variables in the formula. |
freq |
Logical. If TRUE (default), displays frequencies. If FALSE, displays percentages. |
order |
Controls the order in which groups appear in the plot and legend.
Use |
col |
Color for the bars. |
lwd |
Line width for the frequency bars. Default is 9. |
width |
Numeric. Width of the frequency bars. If NULL (default), width is automatically calculated based on the spacing between values. |
value.labels |
Controls value labeling. If numeric, shows labels for the |
ticks.max |
Integer. Maximum number of unique x values to label on the x-axis. If there are more
than |
show.x.value |
Either |
show.legend |
Logical. If TRUE (default), displays a legend when |
legend.title |
Character string. Title for the legend when |
col.text |
Color for the value labels. If not specified, uses |
... |
Pass on any argument accepted by |
This function creates a frequency plot where each observed value is shown with its frequency. Unlike a standard histogram, there is no binning, unlike a barplot, non-observed values of the variable are shown with 0 frequency instead of skipped.
Invisibly returns a data frame with values and their frequencies.
# Simple example
x <- c(1, 1, 2, 2, 2, 5, 5)
plot_freq(x)
# Pass on some common \code{plot()} arguments
plot_freq(x, col = "steelblue", xlab = "Value", ylab = "Frequency",ylim=c(0,7))
# Add to an existing plot
plot_freq(x, col = "dodgerblue")
# Compare two vectors
y1 <- c(1, 1, 2, 2, 2, 5, 5)
y2 <- c(1, 2, 2, 3, 3, 3)
plot_freq(y1, y2)
# Using a data frame with grouping
df <- data.frame(value = c(1, 1, 2, 2, 2, 5, 5), group = c("A", "A", "A", "B", "B", "A", "B"))
plot_freq(value ~ 1, data = df) # single variable
plot_freq(value ~ group, data = df) # with grouping
# Control group order in legend and plot
plot_freq(value ~ group, data = df, order = c("B", "A")) # B first, then A
plot_freq(value ~ group, data = df, order = -1) # Reverse default order
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.