R,使用quadplot,根据第5列值生成数据点的颜色渐变

时间:2021-10-31 00:05:55

I'd like to use quadplot to graph data points that are color coded based on a 5th variable. As an example:

我想使用quadplot来绘制基于第5个变量进行颜色编码的数据点。举个例子:

a <- c(.2,.4,.6,.4,.2,.4,.2,.5)
b <- c(.2,.3,.1,.3,.3,.3,.4,.2)
c <- c(.3,.1,.2,.1,.1,.1,.1,.1)
d <- c(.2,.2,.1,.2,.4,.2,.3,.2)
e <- c(-10,20,-100,90,10,-30,-12)
f <- data.matrix(data.frame(a,b,c,d,e))

      a   b   c   d    e
[1,] 0.2 0.2 0.3 0.2  -10
[2,] 0.4 0.3 0.1 0.2   20
[3,] 0.6 0.1 0.2 0.1 -100
[4,] 0.4 0.3 0.1 0.2   90
[5,] 0.2 0.3 0.1 0.4  -10
[6,] 0.4 0.3 0.1 0.2   20
[7,] 0.2 0.4 0.1 0.3 -100
[8,] 0.5 0.2 0.1 0.2   90

I want to plot a, b, c, and d, and have a color gradient for each data point, based on the value in column "e". Any ideas? If there is another package that can do what I need, then that works too. Thanks for any input, in advance.

我想绘制a,b,c和d,并根据“e”列中的值为每个数据点设置颜色渐变。有任何想法吗?如果有另一个包可以做我需要的,那么这也有效。感谢任何输入,提前。

1 个解决方案

#1


1  

Is this what you want ?

这是你想要的吗 ?

# Create custom color palette (blue to red gradient)
grad <- colorRampPalette(c("blue","red"))

# Get a vector of colors and deal with negative values in column "e"
colors <- grad(length(min(f[,5]):max(f[,5])))
index <- f[,5] + abs(min(f[,5]))

# Plot using quadplot
quadplot(f[,-5], labelcol=1, labelpch=1:4, col=colors[index], pch=19)

# Add color legend
library(shape)
colorlegend(col=colors, zlim=range(f[,5]), zval=sort(unique(f[,5])), posx = c(0.86, 0.89), posy=c(0.2,0.9))

#1


1  

Is this what you want ?

这是你想要的吗 ?

# Create custom color palette (blue to red gradient)
grad <- colorRampPalette(c("blue","red"))

# Get a vector of colors and deal with negative values in column "e"
colors <- grad(length(min(f[,5]):max(f[,5])))
index <- f[,5] + abs(min(f[,5]))

# Plot using quadplot
quadplot(f[,-5], labelcol=1, labelpch=1:4, col=colors[index], pch=19)

# Add color legend
library(shape)
colorlegend(col=colors, zlim=range(f[,5]), zval=sort(unique(f[,5])), posx = c(0.86, 0.89), posy=c(0.2,0.9))