
par(mfrow=c(2,2))
x=1:100
y=sample(seq(-10,10,0.05),size = 100,replace = T)
plot(sort(x),sort(y),xlab = "正相关",ylab="")
plot(sort(x),sort(y,decreasing = T),xlab = "负相关",ylab="")
x=seq(-10,10,0.1)
plot(sort(x),x^2,,xlab = "非线性相关",ylab="")
x=rnorm(100,0,10)
y=rnorm(100,0,10)
plot(x,y,xlab = "不相关",ylab="")
ggplot2也可以做
准备好数据可以用for循环批量做图的
df1 <- data.frame( x=1:100,
y=sort(sample(seq(-10,10,0.05),
size = 100,
replace = T)
)
)
df2 <- data.frame(x=1:100,
y=sort(sample(seq(-10,10,0.05),
size = 100,
replace = T),
decreasing = T)
)
df3 <- data.frame(x=seq(-10,10,0.1),
y=(seq(-10,10,0.1))^2)
df4 <- data.frame(x=rnorm(100,0,10),
y=rnorm(100,0,10))
pd <- list(df1,df2,df3,df4)
namex=c("正相关","负相关","非线性相关","不相关")
plots <- list()
n=1
for(n in 1:length(pd)){
plots[[n]] <- ggplot(pd[[n]],aes(x=x,y=y)) +
geom_point()+
theme_bw()+
scale_x_discrete(name = namex[[n]]) +
scale_y_continuous(name = "")
}
grid.arrange(grobs = plots, ncol = 2)

网友评论