2
votes

This code makes a simple 3d scatter plot of the Fisher iris dataset, with an additional categorical variable added:


library(plotly)
roots <- factor(round(runif(n = dim(iris)[2],min = -.499,max = 2.499)))
my_iris <- cbind(data.frame(roots), iris)


plot_ly() %>%
  add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
            x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width, 
            color = ~Species,
            colors = c("red","blue","green")

  )


By looking at this help page: https://plot.ly/r/marker-style/

I was able to figure out that you can add an outline to the points like this:

plot_ly() %>%
  add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
            x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width, 
            color = ~Species,
            colors = c("#00FA9A34","#B22222dd","#00BFFFee"),
            marker = list(

              line = list(
                color = "#aabbffdd",
                width = 2
              )

            )
  )

Looking at this site https://plot.ly/r/reference/#scatter3d made the idea that lines are a property of scatter3d markers that in turn have the properties color and width make sense.

Now I attempt to map colors to the outlines based on my new roots variable,

plot_ly() %>%
  add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
            x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width,
            color = ~Species,
            colors = c("#00FA9A34","#B22222dd","#00BFFF66"),
            marker = list(

              line = list(
                color = ~roots,
                colors = c("#000000ff","#f00f3355","#dd22ccbb"),
                width = 2
              )

            )
  )

and it doesn't quite work: The first hex+alpha value I use should map to completely opaque black, but that is not one of the colors I get, and I would expect to see legend entries that describe the output.

So my question is: is there a way to do this aesthetic mapping? Perhaps instead of using add_trace I should use add_markers? Is there a way to do this in 2d scatters in Plotly R? Also would appreciate hints on how to learn Plotly for R properly as the documentation page I linked to above is a bit opaque and there seem to be fewer great resources to learn plotly than there are for ggplot2.

2

2 Answers

2
votes

it seems to me that plotly doesn't allow to specifying the colors of markers outline.

For the labels you can change the hover text. Or use annotations:

Maybe this doesn't solve your problem but I hope that be useful someway.

# Yours data

library(dplyr)
library(plotly)
roots <- as.character(round(runif(n = dim(iris)[2],min = -.499,max = 2.499)))
my_iris <- cbind(data.frame(roots), iris)


# Changing plot
my_iris %>%
plot_ly(type = 'scatter3d', mode = "markers") %>%
  add_trace(x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width,
            color = ~Species,
            colors = c("#00FA9A34","#B22222dd","#00BFFF66"),
            marker = list(
              line = list(
                color = ~roots,
                colors = c("#000000","#E35F13","#E3138B"),
                width = 6
              )
            ),
            hoverinfo = 'text',
            text = ~paste('</br> Species: ', Species,
                          '</br> Petal Length: ', Petal.Length,
                          '</br> Petal Width: ', Petal.Width,
                          '</br> Roots: ',roots)
            )

The Output:

image

0
votes

I was able to find a solution that I still think is inadequate: Set the variable that you are using to color the marker outlines to the colors you want to use themselves:

library(plotly)
roots <- round(runif(n = dim(iris)[1],min = -.499,max = 2.499))
roots_colors <- vector(mode="character", length=length(roots))
#setting  the value of roots colors to be the colors we want
roots_colors[roots == 0] <- "#000000ff"
roots_colors[roots == 1] <-  "#ff0000ff"
roots_colors[roots == 2] <-  "#0000ffff"

my_iris <- cbind(data.frame(roots_colors), iris)

plot_ly() %>%
  add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
            x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width,
            color = ~Species,
            colors = c("#00FA9A34","#B22222dd","#00BFFF66"),
            marker = list(

              line = list(
                color = ~roots_colors,
                width = 2
              )

            )
  )

We now have the appropriate colors that we want, but no legend describing them. Now I want to know how to make the legend reflect these colors.