如何在R
中为散点图中的单个数据点设置颜色?
我正在使用plot
发布于 2012-01-08 07:48:40
要扩展@Dirk Eddelbuettel的答案,您可以在对plot
的调用中使用任何col
函数。例如,这会将x==3
点着色为红色,而将所有其他点保留为黑色:
x <- 1:5
plot(x, x, col=ifelse(x==3, "red", "black"))
点字符扩展、字符扩展cex
等也是如此。
plot(x, x, col=ifelse(x==3, "red", "black"),
pch=ifelse(x==3, 19, 1), cex=ifelse(x==3, 2, 1))
发布于 2012-01-08 08:01:37
通过代码做你想做的事情很容易,其他人已经提供了很好的方法来做到这一点。但是,如果你更愿意点击你想要改变颜色的点,你可以通过使用'identify‘和' points’命令在这些点上以新的颜色重新绘图来实现这一点。
# 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)
https://stackoverflow.com/questions/8774002
复制相似问题