2017-06-18 3 views
0

Ich versuche, eine 3d-Linie zu einem 3D plot_ly Oberflächendiagramm hinzuzufügen. Wenn ich die Funktion add_lines() nicht innerhalb von plot_ly hinzufüge, sieht es gut aus. Sobald ich die add_lines hinzufüge, wird der Graph komplett durcheinander gebracht und das 3D-Liniendiagramm wird nicht hinzugefügt. Ich benutze plotly 4.7.0.R: Plot_ly 3D-Grafik mit Spurlinie

library(plotly) 
m_x = seq(-2, 2, .01) 
m_y = seq(-2, 2, .01) 
df = expand.grid(m_x, m_y) 
df['matrix'] = exp(-(df$Var1^2+df$Var2^2)) 
m_z = matrix(df$matrix, nrow = length(m_x), ncol = length(m_y)) 
m_df = list(m_x, m_y, m_z) 

x1 = seq(-2, 0, by=0.0202) 
y1 = runif(100, min=-0.03,max=0.03) 
z1 = exp(-(x1^2+y1^2)) 
df = data.frame(x1, y2, z2) 
names(df) <- c("df_x", "df_y", "df_z") 


colors = c("Blue", "Cyan", "Green", "Yellow", "Orange", "Red") 
p1 <- plot_ly(x = m_df[[1]], y = m_df[[2]], z = m_df[[3]], 
       colors = colors, color = m_df[[3]]) %>% add_surface() %>% 
    add_lines(x = df$df_x, y = df$df_y, z = df$df_z, data = df, 
      line = list(color = 'red', width = 1)) %>% 
    layout(title = "Hike_Example", 
     scene = list(aspectratio = list(x = 4, y = 4, z = 1))) 

p1 
+0

In 'data.frame (x1, y2, z2)' 'Objekte y2' und' z2' nicht –

Antwort

1

Hier ist der Code des Oberflächendiagramms mit der 3D-Linie.
Ich zeichnete die 3D-Linie mit add_trace anstelle von add_lines.

library(plotly) 
m_x = seq(-2, 2, .01) 
m_y = seq(-2, 2, .01) 
df = expand.grid(m_x, m_y) 
df['matrix'] = exp(-(df$Var1^2+df$Var2^2)) 
m_z = matrix(df$matrix, nrow = length(m_x), ncol = length(m_y)) 
m_df = list(m_x, m_y, m_z) 

x1 = seq(-2, 0, by=0.0202) 
y1 = runif(100, min=-0.03,max=0.03) 
z1 = exp(-(x1^2+y1^2)) 
df = data.frame(x1, y1, z1) 
names(df) <- c("df_x", "df_y", "df_z") 

colors = c("Blue", "Cyan", "Green", "Yellow", "Orange", "Red") 
p1 <- plot_ly(x = m_df[[1]], y = m_df[[2]], z = m_df[[3]], 
       colors = colors, color = m_df[[3]]) %>% add_surface() %>% 
    add_trace(x = ~df_x, y = ~df_y, z = ~df_z, data = df, 
      type="scatter3d", mode="lines", 
      line = list(color = "red", width = 4)) %>% 
    layout(title = "Hike_Example", 
     scene = list(aspectratio = list(x = 4, y = 4, z = 1))) 

p1 

enter image description here

+0

definiert sind, wenn ich die add_trace tun, es sieht aus wie es zwei Farbbalken erzeugt auf meine Handlung (eine für die 3D-Karte und eine für das Streudiagramm). Sie sind auch verschieden große Farbleisten. Gibt es eine Möglichkeit, die Farbleiste add_trace() zu ignorieren? – Mike

+0

@Mike Ich habe dein Bild gesehen. Es ist sehr seltsam, auf meinem Windows7 PC mit R 3.3.3 und plotly_4.5.6 habe ich dieses Problem nicht. Versuchen Sie die 'showscale = FALSE' Option in' add_trace' und/oder 'plot_ly' hinzuzufügen. Gib mir Bescheid. –

+0

Hinzufügen von 'showscale = False' zur' plot_ly' Anweisung funktioniert, danke! – Mike

Verwandte Themen