2016-08-31 53 views
3

Ich versuche, zwei Donut-Plots Seite an Seite in Plotly zu bekommen. Ich bekomme jedoch nur ein Bild. Irgendwelche Ratschläge, was ich vermisse?R Plotly Zwei Plots nebeneinander

library(dplyr) 
library(plotly) 

df1 <- as.data.frame(matrix(data = c("a","b", "c", 34,28, 29), nrow = 3, ncol = 2)) 
colnames(df1) <- c("category", "count") 
df2 <- as.data.frame(matrix(data = c("Q","F", "G", 29,50, 76), nrow = 3, ncol = 2)) 
colnames(df2) <- c("group", "count") 


p <- subplot(
    plot_ly(df1, labels = category, values = count, type = "pie", hole = 0.6, showlegend = TRUE), 
    plot_ly(df2, labels = group, values = count, type = "pie", hole = 0.6, showlegend = TRUE), 
    margin = 0.05, 
    nrows = 2 
) 
p 

Stromausgang:

enter image description here

Antwort

4

Du bist so nah. Dies ist eine Anpassung des Codes Plotly's pie charts examples: (. Sie sind subplotadd_trace anstelle eines Elternteils mit)

plot_ly(df1, labels = category, values = count, type = "pie", hole = 0.6, showlegend = TRUE, 
     domain = list(x = c(0, 1), y = c(0.5, 1))) %>% 
    add_trace(data = df2, labels = group, values = count, type = "pie", hole = 0.6, showlegend = TRUE, 
      domain = list(x = c(0, 1), y = c(0, 0.5))) 

side-by-side pies

Wenn Sie Rasterlinien hinzufügen (pro @ Technophobe01 dem Beispiel) würden Sie hinzufügen

ax <- list(showline= TRUE) 
plot_ly(...) %>% add_trace(...) %.% 
    layout(xaxis = ax, yaxis = ax) 

Viele weitere Optionen dafür, sapmles auf ihre axes Seite.

+0

Mit 'domain' wie das von mir erinnert' par (Abb = ..., new = TRUE) 'in Base R. Scheint etwas drakonisch, aber es funktioniert. – r2evans

+0

Super, danke dafür. Warum funktioniert Subplot nicht? Die Unterlagen schlagen das vor? – user1357015

+0

Keine Ahnung, tut mir leid. (Können Sie eine der Antworten "akzeptieren"?) – r2evans

1

Wenn Sie r2evans Lösung nehmen können Sie es zu Ihrer Frage passen, wie folgt:

library(dplyr) 
library(plotly) 

df1 <- as.data.frame(matrix(data = c("a","b", "c", 34,28, 29), nrow = 3, ncol = 2)) 
colnames(df1) <- c("category", "count") 
df2 <- as.data.frame(matrix(data = c("Q","F", "G", 29,50, 76), nrow = 3, ncol = 2)) 
colnames(df2) <- c("group", "count") 


p2<- subplot(
    plot_ly(df1, labels = category, values = count, type = "pie", hole = 0.6, showlegend = TRUE, 
      domain = list(x = c(0, 0.5), y = c(0, 1))), 
    add_trace(data = df2, labels = group, values = count, type = "pie", hole = 0.6, 
       domain = list(x = c(0.5, 1), y = c(0, 1))), 
    margin = 0.05) 
p2 

Ergebnis:

enter image description here

+1

Ist es "subplot", das das Hintergrundraster hinzufügt? Ich konnte es ohne es nicht zur Arbeit bringen. (Übrigens: Ich habe "nrows = 2" vermisst, und Sie folgten nach ... :-) – r2evans

+0

Ja, da der Subplot das Hintergrundraster hinzufügt. ref: https://plot.ly/r/subplots/ – Technophobe01

+0

Kann mit ['layout'] (https://plot.ly/r/axes/) gemacht werden, obwohl ich in diesem Zusammenhang nicht sicher bin, ob es hinzugefügt wird viel (was bedeutet "0,0" wenn etwas außerhalb eines Kreisdiagramms?). – r2evans