【问题标题】:Fix Plotly legend position and disable Plotly panel for Shiny in RMarkdown修复 Plotly 图例位置并禁用 RMarkdown 中 Shiny 的 Plotly 面板
【发布时间】:2019-07-08 18:14:48
【问题描述】:

我正在使用闪亮元素制作 RMarkdown 报告,使用 ggplot2 并使用 ggplotly 转换其图表。 在常规的 RMarkdown 报告中,一切都很完美。 ggplotly does not allow 你在底部放一个水平图例,但我还是设法做到了,using this answer

情节现在看起来很棒

这个情节的代码是

semiformal_savings_chart <- ggplot(semiformal_savings, aes(x = Country, y = 
Percent, fill = Category)) + 
geom_bar(stat = 'identity', width = 0.7) +      
theme_tufte() +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
ylim(0, 80) + 
theme(legend.text = element_text(size = 12)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 12)) +
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm")) 

ggplotly(semiformal_savings_chart) %>% config(displayModeBar = F)  %>%
layout(legend = list(orientation = "h", x = 0.4, y = -0.2))

当我把它变成 Shiny 格式时,我不得不用 ggplotly 删除最后两行代码,这会禁用 Plotly 面板。仅使用管道将其连接到 ggplot 不起作用,并且出现错误。

错误:没有适用于“布局”的方法应用于“c('theme', 'gg')”类的对象

反应性完美,但在 Shiny 中,我现在的情节右侧有 Plotly 面板 + 图例,我不喜欢。

闪亮的代码看起来像这样

semiformal_savings_data <- reactive({
filter(semiformal_savings,
       Country %in% input$Country)

selectInput(inputId = "Country", label = "Please select a 
country/countries", choices = unique(semiformal_savings$Country), 
            selected = unique(semiformal_savings$Country), multiple = TRUE)
plotlyOutput("semiformalsavingsPlot")

output$semiformalsavingsPlot <- renderPlotly({
ggplot(semiformal_savings_data(), aes(x = Country, y = Percent, fill = 
Category)) + geom_bar(stat = 'identity', width = 0.7) +      
theme_tufte() +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
ylim(0, 80) + 
theme(legend.text = element_text(size = 12)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 12)) +
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm")) 
})

但这部分现在不见了

%>% config(displayModeBar = F) %>% layout(legend = list(orientation = "h", 
x = 0.4, y = -0.2))

如何将此功能附加到我几乎完美的情节?

谢谢!

【问题讨论】:

  • 请提供数据以重现这些图。

标签: r ggplot2 shiny plotly r-plotly


【解决方案1】:

个人解决

改动很小,我还是用ggplotly()

g1 <- ggplot(ownership_data(), aes(x = as.character(Year), y = Percent, fill = 
Category)) + 
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Country) +
theme_tufte() +
ylim(0, 100) +
scale_fill_brewer(palette = "Paired") +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(legend.text = element_text(size = 13)) +
theme(axis.text.x = element_text(size = 12)) +
theme(axis.text.y = element_text(size = 13)) +
theme(strip.text.x = element_text(size = 12)) +
theme(legend.position = "bottom") +
theme(legend.title = element_blank()) 

return(ggplotly(g1, tooltip = c("y", "text")) %>% config(displayModeBar = F)  %>%
layout(legend = list(orientation = "h", x = 0.4, y = -0.2)))

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2015-07-08
    • 2021-07-26
    • 2021-07-30
    • 2017-10-20
    • 1970-01-01
    相关资源
    最近更新 更多