首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >设置单个数据点的颜色

设置单个数据点的颜色
EN

Stack Overflow用户
提问于 2012-01-08 07:28:11
回答 2查看 91.9K关注 0票数 23

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

我正在使用plot

EN

回答 2

Stack Overflow用户

发布于 2012-01-08 07:48:40

要扩展@Dirk Eddelbuettel的答案,您可以在对plot的调用中使用任何col函数。例如,这会将x==3点着色为红色,而将所有其他点保留为黑色:

代码语言:javascript
复制
x <- 1:5
plot(x, x, col=ifelse(x==3, "red", "black"))

点字符扩展、字符扩展cex等也是如此。

代码语言:javascript
复制
plot(x, x, col=ifelse(x==3, "red", "black"),
     pch=ifelse(x==3, 19, 1), cex=ifelse(x==3, 2, 1))

票数 38
EN

Stack Overflow用户

发布于 2012-01-08 08:01:37

通过代码做你想做的事情很容易,其他人已经提供了很好的方法来做到这一点。但是,如果你更愿意点击你想要改变颜色的点,你可以通过使用'identify‘和' points’命令在这些点上以新的颜色重新绘图来实现这一点。

代码语言:javascript
复制
# 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)
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8774002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档