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

查看:146
本文介绍了将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])
}


谢谢提前!

Thank you in advance!

推荐答案

尝试 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天全站免登陆