plot in 3D with plotly in R

abraham

I have 2 data frames:

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

and I use it to make a 2D plot like:

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" )

it result in enter image description here

so if I want to make something similar with plotly but instead 2D generate a 3D plot. I have a code to make a 3D but I don't know how to connect the dots and generate a central point.

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"])) 

how can I add something similar to geom_segment in plotly ?

Thanks so much !!!

dput of df1

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")
Sinh Nguyen

You will need to prepare the data in certain format to feed into plotly so plotly know which point is which, and which point should be connected by a line. Below is a way to achieve it.

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

Created on 2021-05-11 by the reprex package (v2.0.0)

Plotly code using data above

# 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive