在RShiny报告的Rmd文件中无法并排显示两个ggplotly图表的技术求助
Hey there, I totally get why this is frustrating—you tried all the usual knitr figure parameters to get those two ggplotly charts side-by-side, but they just keep stacking vertically. The root issue here is that ggplotly generates interactive HTML widgets, not static ggplot images—so knitr's fig.show='hold', out.width, etc., don't affect them at all. These widgets are block-level elements by default, meaning they take up the full width of their container and stack on top of each other.
Let's walk through a couple of reliable fixes:
Solution 1: Use htmltools with Flexbox (Recommended)
We can use the htmltools package to create a flex container that forces the two charts to sit next to each other. Here's how to adjust your code:
{r echo=FALSE} library(htmltools) # First, create your two ggplotly objects separately (cleaner code!) plot1 <- ggplotly( qplot(params$sdr_v$Voltage_sensor, params$sdr_v$sdr, xlab= "Voltage sensors", ylab = "SDR(mV/day)") + geom_point(aes(x = params$sdr_v$Voltage_sensor, y= params$sdr_v$sdr)) + theme(axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0)) + theme(legend.position = "none", axis.text.x = element_blank()) ) plot2 <- ggplotly( qplot(params$sdr_s$Voltage_sensor, params$sdr_s$sdr_soc, xlab= "Voltage sensors", ylab = "SDR (%SOC/day)") + geom_point(aes(x = params$sdr_s$Voltage_sensor, y= params$sdr_s$sdr_soc, colour = params$sdr_s$sdr_soc > 0.016)) + scale_colour_manual(values = c("black", "red")) + theme(axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0)) + theme(legend.position = "none", axis.text.x = element_blank()) ) # Create a flex container to arrange them side-by-side div( style = "display: flex; gap: 1.5rem; justify-content: center;", div(plot1, style = "width: 48%;"), div(plot2, style = "width: 48%;") )
A quick breakdown of the CSS here:
display: flex: Turns the outer div into a flex container, which lets its children (the charts) arrange horizontallygap: 1.5rem: Adds a small space between the two charts so they don't look crampedwidth: 48%: Makes each chart take up almost half the container width (leaving a little room for the gap)justify-content: center: Centers the pair of charts in the container
Solution 2: Use bslib's Grid Layout (Great for Bootstrap Themes)
If your R Markdown document uses a Bootstrap theme (which is the default for html_document), the bslib package makes this even simpler with its layout_columns() function:
{r echo=FALSE} library(bslib) # Reuse plot1 and plot2 from Solution 1 above layout_columns( col_widths = c(6, 6), # Split Bootstrap's 12-column grid evenly between the two charts plot1, plot2 )
This method avoids writing custom CSS entirely and works seamlessly with Bootstrap's responsive design—your charts will even stack on smaller screens automatically.
Why Your Original Code Didn't Work
Just to clarify: Knitr's fig.* parameters (like fig.show='hold', out.width) are designed for static image outputs (PNGs, PDFs, etc.). Since ggplotly produces htmlwidgets (essentially chunks of HTML/JS), those knitr settings don't have any control over how they're laid out in the final HTML report. You need to use HTML/CSS-based layout tools instead.
内容的提问来源于stack exchange,提问作者cagla

