如何在R语言的plotly中移除网格线并设置透明背景?
Plotly 3D散点图自定义样式操作指南
Plotly完全支持主题、背景色、网格线的自定义配置,无需切换其他包即可实现无网格线透明背景效果,具体修改方法如下:
核心修改逻辑
所有3D场景相关的样式都可以在layout的scene参数中配置:
- 移除坐标轴网格线:给x/y/z三个轴分别添加
showgrid = FALSE参数 - 设置3D场景透明背景:在scene层级添加
bgcolor = "rgba(0,0,0,0)",rgba最后一位参数代表透明度,取值0-1,0为完全透明 - 可选移除坐标轴线条:给三个轴添加
showline = FALSE - 可选设置画布整体透明背景:在layout层级添加
paper_bgcolor = "rgba(0,0,0,0)"
修改后的完整代码
library(plotly) mtcars$am[which(mtcars$am == 0)] <- 'Automatic' mtcars$am[which(mtcars$am == 1)] <- 'Manual' mtcars$am <- as.factor(mtcars$am) fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E')) fig <- fig %>% add_markers() fig <- fig %>% layout( # 整体画布背景透明 paper_bgcolor = "rgba(0,0,0,0)", scene = list( # 3D场景背景透明 bgcolor = "rgba(0,0,0,0)", xaxis = list( title = 'Weight', showgrid = FALSE, # 移除x轴网格线 showline = FALSE # 可选移除x轴线 ), yaxis = list( title = 'Gross horsepower', showgrid = FALSE, # 移除y轴网格线 showline = FALSE # 可选移除y轴线 ), zaxis = list( title = '1/4 mile time', showgrid = FALSE, # 移除z轴网格线 showline = FALSE # 可选移除z轴线 ) ) ) fig
内容的提问来源于stack exchange,提问作者sheep
相关产品推荐
相关产品推荐

