2016-10-16 9 views
9

मैं r में plotly का उपयोग कर रहा हूं ताकि कई सबप्लॉट उत्पन्न हो सकें। एक खिलौना उदाहरण नीचे दिखाया गया है।सबप्लॉट्स के लिए प्लॉटली लीजेंड ग्रुप इसलिए एक एकल किंवदंती सभी चार्टों को नियंत्रित करता है

library(shiny) 
library(dplyr) 
library(plotly) 

## Toy Example 
ui <- fluidPage(
    h3("Diamonds"), 
    plotlyOutput("plot", height = 600) 
) 

server <- function(input, output, session) { 

    # reduce down the dataset to make the example simpler 
    dat <- diamonds %>% 
    filter(clarity %in% c("I1", "IF")) %>% 
    mutate(clarity = factor(clarity, levels = c("I1", "IF"))) 

    output$plot <- renderPlotly({ 

    # Generates the chart for a single clarity 
    byClarity <- function(df){ 

     Clarity <- df$clarity[1]; 

     plot_ly(df, x = ~carat, y = ~price, color = ~cut, name = ~clarity) %>% 
     add_trace(
      type="bar" 
      ## Also tried adding this with no success 
      # legendgroup = ~cut 
     ) %>% 
     layout(
      barmode = "stack" 
     ) 
    } 

    dat %>% 
     split(.$clarity) %>% 
     lapply(byClarity) %>% 
     subplot(nrows = NROW(.), shareX = TRUE, which_layout = "merge") 
    }) 
} 

shinyApp(ui, server) 

मैं किंवदंतियों ऐसी है कि पर एक पौराणिक कथा पर 'कट' पर क्लिक दिखाएगा/बनाने के लिए चाहते हैं छिपाने कि दोनों के बजाय सिर्फ इतना है कि कथा के साथ जुड़े चार्ट चार्ट से 'कट'।

DefaultVsIdeal

मैं legendgroup को देखा लेकिन समझ नहीं कैसे cut बजाय clarity से संबद्ध करना (clarity समूहीकरण मैं subplots बनाने के लिए उपयोग कर रहा हूँ है)।

मैं भी समाधान वहाँ अन्य plot_ly कार्यक्षमताओं मैं की जरूरत है कि ggplotly में उपलब्ध नहीं हैं के रूप में नहीं ggplotly कच्चे plot_ly साथ काम करते हैं और की जरूरत है।

किसी भी मदद की सराहना की जाएगी। मैं plotly_4.5.2, dplyr_0.5.0, और shiny_0.14 का उपयोग कर रहा हूं।

+0

आप plotly 4.0 के तहत कोशिश 'बेहतर अभ्यास और इस जवाब में above.' किया: ' https://stackoverflow.com/a/41122127/2296728 ' – urwaCFC

उत्तर

1

ठीक है, यहाँ एक समाधान ggplot2 उपयोग कर रहा है:

library(ggplot2) 
library(dplyr) 
library(plotly) 
dat <- diamonds %>% 
    filter(clarity %in% c("I1", "IF")) %>% 
    mutate(clarity = factor(clarity, levels = c("I1", "IF"))) 
# Function for nice labels 
k_label <- function(x) { 
c(0, paste0((x)/1000,"K")[-1]) 
} 
# ggplot 
p <- ggplot(dat,aes(x=carat, y=price, fill=cut)) + 
      geom_bar(stat="identity") + 
      facet_wrap(~clarity,nrow=2, scales = "free_y") + 
      scale_y_continuous(labels = k_label) + 
      theme_minimal() + ylab("") + xlab("") + 
      theme(legend.title=element_blank(), 
       panel.grid.major.x=element_blank()) 
# a plotly 
ggplotly(p) 

enter image description here

+0

धन्यवाद! दुर्भाग्यवश, मैं ऐसे समाधान की तलाश में हूं जो कच्चे 'plot_ly' के साथ काम करता है और 'ggplotly' नहीं है क्योंकि अन्य' plot_ly' कार्यक्षमताएं हैं जो मुझे चाहिए 'ggplotly' में उपलब्ध नहीं हैं ... मैं इस प्रश्न को अपडेट करूंगा यह स्पष्ट करो। – adilapapaya

+1

@adilapapaya मैं अभी भी सोच रहा हूं कि कम से कम 'आर' में यह संभव नहीं है (अब तक)। यह जांचने में मददगार हो सकता है कि यह 'पायथन' या 'matlab' में संभव है या नहीं। सॉफ्टवेयर के बीच कुछ अंतर हैं। – Jimbou

+0

@adilapapaya मुझे भी इस समस्या का सामना करना पड़ रहा है, क्या आप कभी भी समाधान के साथ आने में सक्षम थे? – Danny

0

दोनों निशान को legendgroup = ~cut जोड़ने और उनमें से एक के लिए showlegend = F सेट करके देखें। तब लेआउट में सेट showlegend = T

इस तरह:

plot_ly(df, x = ~carat, y = ~price, color = ~cut, name = ~clarity, legendgroup = ~cut, showlegend = T) %>% 
    add_trace(type="bar", legendgroup = ~cut, showlegend = F) %>% 
    layout(
     barmode = "stack",showlegend = T 
    ) 
+1

जब मैं ऐसा करता हूं, तो मैं पूरी तरह से किंवदंती खो देता हूं ... – adilapapaya

संबंधित मुद्दे