2017-04-11 173 views
1

我中的R構建scatterplot具有下列數據:散點圖顏色和圖例基於標籤上的R(plotly)

df <- data.frame(
    produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10') 
    , categoria=c('A','B','C','A','B','A','B','C','A','C') 
    , qtd_reviews=runif(10,10,50) 
    , nota=runif(10,0,5) 
    , flag_presente=c('Presente', 'Presente', 'Presente','Ausente', 'Ausente', 'MP','MP', 'Ausente', 'Presente', 'MP') 
    , stringsAsFactors = F 
) 

然後,創建用於每個類別的一個dataframe(一個用於「A」 ,'B'和'C'),因爲我想爲每個類別創建一個下拉菜單。

我想爲flag_presente創造傳奇(「Presente」,「Ausente」,「MP」)有3種顏色,但是當我上添加add_trace()參數colorcolorsupdatemenus過濾不起作用。

下面是完整的代碼:

library("plotly") 

df <- data.frame(
    produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10') 
    , categoria=c('A','B','C','A','B','A','B','C','A','C') 
    , qtd_reviews=runif(10,10,50) 
    , nota=runif(10,0,5) 
    , flag_presente=c('Presente', 'Presente', 'Presente','Ausente', 'Ausente', 'MP','MP', 'Ausente', 'Presente', 'MP') 
    , stringsAsFactors = F 
) 

unq_categorias <- unique(df$categoria) 
for (ctg in unq_categorias) { 
    assign(
    paste0("df_", ctg), 
    subset(df, categoria==ctg) 
) 
} 


# Retorna o número maximo de reviews 
max_rev <- max(df$qtd_reviews) + 1 


###### 
# Gráfico 
p <- plot_ly() %>% 

    add_trace(data = df, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text', 
      text = ~paste('Produto: ', produto, 
          '</br>Categoria: ', categoria, 
          '</br>Qtd Avaliações: ', round(qtd_reviews, 1), 
          '</br>Nota Média: ', round(nota, 1))) %>% 

    add_trace(data = df_A, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text', 
      text = ~paste('Produto: ', produto, 
          '</br>Categoria: ', categoria, 
          '</br>Qtd Avaliações: ', round(qtd_reviews, 1), 
          '</br>Nota Média: ', round(nota, 1)), 
      visible = F) %>% 

    add_trace(data = df_B, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text', 
      text = ~paste('Produto: ', produto, 
          '</br>Categoria: ', categoria, 
          '</br>Qtd Avaliações: ', round(qtd_reviews, 1), 
          '</br>Nota Média: ', round(nota, 1)), 
      visible = F) %>% 

    add_trace(data = df_C, y = ~nota, x = ~qtd_reviews, type = 'scatter', mode = 'markers', hoverinfo = 'text', 
      text = ~paste('Produto: ', produto, 
          '</br>Categoria: ', categoria, 
          '</br>Qtd Avaliações: ', round(qtd_reviews, 1), 
          '</br>Nota Média: ', round(nota, 1)), 
      visible = F) %>% 



    layout(
    title = 'Título', 

    # Legend 
    legend = list(
     x = 0.6, y = 1.1, 
     bordercolor = "#000000", 
     borderwidth = 1, 
     orientation = 'h', 
     font = list(
     family = "sans-serif", 
     size = 12, 
     color = "#000") 
    ), 

    # Axes 
    yaxis = list(
     title='Nota Média', 
     zeroline = T, 
     rangemode = "tozero", 
     autorange = F, 
     range=list(0,5.1) 
    ), 

    xaxis = list(
     title='Quantidade de Avaliações', 
     zeroline = T, 
     rangemode = "tozero", 
     autorange = F, 
     range=list(0, max_rev) 
    ), 

    updatemenus = list(

     list(
     y = 0.8, 
     buttons = list(

      # Todas categorias 
      list(
      method = "restyle", 
      args = list("visible", list(TRUE, FALSE, FALSE, FALSE)), 
      label = "Todas" 
     ), 

      # Categoria A 
      list(
      method = "restyle", 
      args = list("visible", list(FALSE, TRUE, FALSE, FALSE)), 
      label = "Categoria A" 
     ), 

      # Categoria B 
      list(
      method = "restyle", 
      args = list("visible", list(FALSE, FALSE, TRUE, FALSE)), 
      label = "Categoria B" 
     ), 

      # Categoria C 
      list(
      method = "restyle", 
      args = list("visible", list(FALSE, FALSE, FALSE, TRUE)), 
      label = "Categoria C" 
     ) 

     ) 
    ) 
    ) 

) %>% config(displayModeBar = F) 

