如何在R-Markdown中实现Plotly绘图的网格布局对齐?
在R Markdown中实现Plotly绘图的网格布局
问题描述
请问是否可以在R-Markdown中实现Plotly绘图的对齐布局?目前我的Plotly绘图是逐个纵向排列,希望改为网格形式。此前我使用基础plot函数结合
layout(matrix(...))实现了该效果,但该方法似乎不适用于Plotly。以下是我之前使用基础plot函数实现网格布局的代码,请问能否适配Plotly实现相同效果?
用户原基础绘图代码:
{r comment = NA,fig.width=14, fig.height=14} layout(matrix(c(1,2,3,4,5,6,7,8), 4, 2, byrow = TRUE)) m_alt <- miete[miete$baujahr <= 22,] m_neu <- miete[miete$baujahr > 22,] plot(table(m_alt$bezirk),main="",ylab="Frequency",xlab="Bezirk") plot(table(m_neu$bezirk),main="",ylab="Frequency",xlab="Bezirk") plot(table(m_alt$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche") plot(table(m_neu$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche") plot(table(m_alt$wohnlage,m_alt$zimmerzahl),main="Altbau: Wohnlage u. Zimmerzahl") plot(table(m_neu$wohnlage,m_neu$zimmerzahl),main="Neubau: Wohnlage u. Zimmerzahl") plot(table(miete$bezirk,miete$zimmerzahl),main="Bezirk u. Zimmerzahl") plot(table(miete$warmwasser,miete$bezirk),main="Warmwasser u. Bezirk")
解决方案:使用Plotly的subplot()函数
Plotly自带的subplot()函数可以轻松实现多图网格布局,完全替代基础绘图的layout(matrix)方式。以下是适配你需求的完整代码:
{r comment = NA, fig.width=14, fig.height=14} library(plotly) # 数据处理逻辑保持不变 m_alt <- miete[miete$baujahr <= 22,] m_neu <- miete[miete$baujahr > 22,] # 将每个基础plot转换为Plotly图形 # 单变量频率图(柱状图) p1 <- plot_ly(x = names(table(m_alt$bezirk)), y = as.numeric(table(m_alt$bezirk)), type = "bar") %>% layout(xaxis = list(title = "Bezirk"), yaxis = list(title = "Frequency"), showlegend = FALSE) p2 <- plot_ly(x = names(table(m_neu$bezirk)), y = as.numeric(table(m_neu$bezirk)), type = "bar") %>% layout(xaxis = list(title = "Bezirk"), yaxis = list(title = "Frequency"), showlegend = FALSE) p3 <- plot_ly(x = names(table(m_alt$wohnflaeche)), y = as.numeric(table(m_alt$wohnflaeche)), type = "bar") %>% layout(xaxis = list(title = "Wohnfläche"), yaxis = list(title = "Frequency"), showlegend = FALSE) p4 <- plot_ly(x = names(table(m_neu$wohnflaeche)), y = as.numeric(table(m_neu$wohnflaeche)), type = "bar") %>% layout(xaxis = list(title = "Wohnfläche"), yaxis = list(title = "Frequency"), showlegend = FALSE) # 交叉表可视化(热力图示例,可替换为柱状图等) tab_alt <- table(m_alt$wohnlage, m_alt$zimmerzahl) p5 <- plot_ly(z = as.matrix(tab_alt), x = colnames(tab_alt), y = rownames(tab_alt), type = "heatmap") %>% layout(title = "Altbau: Wohnlage u. Zimmerzahl") tab_neu <- table(m_neu$wohnlage, m_neu$zimmerzahl) p6 <- plot_ly(z = as.matrix(tab_neu), x = colnames(tab_neu), y = rownames(tab_neu), type = "heatmap") %>% layout(title = "Neubau: Wohnlage u. Zimmerzahl") tab_bezirk_zimmer <- table(miete$bezirk, miete$zimmerzahl) p7 <- plot_ly(z = as.matrix(tab_bezirk_zimmer), x = colnames(tab_bezirk_zimmer), y = rownames(tab_bezirk_zimmer), type = "heatmap") %>% layout(title = "Bezirk u. Zimmerzahl") tab_warmwasser_bezirk <- table(miete$warmwasser, miete$bezirk) p8 <- plot_ly(z = as.matrix(tab_warmwasser_bezirk), x = colnames(tab_warmwasser_bezirk), y = rownames(tab_warmwasser_bezirk), type = "heatmap") %>% layout(title = "Warmwasser u. Bezirk") # 组合为4行2列的网格布局 subplot(p1, p2, p3, p4, p5, p6, p7, p8, nrows = 4, shareX = FALSE, shareY = FALSE, titleX = TRUE, titleY = TRUE) %>% layout(height = 1000, width = 1200) # 按需调整整体尺寸
关键说明
subplot()的nrows参数设置为4,自动分配为2列布局,和你基础绘图的matrix(c(1,2,3,4,5,6,7,8),4,2)逻辑一致- 若需要更灵活的子图位置控制,可以使用
layout(grid = list(...))自定义每个子图的domain范围,但subplot()足以满足常规网格需求 - 交叉表的可视化类型可根据需求调整,比如换成分组柱状图,只需要修改
type参数并调整数据格式即可
内容的提问来源于stack exchange,提问作者P.R.F.
相关产品推荐
相关产品推荐

