knit
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
knitr::opts_chunk$set(tidy = TRUE, tidy.opts = list(width.cutoff = 60))
library(tidyverse)
library(knitr)
ggplot
箱线图案例
# Load the ggplot2 package
if (!require("ggplot2")) install.packages("ggplot2")
library(ggplot2)
# Create sample data
set.seed(123) # Set a random seed for reproducibility
data <- data.frame(
Category = rep(c("A", "B", "C"), each = 100),
Value = c(rnorm(100, mean = 10, sd = 2),
rnorm(100, mean = 12, sd = 3),
rnorm(100, mean = 8, sd = 1))
)
# Create a boxplot using ggplot2
ggplot(data, aes(x = Category, y = Value)) +
geom_boxplot() + # Add boxplot layer
geom_point(position = position_jitter(width = 0.2), color = "blue") + # Add jittered points layer to show raw data
labs(title = "Example Boxplot with ggplot2", # Set the chart title
x = "Categories", # Set the x-axis label
y = "Values") + # Set the y-axis label
theme_minimal() + # Use a minimalist theme
theme(axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-axis labels for better readability
plot.title = element_text(hjust = 0.5)) + # Center the title
scale_y_continuous(breaks = seq(0, 15, by = 2)) + # Set y-axis tick marks
scale_color_brewer(palette = "Pastel1") + # Use a color brewer palette for points
theme(plot.background = element_rect(fill = "gray95"), # Set the plot background color
axis.line = element_line(color = "black"), # Set axis line color
panel.grid.major = element_line(color = "gray80"), # Set major grid line color
panel.grid.minor = element_line(color = "gray90")) # Set minor grid line color
标签:set,样式,chunk,ggplot,library,ggplot2,knit,opts,knitr
From: https://www.cnblogs.com/chen-heybro/p/18220973