ggplotly(p) 

其在葡萄牙,但它是沒有必要的理解代碼。

任何人都可以幫忙嗎?謝謝

回答

0

我得到了它(後Maximilian Peters答案),新增的3個痕跡每個類別的方式。每條痕跡代表flag_presente的等級。

然後,對於每個按鈕,我們需要列出9個TRUEFALSE

它是這樣的:

library("plotly") 

df <- data.frame(produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10'), 
       categoria=c('A','B','C','A','B','A','B','C','A','C'), 
       qtd_reviews=runif(10,10,50), 
       nota=runif(10,0,5), 
       flag_presente=c('P', 'P', 'P','A', 'A', 'MP','MP', 'A', 'P', 'MP'), 
       stringsAsFactors = F 
) 

p <- plot_ly() 
for (i in unique(df$categoria)) { 

    # P 
    plot_df <- df[(df$categoria==i) & (df$flag_presente=='P'),] 
    p <- add_trace(p, 
       data = plot_df, 
       y = ~nota, 
       x = ~qtd_reviews, 
       type = 'scatter', 
       mode = 'markers', 
       marker = list(color = "red")) 

    # A 
    plot_df <- df[(df$categoria==i) & (df$flag_presente=='A'),] 
    p <- add_trace(p, 
       data = plot_df, 
       y = ~nota, 
       x = ~qtd_reviews, 
       type = 'scatter', 
       mode = 'markers', 
       marker = list(color = "green")) 

    # MP 
    plot_df <- df[(df$categoria==i) & (df$flag_presente=='MP'),] 
    p <- add_trace(p, 
       data = plot_df, 
       y = ~nota, 
       x = ~qtd_reviews, 
       type = 'scatter', 
       mode = 'markers', 
       marker = list(color = "blue")) 
} 


p <- layout(p, showlegend = F, 

      updatemenus = list(
       list(
       y = 0.8, 
       buttons = list(
        list(
        method = "restyle", 
        args = list("visible", list(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)), 
        label = "All" 
       ), 

        list(
        method = "restyle", 
        args = list("visible", list(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)), 
        label = "Category A" 
       ), 

        list(
        method = "restyle", 
        args = list("visible", list(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE)), 
        label = "Category B" 
       ), 

        list(
        method = "restyle", 
        args = list("visible", list(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE)), 
        label = "Category C" 
       ) 
       ) 
      ) 
      ) 
) 

p 

我們還需要注意到,傳說是基於每一個痕跡,所以它是更好地隱藏。

1

您可以通過在循環中添加每個categoria來簡化整個代碼。然後你會得到「免費」的傳說。

p <- plot_ly() 
for (i in unique(df$categoria)) { 
    p <- add_trace(p, 
       data = df[df$categoria==i,], 
       y = ~nota, 
       x = ~qtd_reviews, 
       type = 'scatter', 
       mode = 'markers') 
} 

這也簡化了你的updatemenus

enter image description here

完整,減少代碼

library("plotly") 

df <- data.frame(produto=c('P1','P2','P3','P4','P5','P6','P7','P8','P9','P10'), 
       categoria=c('A','B','C','A','B','A','B','C','A','C'), 
       qtd_reviews=runif(10,10,50), 
       nota=runif(10,0,5), 
       stringsAsFactors = F 
) 

p <- plot_ly() 
for (i in unique(df$categoria)) { 
    p <- add_trace(p, 
       data = df[df$categoria==i,], 
       y = ~nota, 
       x = ~qtd_reviews, 
       type = 'scatter', 
       mode = 'markers') 
} 


p <- layout(p, 
    updatemenus = list(
    list(
     y = 0.8, 
     buttons = list(
     list(
      method = "restyle", 
      args = list("visible", list(TRUE, TRUE, TRUE)), 
      label = "All" 
     ), 

     list(
      method = "restyle", 
      args = list("visible", list(TRUE, FALSE, FALSE)), 
      label = "Category A" 
     ), 

     list(
      method = "restyle", 
      args = list("visible", list(FALSE, TRUE, FALSE)), 
      label = "Category B" 
     ), 

     list(
      method = "restyle", 
      args = list("visible", list(FALSE, FALSE, TRUE)), 
      label = "Category C" 
     ) 
    ) 
    ) 
) 
) 

p 
+0

我感到困惑,但這不是我想要的。我希望這個圖例基於'flag_presente'。對不起,如果問題很混亂,我會嘗試編輯以簡化它。 – TheBiro