2017-11-14 13 views
4

내가하고 싶은 대화 (즉, 그들은 상자/올가미 선택을 선택할 수 있습니다 의미) 지터 점이 상자 그림 그룹화 된 에 표시합니다. 나는이 질문에서 나온 것이다 : Add jitter to box plot using markers in plotly. 나는 정확히 같은 것을 원하지만 boxplots은 그룹화되어야합니다.추가 지터는 plotly

는 내가 상자 그림으나 포인트는 모두 혼합되어

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

은 내가 할 수 없습니다 (각 조합은 수평이되도록) 요인에 의해 그룹화 된 X 축을하려고 할 때 내 상자 그림 그룹화 :

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

,691 나는 X 축에 변수를 혼합 할 때

, 나는 상자 그림에서 멀리 포인트를 얻을 :

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

어떤 아이디어?

+0

일부 예제 데이터를 게시 할 수 있습니까? – aocall

답변

1

ggplot2 개체를 만든 다음 ggplotly() 함수를 사용하여 대화 형으로 만들 수 있습니다.

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

저, 고마워요! – potockan

2


박스 포인트은 당신을 위해 작동합니까? See this
이 포인트는 pointpos 매개 변수로 이동할 수 있습니다.

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

그게 효과가 있지만 jittered 포인트가 "더 jittered"싶습니다. 그게 가능하니? 지터 매개 변수의 값을 변경했지만 많이 변경하지 않았습니다 ... – potockan

+0

"지터"매개 변수는 상자 너비의 백분율이므로이 간단한 솔루션이 제한적입니다. –

+0

해당 솔루션의 문제점은 점이 대화식이 아니며 점을 선택할 수 없다는 것입니다 (상자/올가미 선택 포함). add_markers를 사용하면됩니다. – potockan