将 lines() 应用于数据框/矩阵的列;每条线都有不同的颜色 [英] Apply lines() to columns of a data frame/matrix; each line with a different color

查看:15
本文介绍了将 lines() 应用于数据框/矩阵的列;每条线都有不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一个不涉及使用其他软件包(例如 ggplot)的解决方案.虽然绘制多条线非常简单,但我还没有想出一种方法来将参数的不同值(例如,不同的颜色)应用于不同的线.下面的代码(带有结果图)是我的尝试,这显然没有达到我想要的效果.我也不想使用循环,因为我试图让我的脚本尽可能简单.

I am trying to come up with a solution that doesn't involve using other packages such as ggplot. While plotting multiple lines is pretty straightforward, I haven't figured out a way to apply different values of an argument - e.g., different colors - to different lines. The code below (with the resulting plot) was my attempt, which obviously didn't do what I would like it to do. I also don't want to use a loop because I am trying to make my script as simple as possible.

df = cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10)))
plot(0, xlim = c(1,10), ylim=range(df), type="n")
apply(df, 2, lines, type="b", col = c("red", "blue", "black"))

我真正想要的是如下图:

What I really want is a plot like below:

plot(0, xlim = c(1,10), ylim=range(df), type="n")
color = c("red","blue","black")
for(i in 1:3){
    lines(1:10, df[,i], type = "b", col=color[i])
}

提前谢谢你!

推荐答案

试试matplot():

df <- cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10)))
matplot(df, type="b", lty=1, pch=1, col=c("blue", "red", "black"))

这篇关于将 lines() 应用于数据框/矩阵的列;每条线都有不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