【发布时间】:2012-02-05 03:16:58
【问题描述】:
如何在R 的散点图中设置单个数据点的颜色?
我正在使用plot
【问题讨论】:
-
您想以特定颜色绘制所有数据点还是仅绘制 1 个特定数据点?
-
我想为散点图中的特定数据点着色。
如何在R 的散点图中设置单个数据点的颜色?
我正在使用plot
【问题讨论】:
要扩展@Dirk Eddelbuettel 的答案,您可以在对plot 的调用中使用col 的任何函数。例如,这会将x==3 点染成红色,而将所有其他点染成黑色:
x <- 1:5
plot(x, x, col=ifelse(x==3, "red", "black"))
点字符pch、字符扩展cex等也是如此。
plot(x, x, col=ifelse(x==3, "red", "black"),
pch=ifelse(x==3, 19, 1), cex=ifelse(x==3, 2, 1))
【讨论】:
col=c(rep("black",3), rep("blue", 2)) 会有三个黑点,然后是两个蓝点。
通过代码做你想做的事很容易,其他人已经给出了很好的方法来做到这一点。但是,如果您希望单击要更改颜色的点,可以使用“识别”和“点”命令以新颜色重新绘制这些点。
# 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)
【讨论】:
使用col= 参数,该参数是矢量化 以便例如在
plot(1:5, 1:5, col=1:5)
五种不同颜色的五分:
您可以使用相同的逻辑在数据点中仅使用两种或三种颜色。理解索引是 R 等语言的关键。
【讨论】: