create a boxplot with ggplot2 in r

To create a boxplot with ggplot2 in R, you need to first prepare your data in a tidy format, meaning each variable should be in its own column with observations organized in rows. Here is an example using the built-in mtcars dataset:

main.r
library(ggplot2)

# convert data to tidy format 
mtcars_long <- tidyr::pivot_longer(mtcars, cols = mpg:carb, names_to = "variable", values_to = "value")

# create boxplot using ggplot2 and aes() function 
ggplot(mtcars_long, aes(x = variable, y = value)) +
  geom_boxplot()
274 chars
9 lines

This code will produce a simple boxplot with each variable on the x-axis and its corresponding values on the y-axis. You can add additional aesthetics, such as color or grouping, by adding more arguments to the aes() function.

gistlibby LogSnag