Search code examples
r3dplotly

Change the labels of a 3d plot using R plotly


I created following 3d plot using plotly in R.

 library(plotly)
    library(MASS)
    mu_v=c(0.5,0.4,0.5,0.2)
sigma_m=matrix(data = c(1,0.5,0.7,0.4,0.5,1,0.8,0.9,0.7,0.8,1,0.5,0.4,0.9,0.5,1),nrow = 4,byrow = T)
data1=mvrnorm(  10000,mu = mu_v , Sigma = sigma_m )
data1=as.data.frame(data1)
colnames(data1)=c("X1","X2","X3","X4")
plot_ly(x=~X1, y=~X2, z=~X3, type="scatter3d", color=~(X4), mode="markers",data = data1
          , marker = list(
             size    = 5,
             opacity = 10)) %>% layout(plot_bgcolor = "#bababa")

enter image description here

Is there a way to change the labels of axis and the coloring variable ? Also Can I change the background of the xy,yz and xz surface and increase the plotting area?

Update

I tried to edit the legend and change the background color as follows:

plot_ly(x=~X1, y=~X2, z=~X3, type="scatter3d", color=~(X4), mode="markers",data = data1
          , marker = list(
             size    = 5,
             opacity = 10)) %>% 
  layout(scene = list(legend = list(title = "legend"),plot_bgcolor='#e5ecf6'))

But it didnt work


Solution

  • Here is an option to address your first 2 questions (change x/y/z axis and color legend titles).

    plot_ly(
        x = ~X1, 
        y = ~X2, 
        z = ~X3, 
        type = "scatter3d", 
        mode = "markers",
        data = data1,
        marker = list(
            color = ~X4, 
            colorbar = list(title = "Legend"),    # Change colour legend title
            colorscale = "Blues",                 # Change colour palette
            size = 5,
            opacity = 10)) %>%
        layout(
            plot_bgcolor = "#bababa",
            scene = list(
                xaxis = list(title = "Axis 1"),   # Change x/y/z axis title
                yaxis = list(title = "Axis 2"),
                zaxis = list(title = "Axis 3")))
    

    enter image description here