1.载入相关package
library(patchwork)
library(dplyr)
library(ggplot2)
library(ggstatsplot)
library(ggpattern)
library(esquisse)
library(scales)
2.生成数据
set.seed(2016)
df <- expand.grid(c(2011:2020), c(1:5)) %>%
rename(year= Var1, id = Var2) %>%
mutate(value = runif(dim(df)[1], min = 0, max = 1))
3.画图
f1<-ggplot(df, aes(x = year, y = value, color = factor(id))) +
geom_line(aes(linetype = factor(id)))
f2<-ggplot(df, aes(x = year, y = value, color = factor(id))) +
geom_line(aes(linetype = factor(id))) +
theme_bw()
f3<-ggplot(df, aes(x = year, y = value, group = id, color = factor(id))) +
geom_line(aes(linetype = factor(id)),color= "black") +
labs(title = "Time Trend of Value by ID", x = "Year", y = "Value") +
scale_linetype_manual(values = c("solid", "dashed", "dotted", "dotdash", "longdash"))+
scale_x_continuous(breaks = seq(min(df$year), max(df$year), by = 1))+ # 将 x 轴刻度设置为间隔1年的整数年份
theme_bw()+
theme(plot.title = element_text(hjust = 0.5), # 设置标题居中
aspect.ratio = 15/35)
f4<-ggplot(df, aes(x = year, y = value, group = id, color = factor(id))) +
geom_line(aes(linetype = factor(id))) +
labs(title = "Time Trend of Value by ID", x = "Year", y = "Value") +
scale_linetype_manual(values = c("solid", "dashed", "dotted", "dotdash", "longdash"))+
scale_color_manual(values = c("red", "blue", "green", "orange", "purple")) + #设置颜色
scale_x_continuous(breaks = seq(min(df$year), max(df$year), by = 1))+
theme_bw()+
theme(plot.title = element_text(hjust = 0.5),
aspect.ratio = 15/35)
4.拼图
(f1 | f2) / (f3 | f4)
layout <- '
ABB
CDD
'
f1 + f2 + f3 + f4 + plot_layout(design = layout)