2017-11-14 6 views
4

Ich möchte die interaktive (das heißt, sie können mit Box/lasso Auswahl ausgewählt werden) Jitterbasierten Punkte auf dem boxplot gruppiert angezeigt werden. Ich kam aus dieser Frage heraus: Add jitter to box plot using markers in plotly. Ich möchte genau das gleiche, aber die Boxplots sollten gruppiert werden.Add-Jitter auf gruppierte Boxdiagramm mit Markern in R plotly

machte ich einen boxplot aber die Punkte sind alle durcheinander:

dat %>% 
    plot_ly(x = ~as.numeric(IC), 
      y = ~xval, 
      color = ~gene, 
      type = "box", 
      hoverinfo = "none", 
      boxpoints = FALSE 
     ) %>% 
    add_markers(x = ~jitter(as.numeric(IC)), 
       y = ~xval, 
       color = ~gene, 
       marker = list(size = 3), 
       hoverinfo = "text", 
       text = txt, 
       showlegend = TRUE) %>% 
layout(boxmode = "group") 

enter image description here

Wenn ich versuche, so dass die X-Achse um den Faktor gruppiert (so dass jede Kombination eine Ebene ist) kann ich nicht machen meine boxplot gruppiert:

dat <- dat %>% 
    mutate(gene_x_covariate = as.factor(
    paste0(get(facet_title), "-", gene))) 

dat %>% 
    plot_ly(x = ~as.numeric(gene_x_covariate), 
      y = ~xval, 
      color = ~gene, 
      type = "box", 
      hoverinfo = "none", 
      boxpoints = FALSE 
     ) %>% 
    add_markers(x = ~jitter(as.numeric(gene_x_covariate)), 
       y = ~xval, 
       color = ~gene, 
       marker = list(size = 3), 
       hoverinfo = "text", 
       text = txt, 
       showlegend = TRUE) %>% 
layout(boxmode = "group") 

enter image description here

Wenn ich versuche, die Variablen auf der X-Achse zu mischen, erhalte ich die Punkte weg von den Boxplots:

dat %>% 
    plot_ly(x = ~as.numeric(IC), 
      y = ~xval, 
      color = ~gene, 
      type = "box", 
      hoverinfo = "none" 
     ) %>% 
    add_markers(x = ~jitter(as.numeric(gene_x_covariate)), 
       y = ~xval, 
       color = ~gene, 
       marker = list(size = 3), 
       hoverinfo = "text", 
       text = txt, 
       showlegend = TRUE) %>% 
layout(boxmode = "group") 

enter image description here

Irgendwelche Ideen?

+0

Können Sie bitte einige Beispieldaten posten? – aocall

Antwort

1

Sie könnten ein ggplot2-Objekt erstellen und es dann interaktiv mit ggplotly() Funktion.

library(dplyr) 
library(ggplot2) 
library(plotly) 

dat <- data.frame(xval = sample(100,1000,replace = TRUE), 
       group1 = as.factor(sample(c("a","b","c"),1000,replace = TRUE)), 
       group2 = as.factor(sample(c("g1","g2","g3","g4"),1000, replace = TRUE))) 

p <- dat %>% ggplot(aes(x=group2, y=xval, fill=group1)) + 
       geom_boxplot() + geom_jitter() + facet_grid(~group2) 

ggplotly(p) %>% layout(boxmode = 'group') 
+0

Das funktioniert für mich, danke! – potockan

2


funktioniert boxpoints funktioniert für Sie? See this
Sie können diese Punkte verschieben, indem Sie Punktpos Parameter.

iris %>% 
    plot_ly(x = ~cut(Sepal.Length, breaks = 4), 
      y = ~Petal.Width, 
      color = ~Species, 
      type = "box", 
      marker = list(size = 10), 
      boxpoints = "all", 
      jitter = 0.4, 
      pointpos = 0, 
      hoverinfo = "all" 
) %>% layout(boxmode = "group") 
+0

Das funktioniert, aber ich will die jittered Punkte "mehr jitterte". Ist das möglich? Ich änderte den Wert von Jitter-Parameter, aber nicht viel ändert sich ... – potockan

+0

"Jitter" -Parameter ist ein Prozentsatz der Box Breite, so dass ich fürchte, dass diese einfache Lösung begrenzt ist. –

+0

Das Problem mit dieser Lösung ist, dass die Punkte nicht interaktiv sind und Sie die Punkte nicht auswählen können (mit Box/Lasso auswählen). Mit add_markers - Sie können. – potockan