【问题标题】:Coluring the scatterplot in R在 R 中为散点图着色
【发布时间】:2013-10-04 18:55:16
【问题描述】:

我有两个向量

 A=rnorm(500)
 B=rnorm(500)

并且想创建一个散点图并使用

   Plot(A,B,cex=0.5,col="grey") ### this creates the base scatterplot

现在,我还有两个条件,其中我有三个向量,它们是原始向量的子集:

 C<-subset[A,select=c(1:10,20:30,60:75,90,100) ### to be coloured in blue
 D<-subset[A,select=c(25:60)] ### to be coloured in blue
 E<-subset[B,select=c(100:150,120:125)] ### to be colured in red.

我应该如何修改散点图来改变这些向量 C、D、E 的颜色,而不是原来的灰色?概念类似这样:

【问题讨论】:

  • 你能通过改变向量 C、D、E 的颜色来澄清你的意思吗?如果你的意思是改变散点图中的颜色,你需要输入两个相同长度的向量。此外,您提供的代码不是有效的 R 代码。
  • @ialm :是的,解决方案是正确的。谢谢。我想根据代表每个条件的子集向量将灰色点替换为彩色点。
  • 很高兴它成功了。另外,请注意C 和D 指定的索引有一些重叠,因此重复的索引将根据最后一个调用来着色。

标签: r data-visualization scatter-plot


【解决方案1】:

我是这样解释你的问题的:

“用不同颜色绘制问题中由索引C、D、E 指示的点的子集”。

A <- rnorm(500)
B <- rnorm(500)

## Set the indices, as written in question
Ci <- c(1:10,20:30,60:75,90,100) ### to be coloured in blue
Di <- c(25:60)                   ### to be coloured in blue
Ei <- c(100:150,120:125)         ### to be coloured in red.

## Plot the original scatterplot, then plot over the points of interest with colour
## Use the "points" function from base graphics to plot points on existing plot
## Grab the relevant points from vectors A and B by accessing them at indices 
## Ci, Di, Ei using `[]`
plot(A, B, cex=0.5, col="grey")
points(A[Ci], B[Ci], cex=0.5, col="blue")
points(A[Di], B[Di], cex=0.5, col="blue")
points(A[Ei], B[Ei], cex=0.5, col="red")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2013-04-06
    • 1970-01-01
    • 2023-01-19
    • 2018-01-07
    相关资源
    最近更新 更多