设置单个数据点的颜色

nit*_*nit 19 plot r

如何在散点图中设置单个数据点的颜色R

我在用 plot

Jas*_*gan 34

要扩展@Dirk Eddelbuettel的答案,您可以col在通话中使用任何功能plot.例如,这会使x==3点变为红色,而将所有其他颜色保持为黑色:

x <- 1:5
plot(x, x, col=ifelse(x==3, "red", "black"))
Run Code Online (Sandbox Code Playgroud)

例1

点角色pch,角色扩展cex等也是如此.

plot(x, x, col=ifelse(x==3, "red", "black"),
     pch=ifelse(x==3, 19, 1), cex=ifelse(x==3, 2, 1))
Run Code Online (Sandbox Code Playgroud)

例2


Das*_*son 19

通过代码执行您想要执行的操作非常简单,而其他人则提供了很好的方法来执行此操作.但是,如果您希望单击要更改颜色的点,则可以使用"识别"和"点"命令以新颜色重新绘制这些点.

# Make some data
n <- 15
x <- rnorm(n)
y <- rnorm(n)

# Plot the data
plot(x,y)

# This lets you click on the points you want to change
# the color of.  Right click and select "stop" when
# you have clicked all the points you want
pnt <- identify(x, y, plot = F)

# This colors those points red
points(x[pnt], y[pnt], col = "red")

# identify beeps when you click.
# Adding the following line before the 'identify' line will disable that.
# options(locatorBell = FALSE)
Run Code Online (Sandbox Code Playgroud)


Dir*_*tel 11

使用矢量化col=参数,例如in

 plot(1:5, 1:5, col=1:5)
Run Code Online (Sandbox Code Playgroud)

你得到五种不同颜色的五分:

在此输入图像描述

您可以使用相同的逻辑在数据点中仅使用两种或三种颜色.理解索引是R等语言的关键.