【问题标题】:How to remove gridlines and have clear background for plotly in R?如何在 R 中删除网格线并为 plotly 提供清晰的背景?
【发布时间】:2021-12-17 18:49:31
【问题描述】:

我正在尝试使用 RStudio 创建 3D 绘图。 到目前为止,我制作的内容与此类似(代码来自https://plotly.com/r/3d-scatter-plots/)

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(scene = list(xaxis = list(title = 'Weight'),
                     yaxis = list(title = 'Gross horsepower'),
                     zaxis = list(title = '1/4 mile time')))

fig

但是,我还想编辑主题/背景。就像this 一样,我想要没有任何网格线的清晰背景。我知道这张图片可能是由scatter3D 生成的,所以我想知道 plot_ly 是否有类似的功能?像主题、bgcolor 等?如何删除网格线?

【问题讨论】:

    标签: r 3d plotly


    【解决方案1】:

    与讨论的 here 类似,在 layout() 中为 x、y 和 z 轴指定 showgrid = F 将删除所有网格线:

    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(scene = list(xaxis = list(title = 'Weight', showgrid = F),
                                       yaxis = list(title = 'Gross horsepower', showgrid = F),
                                       zaxis = list(title = '1/4 mile time', showgrid = F)))
    
    fig
    

    还可以在移除网格线后将 x、y 和 z 轴的线添加到绘图中,方法是在 layout() 中为每个 x、y 和 z 轴额外指定 showline = T:

    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(scene = list(xaxis = list(title = 'Weight', showgrid = F, showline = T),
                                       yaxis = list(title = 'Gross horsepower', showgrid = F, showline = T),
                                       zaxis = list(title = '1/4 mile time', showgrid = F, showline = T)))
    
    fig
    

    【讨论】:

    • 这成功了!非常感谢!
    • 很高兴它有帮助:)
    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2017-02-11
    相关资源
    最近更新 更多