Write R code to create a boxplot for the vector scores <- c(78, 85, 90, 88, 92, 80, 76, 85, 89, 91). Use the base R boxplot() function to create a boxplot in lightblue color and the title as "Score Distribution", and y label as "Scores"
Boxplots are used to visualize the distribution of numerical data and identify outliers. They display the median, quartiles, and potential outliers. In R, you can create boxplots using the base boxplot()
function or ggplot2
.
# Sample data
values <- c(5, 7, 8, 5, 6, 7, 8, 9, 5, 6)
# Create a boxplot
boxplot(values, main = "Basic Boxplot", ylab = "Values", col = "lightgreen")
library(ggplot2)
df <- data.frame(values = values)
ggplot(df, aes(y = values)) +
geom_boxplot(fill = "purple", color = "black") +
ggtitle("Boxplot with ggplot2") +
theme_minimal()
Boxplots are particularly useful for comparing distributions across multiple groups and spotting outliers.