【问题标题】:Plot shrank or expanded when activating or deactivating legend keys after converting from ggplot2 to plotly从 ggplot2 转换为 plotly 后激活或停用图例键时绘图缩小或扩展
【发布时间】:2021-11-03 16:57:08
【问题描述】:

在下面的 Shiny 应用程序中,当我双击图例键以激活或停用它们时,情节会缩小或扩大。 我不确定这是否是 plotlyshiny 中的错误。 感谢任何帮助解决此问题。感谢阅读!

library(ggplot2)
library(plotly)
library(shiny)

# ui
ui <- bootstrapPage(
  plotlyOutput('rev')
)

# server
server <- function(input, output) {
  output$rev <- renderPlotly({
    
    p <- ggplot(mpg, aes(x = displ, y = hwy, color = factor(cyl), shape = class)) +
      geom_point(size = 3) +
      scale_color_manual(values = c("4" = "#abd9e9",
                                    "5" = "#2c7bb6",
                                    "6" = "#ff7f00",
                                    "8" = "#d7191c")) +
      scale_shape_manual(values = c(2, 5, 16, 17, 18, 19, 25)) +
      guides(color = guide_legend(title = "Cyl", order = 1),
             shape = guide_legend(title = "", order = 2)) +
      theme_classic(base_size = 16)
    p
    
    ggplotly(p) %>% 
      # layout(autosize = FALSE) %>%                        # did not work
      layout(xaxis = list(autorange = "reversed", 
                          tickvals = c(2, 3, 5, 6),
                          ticktext = c("2", "", "5", "6")))
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

reprex package (v2.0.1) 于 2021-09-06 创建

【问题讨论】:

    标签: r ggplot2 shiny plotly ggplotly


    【解决方案1】:

    我跳过了闪亮的部分,因为这个问题也以纯情节为主。问题似乎是空字符串。您可以考虑提交错误报告。

    p <- ggplot(mpg, aes(x = displ, y = hwy, color = factor(cyl), shape = class)) +
    geom_point(size = 3) +
        scale_color_manual(values = c("4" = "#abd9e9",
                                      "5" = "#2c7bb6",
                                      "6" = "#ff7f00",
                                      "8" = "#d7191c")) +
        scale_shape_manual(values = c(2, 5, 16, 17, 18, 19, 25)) +
        guides(color = guide_legend(title = "Cyl", order = 1),
               shape = guide_legend(title = "", order = 2)) +
        theme_classic(base_size = 16)
    p
    
    ggplotly(p) %>%
        layout(xaxis = list(autorange = "reversed",
                            tickvals = c(2, 3, 5, 6),
                            
                            # empty strings cause trouble, just include a space
                            ticktext = c("2", " ", "5", "6"))) 
    

    【讨论】:

    • 很棒的发现。感谢您的帮助!
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 2020-10-20
    • 2016-01-02
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多