R:设置绘图悬停文本的格式

R:设置绘图悬停文本的格式,r,hover,plotly,data-visualization,R,Hover,Plotly,Data Visualization,我正在使用R编程语言。我试图学习如何自定义3d plotly对象中的悬停文字,如下所示: 最近,我学习了如何为我模拟的一些数据创建3d plotly对象: library(Rtsne) library(dplyr) library(ggplot2) library(plotly) library(caret) library(randomForest) #data a = iris a <- unique(a) #create t

我正在使用R编程语言。我试图学习如何自定义3d plotly对象中的悬停文字,如下所示:

最近,我学习了如何为我模拟的一些数据创建3d plotly对象:

  library(Rtsne)
    library(dplyr)
    library(ggplot2)
    library(plotly)
    library(caret)
    library(randomForest)
    
  #data
a = iris
a <- unique(a)

#create two species just to make things easier
s <- c("a","b")
species<- sample(s , 149, replace=TRUE, prob=c(0.3, 0.7))
a$species = species
a$species = as.factor(a$species)

#split data into train/test, and then random forest 

index = createDataPartition(a$species, p=0.7, list = FALSE)
train = a[index,]
test = a[-index,]

rf = randomForest(species ~ ., data=train, ntree=50, mtry=2)

#have the model predict the test set
pred = predict(rf, test, type = "prob")
labels = as.factor(ifelse(pred[,2]>0.5, "a", "b"))
confusionMatrix(labels, test$species)


#tsne algorithm
tsne_obj_3 <- Rtsne(test[,-5], perplexity=1, dims=3)
df_m2 <- as.data.frame(tsne_obj_3$Y)

df_m2$labels = test$species

df_m2$color = ifelse(df_m2$labels == "a", "red","blue")
df_m2$petal_length = test$Petal.Length

axis_1 = df_m2$V1
axis_2 = df_m2$V2
axis_3 = df_m2$V3

plot_ly(x=as.vector(axis_1),
        y=as.vector(axis_2), 
        z=axis_3, 
        type="scatter3d",
        mode="markers", 
        name = "Obs", 
        marker = list(size = 3)) %>% 
   add_mesh(x=as.vector(axis_1), 
            y=as.vector(axis_2), 
            z=df_m2$pred, 
            type = "mesh3d", 
            name = "Preds")
库(Rtsne)
图书馆(dplyr)
图书馆(GG2)
图书馆(绘本)
图书馆(插入符号)
图书馆(森林)
#资料
a=虹膜

a不确定您想对预测的类做什么,但可能是这样的?(颜色对应真实物种,鼠标悬停也显示预测)

库(reprex)
雷普雷克斯({
SuppressPackageStatupMessages(不可见)(
lapply(c(“Rtsne”、“dplyr”、“ggplot2”、“plotly”、“插入符号”、“随机森林”),
require,character.only=TRUE)))
#资料

a您的plot_-ly-call的第9行有一个输入错误:
text=paste(“种类:,df_-m2$标签;“宽度:,df_-m2$花瓣宽度;“颜色:,df_-m2$颜色”)
。保留最后一个
最后。我试过了,我想会有更多的错误。我会继续试着调试它谢谢你的回答!我花了几个小时来修复这些错误!我真是太感谢你了!我只对一件事感到困惑。我想了整个“3d绘图”应该有3种颜色:一种是所有点的颜色,带有“种类a”,一种是所有点的颜色,带有“种类B”,以及其他所有曲面的一种颜色。答案中有4种颜色:红色、蓝色、棕色和粉色。是否可以删除棕色或粉色?我的另一个问题是:当我将鼠标移到棕色和粉色区域上时,会弹出“不可见点”的弹出窗口。这是否正确?您可以为网格使用
facecolor
,例如,添加到
add_mesh
函数
、facecolor=rep(“蓝色”,55)、不透明度=0.1
,或类似的效果。
p <- plot_ly(type = 'scatter3d', mode = 'markers', colors = "Accent", color = df_m2$color) %>%
  add_trace(
    x = df_m2$V1,
    y = df_m2$V2,
    z = df_m2$V3,
    marker = list(
      size = 3),
    name = df_m2$labels,
    text = paste("Species: ", df_m2$labels ; "Width: ", df_m2$petal.width ; "color: ", df_m2$color" ),
    showlegend = T
  )  %>%


   add_mesh(x=as.vector(axis_1), 
            y=as.vector(axis_2), 
            z=df_m2$pred, 
            type = "mesh3d", 
            name = "Preds")
  %>% 
  layout(
    title = "none",
    titlefont = list(
      size = 10
    ),
    paper_bgcolor = "#fffff8",
    font = t,
    xaxis = list(
      zeroline = F
    ),
    yaxis = list(
      hoverformat = '.2f',
      zeroline = F
    )
  )  

p