线条的不同颜色,而不是 R 中的点 [英] Different colors for lines as opposed to points in R

查看:48
本文介绍了线条的不同颜色,而不是 R 中的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个笛卡尔坐标数据集.我想制作一个基本的散点图,其中每个点都根据预设的颜色向量进行着色.使用以下示例很容易实现这一点.

I have a dataset of Cartesian coordinates. I want to make a basic scatter plot where each point is colored according to a pre-set vector of colors. This is easy to achieve with something like the following example.

# A hypothetical dataset
XCoords<-c(1,3,5,6,8)
YCoords<-c(3,9,4,3,4)

# Sorry for picking such ugly colors, its just an example
Colors<-c("#FDA75F","#F1E19D","#E5AC4D","#FDC07A","#FDB46C")

# Plot the scatter plot
plot(x=XCoords,y=YCoords,pch=16,col=Colors)

但是,如果我想使用 type="o" 或 type="b"(取决于系统)在点之间画一条线,那条线的颜色将默认为 中的第一种颜色颜色 向量.

However, if I want to draw a line between the points using type="o" or type="b" (depending on the system), the color of that line will default to the first color in the Colors vector.

# An example using type="o"
plot(x=XCoords,y=YCoords,pch=16,col=Colors,type="o")

如果我希望线条是完全不同的颜色怎么办,例如黑色的?换句话说,如何将连接点的线的颜色设置为独立于点的配色方案.

What if I want the line to be a completely different color, e.g. black? In other words, how can I set the color of the line connecting the points to be independent of the coloring scheme of the points.

我专门在 base(没有 ggplot)中寻找解决此问题的方法,理想情况下,它不会要求我在两个单独的步骤中绘制线条和点(尽管如果这是唯一的方法,那也没关系).

I am specifically looking for a solution to this problem in base (no ggplot), and ideally one that does not ask me to plot the lines and points in two separate steps (though if that's the only way, that's okay).

推荐答案

我认为正确的做法其实是分两步做:

I think the correct way to do this is actually to do it in two steps:

plot(x=XCoords,y=YCoords,pch=16,col=Colors)
lines(x = XCoords,y=YCoords,col = "black")

这篇关于线条的不同颜色,而不是 R 中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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