R Shiny:图例在使用 renderPlotly 时分裂

问题描述 投票:0回答:1

我注意到一个奇怪的现象,renderPlotly可以拆分同一个变量的图例

例如,当使用 renderPlot() 时,生成的图形如下所示: 但是,如果我试图将它转换成一个 plotly 对象,它就会变成这样:

有谁知道发生了什么以及如何解决这个问题?谢谢你! ggplot 版本:

library(shiny)
library(plotly)

ColorblindnessFriendlyValues <- c("Same" = "#648FFF", "Alt" = "#FFB000")

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("crossloadings", "Number Crossloadings",
                  min = 1, max = 10, value = 5),
      sliderInput("group1", "Group 1",
                  min = 0, max = 1, value = 0.5),
      sliderInput("group2", "Group 2",
                  min = 0, max = 1, value = 0.5)
    ),
    mainPanel(
      plotlyOutput("plot") # Changed here
    )
  )
)

server <- function(input, output) {
  output$plot <- renderPlotly({
    number_crossloadings <- seq(1, input$crossloadings)
    group1 <- runif(input$crossloadings, min = 0, max = input$group1)
    group2 <- runif(input$crossloadings, min = 0, max = input$group2)

    results <- data.frame(number_crossloadings, group1, group2)

    plot <- ggplot(data=results, aes(x=number_crossloadings))+
      geom_line(aes(y=group1,
                    color="Same"))+
      geom_line(aes(y=group2,color="Alt"))+
      suppressWarnings(geom_point(aes(y=group1,
                                      color="Same",
                                      shape = "Same",
                                      text = paste0("# of cross Loadings: ", number_crossloadings,
                                                    "<br>SRMR: ", sprintf('%.3f', group1)))))+
      suppressWarnings(geom_point(aes(y=group2,
                                      color="Alt",
                                      shape = "Alt",
                                      text = paste0("# of cross Loadings: ", number_crossloadings,
                                                    "<br>SRMR: ", sprintf('%.3f', group2)))))+
      scale_color_manual(values = ColorblindnessFriendlyValues, labels = c("Same", "Alt")) +
      scale_shape_manual(values = c("Same" = 16, "Alt" = 17), labels = c("Same", "Alt")) +
      geom_abline(color="grey",slope=0, intercept=0.08) +
          labs(color = "Legend", shape = "Legend")  +
      ylim(NA,1)

      ggplotly(plot,tooltip = c("text"))

    })
}

shinyApp(ui, server)

剧情版本:

library(shiny)
library(plotly)

ColorblindnessFriendlyValues <- c("Same" = "#648FFF", "Alt" = "#FFB000")

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("crossloadings", "Number Crossloadings",
                  min = 1, max = 10, value = 5),
      sliderInput("group1", "Group 1",
                  min = 0, max = 1, value = 0.5),
      sliderInput("group2", "Group 2",
                  min = 0, max = 1, value = 0.5)
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output) {
  output$plot <- renderPlot({
    number_crossloadings <- seq(1, input$crossloadings)
    group1 <- runif(input$crossloadings, min = 0, max = input$group1)
    group2 <- runif(input$crossloadings, min = 0, max = input$group2)

    results <- data.frame(number_crossloadings, group1, group2)

    plot <- ggplot(data=results, aes(x=number_crossloadings))+
      geom_line(aes(y=group1, color="Same"))+
      geom_line(aes(y=group2,color="Alt"))+
      suppressWarnings(geom_point(aes(y=group1,
                                      color="Same",
                                      shape = "Same",
                                      text = paste0("# of cross Loadings: ", number_crossloadings,
                                                    "<br>SRMR: ", sprintf('%.3f', group1)))))+
      suppressWarnings(geom_point(aes(y=group2,
                                      color="Alt",
                                      shape = "Alt",
                                      text = paste0("# of cross Loadings: ", number_crossloadings,
                                                    "<br>SRMR: ", sprintf('%.3f', group2)))))+
      scale_color_manual(values = ColorblindnessFriendlyValues, labels = c("Same", "Alt")) +
      scale_shape_manual(values = c("Same" = 16, "Alt" = 17), labels = c("Same", "Alt")) +
      geom_abline(color="grey",slope=0, intercept=0.08) +
      labs(color = "Legend", shape = "Legend")  +
      ylim(NA,1)

    plot
  })
}

shinyApp(ui, server)
r shiny plotly ggplotly
1个回答
0
投票

每当翻译

ggplot
<->
plotly
没有产生我想要的结果时,我直接使用
plot_ly
,因为它允许更好的控制。

话虽如此,您可以像这样使用

plot_ly
生成类似的图(N.B. 我稍微更改了您的数据结构以避免重复):

output$plot <- renderPlotly({
   number_crossloadings <- seq(1, input$crossloadings)
   group1 <- runif(input$crossloadings, min = 0, max = input$group1)
   group2 <- runif(input$crossloadings, min = 0, max = input$group2)
   results <- data.frame(x = rep(number_crossloadings, 2),
                         y = c(group1, group2),
                         g = rep(c("Same", "Alt"), each = input$crossloadings))
   plot_ly(results, 
           x = ~ x, 
           y = ~ y, 
           colors = ColorblindnessFriendlyValues, 
           symbols = c("triangle-up",  "circle"))  %>% 
      add_trace(type = "scatter", 
                mode = "lines", 
                showlegend = FALSE, 
                hoverinfo = "none",
                color = I("gray"), 
                x = range(results$x) + c(-1, 1) * .2, 
                y = c(.08, .08)) %>% 
      add_trace(type = "scatter", 
                mode = "markers+lines", 
                color = ~ g, 
                symbol = ~ g, 
                marker = list(size = 8),
                hoverinfo = "text",
                text = ~ paste0("# of cross Loadings: ", number_crossloadings,
                                "<br>SRMR: ", sprintf("%.3f", y))) %>% 
      layout(legend = list(title = list(text = "Legend")),
             xaxis = list(title = list(text = "number_crossloadings")),
             yaxis = list(title = list(text = "y"))
})

阿布林

我为

abline
使用了单独的跟踪,另一种选择是将
shape
添加到
layout
,如下所示:

layout(
   #...
   shapes = list(
      list(
         type = "rect",
         x0 = 0,
         x1 = 1,
         xref = "paper",
         y0 = 0.08,
         y1 = 0.08,
         yref = "y",
         line = list(dash = "solid",
                     color = "grey"),
         layer = "below"
      )
   )
)
© www.soinside.com 2019 - 2024. All rights reserved.