Boxplot in R (9 Examples) | Create a Box-and-Whisker Plot in RStudio | boxplot() Function & ggplot2

Описание к видео Boxplot in R (9 Examples) | Create a Box-and-Whisker Plot in RStudio | boxplot() Function & ggplot2

How to draw a boxplot in the R programming language. More details: https://statisticsglobe.com/boxplot-in-r
R code of this video tutorial:

##### Example 1
set.seed(8642) # Create random data
x <- rnorm(1000)
boxplot(x) # Basic boxplot in R

##### Example 2
y <- runif(1000) # Create more variables
z <- rpois(1000, 3)
data <- data.frame(values = c(x, y, z), # Combine variables in data frame
group = c(rep("x", 1000),
rep("y", 1000),
rep("z", 1000)))
head(data) # First six rows of data
boxplot(values ~ group, data) # Multiple boxplots in same graph

##### Example 3
boxplot(values ~ group, data, # Change main title and axis labels
main = "My Boxplots",
xlab = "My Boxplot Groups",
ylab = "The Values of My Boxplots")

##### Example 4
boxplot(values ~ group, data, # Horizontal boxplots
horizontal = TRUE)

##### Example 5
boxplot(values ~ group, data, # Thin boxplots
notch = TRUE)

##### Example 6
boxplot(values ~ group, data, # Color of boxplots
col = "red")

##### Example 7
boxplot(values ~ group, data, # Different color for each boxplot
col = c("red", "green", "purple"))

##### Example 8
data2 <- data # Replicate data
data2$group <- c(rep("x1", 500), rep("x2", 500), # Modify group variable
rep("y1", 500), rep("y2", 500),
rep("z1", 500), rep("z2", 500))
boxplot(values ~ group, data2, # Boxplot with manual positions
col = c("blue", "pink"),
at = c(1, 2, 5, 6, 9, 10))

##### Example 9
install.packages("ggplot2") # Install and load ggplot2
library("ggplot2")
ggplot(data2, aes(x = group, y = values, fill = group)) + # Create boxplot chart in ggplot2
geom_boxplot()

Комментарии

Информация по комментариям в разработке