在R 4.2.1中用Plotly 4.10.0绘制多3D散点子图堆叠问题求助
问题描述
我使用R 4.2.1及Plotly 4.10.0,想要将不同聚类算法的结果制作成6幅3D散点图组成的子图:
- 数据包含V1、V2、V3三个特征列,对应不同聚类标签(取值为1和2)
- 单独绘制单幅3D散点图时,能正常按标签用Set2色系区分标记点
- 但使用
subplot()整合6幅图时,所有图形堆叠到了同一幅图中,无法形成独立子图
我的代码如下:
### Atom true labels atom_true_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(atom_labels), colors="Set2") %>% add_markers(size=2) ### Atom km labels atom_km_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(atom_km$cluster), colors="Set2") %>% add_markers(size=2) ### Atom cl labels atom_cl_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(atom_cl$clustering), colors="Set2") %>% add_markers(size=2) ### Atom ag_avr labels atom_ag_avr_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(cutree(atom_ag_avr, k=groups_num)), colors="Set2") %>% add_markers(size=2) ### Atom ag_wrd labels atom_ag_wrd_labels <- plot_ly(atom, x=~V1, y=~V2,z=~V3, color=~factor(cutree(atom_ag_wrd, k=groups_num)), colors="Set2") %>% add_markers(size=2) ### Atom di labels atom_di_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(cutree(atom_di, k=groups_num)), colors="Set2") %>% add_markers(size=2) fig <- subplot(atom_true_labels, atom_km_labels, atom_cl_labels, atom_ag_avr_labels, atom_ag_wrd_labels, atom_di_labels) fig %>% layout(title="Atom data clustering")
解决方案
堆叠问题的核心原因是Plotly处理3D图时,默认共享坐标系,导致子图重叠。通过为每个子图配置独立坐标系、明确子图布局结构即可解决:
修改后的代码如下:
# 定义统一的3D坐标系配置,确保每个子图坐标轴一致 scene_config <- list( xaxis = list(title = "V1"), yaxis = list(title = "V2"), zaxis = list(title = "V3") ) ### Atom true labels atom_true_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(atom_labels), colors="Set2") %>% add_markers(size=2) %>% layout(scene = scene_config, title = "True Labels") ### Atom km labels atom_km_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(atom_km$cluster), colors="Set2") %>% add_markers(size=2) %>% layout(scene = scene_config, title = "K-Means") ### Atom cl labels atom_cl_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(atom_cl$clustering), colors="Set2") %>% add_markers(size=2) %>% layout(scene = scene_config, title = "CLARA") ### Atom ag_avr labels atom_ag_avr_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(cutree(atom_ag_avr, k=groups_num)), colors="Set2") %>% add_markers(size=2) %>% layout(scene = scene_config, title = "Agglomerative (Average)") ### Atom ag_wrd labels atom_ag_wrd_labels <- plot_ly(atom, x=~V1, y=~V2,z=~V3, color=~factor(cutree(atom_ag_wrd, k=groups_num)), colors="Set2") %>% add_markers(size=2) %>% layout(scene = scene_config, title = "Agglomerative (Ward)") ### Atom di labels atom_di_labels <- plot_ly(atom, x=~V1, y=~V2, z=~V3, color=~factor(cutree(atom_di, k=groups_num)), colors="Set2") %>% add_markers(size=2) %>% layout(scene = scene_config, title = "Diana") # 明确子图排列结构,关闭坐标轴共享 fig <- subplot( atom_true_labels, atom_km_labels, atom_cl_labels, atom_ag_avr_labels, atom_ag_wrd_labels, atom_di_labels, rows = 2, cols = 3, shareX = FALSE, shareY = FALSE, titleX = TRUE, titleY = TRUE ) # 调整整体布局标题和间距 fig %>% layout( title = list(text = "Atom data clustering", x = 0.5), margin = list(l = 50, r = 50, b = 50, t = 80) )
关键修改点
- 为每个子图添加独立的
scene配置,避免共享3D坐标系 - 在
subplot()中指定rows和cols,明确子图的排列方式(如2行3列) - 设置
shareX=FALSE和shareY=FALSE,确保每个子图坐标轴独立 - 为每个子图添加单独标题,方便区分不同聚类算法结果
内容的提问来源于stack exchange,提问作者Kamil Niewulis
相关产品推荐
相关产品推荐

