gpt4 book ai didi

r - 在一个图中创建大量密度线

转载 作者:行者123 更新时间:2023-12-04 10:50:56 25 4
gpt4 key购买 nike

我有一个如下所示的数据框:

DF<-as.data.frame(t(replicate(150, sample(seq(100, 1000),15,replace=T))),rownames=T)

我想将单个行绘制为密度,以便获得具有多个密度曲线的图。我知道我可以像下面这样一行一行地完成:
plot(density(DF[,1]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,2]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,3]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,4]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,5]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,6]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,7]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,8]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,9]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,10]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,11]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,12]),col = adjustcolor('black', alpha.f = .5))
lines(density(DF[,13]),col = adjustcolor('black', alpha.f = .5))
#...and so forth

但我想知道是否有一种自动化的方法可以一次处理所有行,否则这个过程很快就会变得很麻烦。

最佳答案

你也可以使用 lapply:

plot(density(DF[,1]),col = adjustcolor('black', alpha.f = .5),ylim=c(0,0.0015))
lapply(DF[,-1],function(x){lines(density(x),col = adjustcolor('black', alpha.f = .5))}
)

enter image description here
如果你想使用颜色,你可以考虑这样的事情:
#making a palette
mycols <- rainbow(ncol(DF))

#plotting, now using numerical column indices to access colour at same time
plot(density(DF[,1]),col = adjustcolor(mycols[1], alpha.f = .5),ylim=c(0,0.0015))
lapply(2:ncol(DF),function(x){lines(density(DF[,x]),col = adjustcolor(mycols[x], alpha.f = .5))}
)
#add legend
legend(x=1100, y=0.0015,col=mycols,lty=1,legend=colnames(DF))

enter image description here

关于r - 在一个图中创建大量密度线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34616464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com