根据值用条件颜色绘制折线图 [英] Plot a line chart with conditional colors depending on values

查看:145
本文介绍了根据值用条件颜色绘制折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制折线图.根据值,它应该更改其颜色. 我发现的是:

I want to plot a line chart. Depending on values it should change its color. What I found is:

plot(sin(seq(from=1, to=10,by=0.1)),type="p", 
       col=ifelse(sin(seq(from=1, to=10,by=0.1))>0.5,"red","yellow"))

那行得通.但是,一旦我从type ="p"更改为type ="l",条件着色就会消失.

That works. But as soon as I change from type="p" to type="l" the conditional colouring disappears.

该行为是故意的吗?

使用基本图形来绘制具有不同颜色的功能线的解决方案是什么?

What is a solution with base graphics to plot a functional line with different colors?

推荐答案

使用segments代替lines.

segments函数将仅添加到现有绘图中.要创建具有正确轴和限制的空白图,请首先将plottype="n"一起使用,以绘制无".

The segments function will only add to an existing plot. To create a blank plot with the correct axes and limits, first use plot with type="n" to draw "nothing".

x0 <- seq(1, 10, 0.1)
colour <- ifelse(sin(seq(from=1, to=10,by=0.1))>0.5,"red","blue")

plot(x0, sin(x0), type="n")
segments(x0=x0, y0=sin(x0), x1=x0+0.1, y1=sin(x0+0.1), col=colour)

有关更多详细信息,请参见?segments.

See ?segments for more detail.

这篇关于根据值用条件颜色绘制折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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