gpt4 book ai didi

r - 使用 R 中的plotly 进行 3D 绘图

转载 作者:行者123 更新时间:2023-12-02 02:13:08 26 4
gpt4 key购买 nike

我有 2 个数据框:

df1

           SeqTech      NMDS1       NMDS2        NMDS3         C1           C2          C3
AM.AD.1 Sanger -1.2408789 0.39893503 -0.036690753 -1.0330785 -0.009904179 -0.06261568
AM.AD.2 Sanger -0.9050894 0.55943858 -0.121985899 -1.0330785 -0.009904179 -0.06261568
AM.F10.T1 Sanger -0.9059108 0.09466239 -0.033827792 -1.0330785 -0.009904179 -0.06261568
AM.F10.T2 Sanger -0.8511172 0.21396548 -0.061612450 -1.0330785 -0.009904179 -0.06261568
DA.AD.1 Sanger -1.1390353 0.05166118 0.306245704 -1.0330785 -0.009904179 -0.06261568
DA.AD.1T Sanger -1.2072895 0.06963215 0.241758582 -1.0330785 -0.009904179 -0.06261568
DA.AD.2 Sanger -1.1279367 -0.18692443 -0.092967153 -1.0330785 -0.009904179 -0.06261568
DA.AD.3 Sanger -1.3517083 -0.03651835 0.008165075 -1.0330785 -0.009904179 -0.06261568
DA.AD.3T Sanger -1.2616186 -0.06099534 -0.016942073 -1.0330785 -0.009904179 -0.06261568
DA.AD.4 Sanger -1.2700349 0.10824017 0.150317712 -1.0330785 -0.009904179 -0.06261568
TS1_V2 Pyro454 0.2094447 -0.53605610 0.154892961 0.2750404 0.002636827 0.01667041
TS10_V2 Pyro454 0.3966404 -0.10453275 -0.016794425 0.2750404 0.002636827 0.01667041
TS100_V2 Pyro454 0.2409404 -0.19935538 -0.095123709 0.2750404 0.002636827 0.01667041
TS101.2_V2 Pyro454 0.3576462 0.78659670 -0.393325760 0.2750404 0.002636827 0.01667041
TS103_V2 Pyro454 0.6018257 -0.19066765 0.017434390 0.2750404 0.002636827 0.01667041
TS104_V2 Pyro454 0.2554765 -0.33614221 -0.009296729 0.2750404 0.002636827 0.01667041
TS105_V2 Pyro454 0.2898261 0.74827877 -0.531568414 0.2750404 0.002636827 0.01667041
TS106_V2 Pyro454 0.3539052 0.17369739 -0.181935984 0.2750404 0.002636827 0.01667041
TS107_V2 Pyro454 0.0385423 0.10432457 0.465820636 0.2750404 0.002636827 0.01667041
TS109_V2 Pyro454 0.2525936 -0.33896215 -0.173134963 0.2750404 0.002636827 0.01667041

df2

 SeqTech         C1           C2          C3
1 Pyro454 0.2750404 0.002636827 0.01667041
2 Sanger -1.0330785 -0.009904179 -0.06261568

我用它来制作 2D 绘图,例如:

ggplot(df1, aes_string(x = "NMDS1", y = "NMDS2" )) +
geom_vline(xintercept = 0, linetype="dashed", size = 0.25, color= "#999999") +
geom_hline(yintercept = 0, linetype="dashed", size = 0.25, color= "#999999") +
# sites
geom_point(size = 2, alpha=0.3, aes_string(color= "SeqTech")) +
#Centroids
geom_point(data = df2, aes_string(x="C1", y="C2", color=df2[,1]), size = 1, alpha=0.7, shape=19 ) +
# Lines
geom_segment(data = df1, aes_string(xend = "C1", yend = "C2", colour=df1$SeqTech ), alpha=0.3, linetype= "dashed" )

结果是 enter image description here

所以如果我想用plotly制作类似的东西,但用2D生成3D图。我有制作 3D 的代码,但我不知道如何连接点并生成中心点。

plotly::plot_ly(
x=df1[,2],
y=df1[,3],
z=df1[,4],
type="scatter3d",
mode="markers",
marker=list(size=3, opacity = 0.7),
color=df1[, "SeqTech"],
hovertemplate = paste(df1[,"SeqTech"]))

如何在plotly中添加类似于geom_segment的内容?

非常感谢!!!

dputdf1

