R Shiny中如何预定义plotly::subplot的高度和宽度
R Shiny中预定义plotly::subplot尺寸的方法
问题背景
在R语言的Shiny应用中使用plotly::subplot组合多图时,即便在renderPlotly中设置height和width参数,subplot的尺寸也不会更新,需要无需用户手动选择、直接在代码中预定义绘图尺寸的实现方式。
解决方法
需要同时调整UI层输出容器和服务端绘图函数的参数,两步即可实现:
- 在UI的
plotlyOutput函数中明确指定width和height参数,定义Shiny绘图容器的渲染尺寸 - 在调用
subplot()函数时传入对应的height和width参数,匹配容器尺寸即可生效
修正后完整代码
library(shiny) library(shinydashboard) library(shinythemes) library(shinyWidgets) library(fontawesome) library(tidyverse) library(plotly) # 定义应用最大上传文件大小 options(shiny.maxRequestSize=30*1024^2) ui = navbarPage("Title", theme = shinytheme("spacelab"), tabPanel("Interactive Plot", icon = icon("chart-area"), # 并排展示绘图 splitLayout( # 此处添加width和height参数定义容器尺寸,可根据需求修改数值 plotlyOutput(outputId = "Comparison_Plots", width = "1080px", height = "600px") ))) # 服务端逻辑 server = function(input, output) { output$Comparison_Plots = renderPlotly({ Group_1_2020 = data.frame(Code = c("A", "B", "C", "AA", "AAA", "AAAA", "BB", "BBB", "BBBB", "CC", "CCC", "CCCC"), Count_2020 = c(1,2,3,11,111,121,22,222,263,33,333,363)) Group_2_2020 = data.frame(Code = c("D", "E", "F", "DD", "DDD", "DDDD", "EE", "EEE", "EEEE", "FF", "FFF", "FFFF"), Count_2020 = c(4,5,6,14,24,34,45,55,65,76,86,96)) Group_1_2021 = data.frame(Code = c("A", "B", "C", "AA", "AAA", "AAAA", "BB", "BBB", "BBBB", "CC", "CCC", "CCCC"), Count_2021 = c(4, 8, 6,14,116,128,42,242,263,43,433,863 )) # 已补全原代码中缺失的1个数值,避免运行时报错 Group_2_2021 = data.frame(Code = c("D", "E", "F","DD", "DDD", "DDDD", "EE", "EEE", "EEEE", "FF", "FFF", "FFFF"), Count_2021 = c(8, 10, 12,44,64,85,105,125,96,46,136,156)) # 合并数据集 DF_Merged_1 = inner_join(Group_1_2020, Group_1_2021) DFF_Merged_1 = DF_Merged_1 %>% dplyr::select(Code, Count_2020, Count_2021) %>% gather(key = Type, value = Value, -Code) %>% mutate(Type = ifelse(Type == "Count_2020", "2020", "2021")) DF_Merged_2 = inner_join(Group_2_2020, Group_2_2021) DFF_Merged_2 = DF_Merged_2 %>% dplyr::select(Code, Count_2020, Count_2021) %>% gather(key = Type, value = Value, -Code) %>% mutate(Type = ifelse(Type == "Count_2020", "2020", "2021")) # 绘制ggplot对象 ggplot_1 = DFF_Merged_1 %>% ggplot(aes(x = reorder(Code,Value), y = Value, fill = Type, text = paste("Count:", Value, "<br>", "Offense Code:", Code, "<br>", "Year:", Type))) + geom_col(position = "dodge", show.legend = FALSE) + xlab("Offense Code") + ylab("Count") + ggtitle("Group 1 in Year 2020 and 2021") + theme(axis.text=element_text(size=8)) ggplot_2 = DFF_Merged_2 %>% ggplot(aes(x = reorder(Code,Value), y = Value, fill = Type, text = paste("Count:", Value, "<br>", "Offense Code:", Code, "<br>", "Year:", Type))) + geom_col(position = "dodge", show.legend = FALSE) + xlab("Offense Code") + ylab("Count") + ggtitle("Group 2 in Year 2020 and 2021") + theme(axis.text=element_text(size=8)) # 转换为交互式绘图 fig1 = ggplotly(ggplot_1, tooltip = "text") fig2 = ggplotly(ggplot_2, tooltip = "text") # 此处添加height和width参数匹配容器尺寸,数值和UI层保持一致即可 subplot(fig1, fig2, height = 600, width = 1080) }) } # 运行应用 shinyApp(ui = ui, server = server)
你可以根据实际需求自行调整代码中width和height的数值,UI层和subplot参数的数值保持一致即可保证subplot按预期尺寸渲染。
内容的提问来源于stack exchange,提问作者Ed_Gravy
相关产品推荐
相关产品推荐

