基于值的颜色散点

时间:2022-12-07 00:26:36

I am able to plot a scatter plot and color the points based on one criteria, i.e. I can color all points >=3 as red and the remainder as black. I would love to be able to color points in this fashion:

我可以绘制散点图,并根据一个标准给点上色,也就是说,我可以把>=3的所有点都涂成红色,其余的都涂成黑色。我希望能够用这种方式给点上色:

  1. =3 color red

    = 3红色

  2. <=1 color blue
  3. < = 1蓝色
  4. The rest as black
  5. 其余的是黑人

The code I have below completes step 1 and 3 but I am not sure how to incorporate the second argument of step 2

下面的代码完成了第1步和第3步,但是我不确定如何合并第2步的第二个参数

data<- read.table('sample_data.txtt', header=TRUE, row.name=1)
pos<- data$col_name1
cn<- data$col_name2
plot(pos,cn, ylim=c(0,5), col="blue")
plot(pos,cn, col=ifelse(cn>=3,"red","black"), ylim=c(0,10))

Any help would be great!!! Thanks in advance

任何帮助都是伟大的!!!谢谢提前

基于值的颜色散点

3 个解决方案

#1


42  

Best thing to do here is to add a column to the data object to represent the point colour. Then update sections of it by filtering.

这里最好的做法是向数据对象添加一个列来表示点颜色。然后通过过滤更新它的部分。

data<- read.table('sample_data.txtt', header=TRUE, row.name=1)
# Create new column filled with default colour
data$Colour="black"
# Set new column values to appropriate colours
data$Colour[data$col_name2>=3]="red"
data$Colour[data$col_name2<=1]="blue"
# Plot all points at once, using newly generated colours
plot(data$col_name1,data$col_name2, ylim=c(0,5), col=data$Colour, ylim=c(0,10))

It should be clear how to adapt this for plots with more colours & conditions.

应该很清楚如何将其应用于色彩和条件更丰富的情节。

#2


14  

Also it'd work to just specify ifelse() twice:

另外,只需指定ifelse()两次也可以:

plot(pos,cn, col= ifelse(cn >= 3, "red", ifelse(cn <= 1,"blue", "black")), ylim = c(0, 10))

#3


2  

It's better to create a new factor variable using cut(). I've added a few options using ggplot2 also.

最好使用cut()创建一个新的因子变量。我还添加了一些使用ggplot2的选项。

df <- data.frame(
  X1=seq(0, 5, by=0.001),
  X2=rnorm(df$X1, mean = 3.5, sd = 1.5)
)

# Create new variable for plotting
df$Colour <- cut(df$X2, breaks = c(-Inf, 1, 3, +Inf), 
                 labels = c("low", "medium", "high"), 
                 right = FALSE)

### Base Graphics

plot(df$X1, df$X2, 
     col = df$Colour, ylim = c(0, 10), xlab = "POS", 
     ylab = "CS", main = "Plot Title", pch = 21)

plot(df$X1,df$X2, 
     col = df$Colour, ylim = c(0, 10), xlab = "POS", 
     ylab = "CS", main = "Plot Title", pch = 19, cex = 0.5)

# Using `with()` 

with(df, 
     plot(X1, X2, xlab="POS", ylab="CS", col = Colour, pch=21, cex=1.4)
     )

# Using ggplot2
library(ggplot2)

# qplot()
qplot(df$X1, df$X2, colour = df$Colour)

# ggplot()
p <- ggplot(df, aes(X1, X2, colour = Colour)) 
p <- p + geom_point() + xlab("POS") + ylab("CS")
p

p + facet_grid(Colour~., scales = "free")

#1


42  

Best thing to do here is to add a column to the data object to represent the point colour. Then update sections of it by filtering.

这里最好的做法是向数据对象添加一个列来表示点颜色。然后通过过滤更新它的部分。

data<- read.table('sample_data.txtt', header=TRUE, row.name=1)
# Create new column filled with default colour
data$Colour="black"
# Set new column values to appropriate colours
data$Colour[data$col_name2>=3]="red"
data$Colour[data$col_name2<=1]="blue"
# Plot all points at once, using newly generated colours
plot(data$col_name1,data$col_name2, ylim=c(0,5), col=data$Colour, ylim=c(0,10))

It should be clear how to adapt this for plots with more colours & conditions.

应该很清楚如何将其应用于色彩和条件更丰富的情节。

#2


14  

Also it'd work to just specify ifelse() twice:

另外,只需指定ifelse()两次也可以:

plot(pos,cn, col= ifelse(cn >= 3, "red", ifelse(cn <= 1,"blue", "black")), ylim = c(0, 10))

#3


2  

It's better to create a new factor variable using cut(). I've added a few options using ggplot2 also.

最好使用cut()创建一个新的因子变量。我还添加了一些使用ggplot2的选项。

df <- data.frame(
  X1=seq(0, 5, by=0.001),
  X2=rnorm(df$X1, mean = 3.5, sd = 1.5)
)

# Create new variable for plotting
df$Colour <- cut(df$X2, breaks = c(-Inf, 1, 3, +Inf), 
                 labels = c("low", "medium", "high"), 
                 right = FALSE)

### Base Graphics

plot(df$X1, df$X2, 
     col = df$Colour, ylim = c(0, 10), xlab = "POS", 
     ylab = "CS", main = "Plot Title", pch = 21)

plot(df$X1,df$X2, 
     col = df$Colour, ylim = c(0, 10), xlab = "POS", 
     ylab = "CS", main = "Plot Title", pch = 19, cex = 0.5)

# Using `with()` 

with(df, 
     plot(X1, X2, xlab="POS", ylab="CS", col = Colour, pch=21, cex=1.4)
     )

# Using ggplot2
library(ggplot2)

# qplot()
qplot(df$X1, df$X2, colour = df$Colour)

# ggplot()
p <- ggplot(df, aes(X1, X2, colour = Colour)) 
p <- p + geom_point() + xlab("POS") + ylab("CS")
p

p + facet_grid(Colour~., scales = "free")