structure(list(Name = c("AM.AD.1", "AM.AD.2", "AM.F10.T1", "AM.F10.T2", 
"DA.AD.1", "DA.AD.1T", "DA.AD.2", "DA.AD.3", "DA.AD.3T", "DA.AD.4",
"TS1_V2", "TS10_V2", "TS100_V2", "TS101.2_V2", "TS103_V2", "TS104_V2",
"TS105_V2", "TS106_V2", "TS107_V2", "TS109_V2"), SeqTech = c("Sanger",
"Sanger", "Sanger", "Sanger", "Sanger", "Sanger", "Sanger", "Sanger",
"Sanger", "Sanger", "Pyro454", "Pyro454", "Pyro454", "Pyro454",
"Pyro454", "Pyro454", "Pyro454", "Pyro454", "Pyro454", "Pyro454"
), NMDS1 = c(-1.2408789, -0.9050894, -0.9059108, -0.8511172,
-1.1390353, -1.2072895, -1.1279367, -1.3517083, -1.2616186, -1.2700349,
0.2094447, 0.3966404, 0.2409404, 0.3576462, 0.6018257, 0.2554765,
0.2898261, 0.3539052, 0.0385423, 0.2525936), NMDS2 = c(0.39893503,
0.55943858, 0.09466239, 0.21396548, 0.05166118, 0.06963215, -0.18692443,
-0.03651835, -0.06099534, 0.10824017, -0.5360561, -0.10453275,
-0.19935538, 0.7865967, -0.19066765, -0.33614221, 0.74827877,
0.17369739, 0.10432457, -0.33896215), NMDS3 = c(-0.036690753,
-0.121985899, -0.033827792, -0.06161245, 0.306245704, 0.241758582,
-0.092967153, 0.008165075, -0.016942073, 0.150317712, 0.154892961,
-0.016794425, -0.095123709, -0.39332576, 0.01743439, -0.009296729,
-0.531568414, -0.181935984, 0.465820636, -0.173134963), C1 = c(-1.0330785,
-1.0330785, -1.0330785, -1.0330785, -1.0330785, -1.0330785, -1.0330785,
-1.0330785, -1.0330785, -1.0330785, 0.2750404, 0.2750404, 0.2750404,
0.2750404, 0.2750404, 0.2750404, 0.2750404, 0.2750404, 0.2750404,
0.2750404), C2 = c(-0.009904179, -0.009904179, -0.009904179,
-0.009904179, -0.009904179, -0.009904179, -0.009904179, -0.009904179,
-0.009904179, -0.009904179, 0.002636827, 0.002636827, 0.002636827,
0.002636827, 0.002636827, 0.002636827, 0.002636827, 0.002636827,
0.002636827, 0.002636827), C3 = c(-0.06261568, -0.06261568, -0.06261568,
-0.06261568, -0.06261568, -0.06261568, -0.06261568, -0.06261568,
-0.06261568, -0.06261568, 0.01667041, 0.01667041, 0.01667041,
0.01667041, 0.01667041, 0.01667041, 0.01667041, 0.01667041, 0.01667041,
0.01667041)), row.names = c(NA, -20L), class = "data.frame")

最佳答案

您需要以某种格式准备数据以输入到绘图中,以便绘图地知道哪个点是哪个点,以及哪个点应该用线连接。下面是实现它的一种方法。

library(plotly)
library(dplyr)

# create a data_frame with two record per Name/SeqTech
# This will be provided to plotly for line drawing later
plotly_data <- bind_rows(
data %>% select(Name, SeqTech, x = NMDS1, y = NMDS2, z= NMDS3),
data %>% select(Name, SeqTech, x = C1, y = C2, z= C3)) %>%
# define the group variable which will be used by plotly for line drawing
group_by(Name)

# Here is what the data look like
plotly_data %>% arrange(Name) %>% head(20)
#> Name SeqTech x y z
#> 1 AM.AD.1 Sanger -1.2408789 0.398935030 -0.036690753
#> 2 AM.AD.1 Sanger -1.0330785 -0.009904179 -0.062615680
#> 3 AM.AD.2 Sanger -0.9050894 0.559438580 -0.121985899
#> 4 AM.AD.2 Sanger -1.0330785 -0.009904179 -0.062615680
#> 5 AM.F10.T1 Sanger -0.9059108 0.094662390 -0.033827792
#> 6 AM.F10.T1 Sanger -1.0330785 -0.009904179 -0.062615680
#> 7 AM.F10.T2 Sanger -0.8511172 0.213965480 -0.061612450
#> 8 AM.F10.T2 Sanger -1.0330785 -0.009904179 -0.062615680
#> 9 DA.AD.1 Sanger -1.1390353 0.051661180 0.306245704
#> 10 DA.AD.1 Sanger -1.0330785 -0.009904179 -0.062615680
#> 11 DA.AD.1T Sanger -1.2072895 0.069632150 0.241758582
#> 12 DA.AD.1T Sanger -1.0330785 -0.009904179 -0.062615680
#> 13 DA.AD.2 Sanger -1.1279367 -0.186924430 -0.092967153
#> 14 DA.AD.2 Sanger -1.0330785 -0.009904179 -0.062615680
#> 15 DA.AD.3 Sanger -1.3517083 -0.036518350 0.008165075
#> 16 DA.AD.3 Sanger -1.0330785 -0.009904179 -0.062615680
#> 17 DA.AD.3T Sanger -1.2616186 -0.060995340 -0.016942073
#> 18 DA.AD.3T Sanger -1.0330785 -0.009904179 -0.062615680
#> 19 DA.AD.4 Sanger -1.2700349 0.108240170 0.150317712
#> 20 DA.AD.4 Sanger -1.0330785 -0.009904179 -0.062615680

reprex package 于 2021 年 5 月 11 日创建(v2.0.0)

使用上面的数据绘制代码

# And here is the code for line & scatter point drawing with plotly
plotly::plot_ly(data = plotly_data %>% group_by(Name),
x= ~x,
y= ~y,
z= ~z,
# as the group is defined in the data the name can be define by SeqTech
name = ~SeqTech,
type="scatter3d",
mode="lines+markers",
marker=list(size=3, opacity = 0.7),
color= ~SeqTech,
hovertemplate = ~SeqTech)

enter image description here

关于r - 使用 R 中的plotly 进行 3D 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67481798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com