如何根据r中变量的值为3D图着色

h43kikqp  于 9个月前  发布在  其他
关注(0)|答案(3)|浏览(106)

我有三个坐标变量和一个连续变量。我想得到一个三维散点图条件的连续变量的具体值。
我的数据的例子是:

A = c( 8.3, 7.5, 8.0, 7.1  6.5, 7.4)

x = c(147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

y = c( 147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

z = c( 22, 21, 22, 22, 30, 26)

字符串
我想得到3D的散点图,以A的值为条件。
例如,如果A的值在88.5之间,则颜色为红色。
如果A的值在7:7.5之间,则颜色为蓝色。
最后,如果A的值在6:6.5之间,则颜色为绿色。
我的数据包含大约3000个观察结果。所以,我只是提供了一个例子,我的问题。任何帮助,请?

请注意,我使用了ploty函数,但由于我的数据非常大,输出不清晰,没有帮助。

np8igboo

np8igboo1#

你可以用ifelse函数来实现,

A = c( 8.3, 7.5, 8.0 ,7.1 , 6.5, 7.4)

x = c(147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

y = c( 147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)

z = c( 22, 21, 22, 22, 30, 26)

my_color <- ifelse(A<=8.5 & A>8,"red", ifelse(A<=7.5 & A>7,"blue","green"))

plot_ly(x=x, y=y, z=z, type="scatter3d", mode="markers", color=my_color)

字符串


的数据
此外,如果您不想使用ifelse分配它,您可以创建集群并将其分配给plotly,如,

n =3 # number of clusters

my_col_cluster <- kmeans(A,n)$cluster

plot_ly(x=x, y=y, z=z, type="scatter3d", mode="markers", color= my_col_cluster)


k3bvogb1

k3bvogb12#

您可以尝试plot3D

install.packages("plot3D")
library(plot3D)

## Use ifelse() to create color labels
colVar <- sapply(A,function(a){ifelse(a>=6&a<=6.5,'green',ifelse(a>=7&a<=7.5,'blue','red'))})
colVar <- factor(colVar,levels=c('green','blue','red'))

## Plot using plot3D(). 
## colvar takes integer values for color groups.
## colkey creates the legend
## col sets the color scheme to each group index
scatter3D(x=x,y=y,z=z,
      colvar=as.integer(colVar),
      colkey=list(at=c(1,2,3),side=4),
      col=as.character(levels(colVar)),
      pch=19)

字符串


的数据

gjmwrych

gjmwrych3#

# If you want a continuous color scale on the z-axis, this is also a possible solution

# Define data
x = c(147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)
y = c( 147.2818, 147.2818, 147.2779, 147.2779, 147.2337, 147.1693)
z = c( 22, 21, 22, 22, 30, 26) 

# Make a data frame
data <- as.data.frame(cbind(x,y,z))

# Make z data a factor
data$z <- factor(data$z)

# Determine how many unique observations exist by checking the number of levels, store that number in "length"
length <- length(levels(data$z))

# Create new dummy column of z data
data$dummy <- data$z

# Define the color scale, using viridis here 
color_scale <- colorRampPalette(c("#40478F","#238A8D", "#55C667", "#FDE725"))(n = length)

# Create a vector of color codes that pair with each z value
data$color <- color_scale[data$dummy]

install.packages("scatterplot3d")
install.packages("plotfunctions")
library(scatterplot3d)
library(plotfunctions)

attach(data)
scatterplot3d(x=x,y=y,z=z, #plot data 
              color = data$color, # use color vector
              type = "h",
              pch = 16)

gradientLegend(valRange = c(20,30), # add the legend
               color = c("#40478F","#238A8D", "#55C667", "#FDE725"),
               side = 4,
               pos.num = 1,
               n.seg = 1)

字符串

相关问题