修复 Plotly 图例位置并禁用 RMarkdown 中 Shiny 的 Plotly 面板
Fix Plotly legend position and disable Plotly panel for Shiny in RMarkdown
我正在使用 ggplot2 处理带有闪亮元素的 RMarkdown 报告,并使用 ggplotly 转换其图表。
在常规的 RMarkdown 报告中,一切都很完美。
ggplotly does not allow you to put a horizontal legend in the bottom, but I still managed to do it, 。
剧情现在看起来很棒
这个情节的代码是
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 是行不通的,我得到一个错误。
错误:没有适用于 'layout' 的方法应用于 class "c('theme', 'gg')"
的对象
反应性完美,但在 Shiny 中,我现在的情节在右侧有 Plotly 面板 + 图例,我不喜欢。
Shiny 代码如下所示
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))
如何将此功能附加到我近乎完美的情节中?
谢谢!
亲自解决
改动很小,我还是用的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)))
我正在使用 ggplot2 处理带有闪亮元素的 RMarkdown 报告,并使用 ggplotly 转换其图表。
在常规的 RMarkdown 报告中,一切都很完美。
ggplotly does not allow you to put a horizontal legend in the bottom, but I still managed to do it,
剧情现在看起来很棒
这个情节的代码是
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 是行不通的,我得到一个错误。
错误:没有适用于 'layout' 的方法应用于 class "c('theme', 'gg')"
的对象反应性完美,但在 Shiny 中,我现在的情节在右侧有 Plotly 面板 + 图例,我不喜欢。
Shiny 代码如下所示
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))
如何将此功能附加到我近乎完美的情节中?
谢谢!
亲自解决
改动很小,我还是用的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)))