##################################################### ### Lab Tutorial: Making a 3D Figure using Plotly ### ##################################################### toy.data <- read.csv("toy.data.csv", header=TRUE) head(toy.data) ######### Making a 3D plot with Plotly################## #Make sure these packages are installed: plotly and RColorBrewer ## Load libraries: library(plotly) #For making 3D plot library(RColorBrewer) # For assigning colors #Set colors, two methods: colors <- brewer.pal(6, "Accent") #Accent or Set2 seem to be the best #Manually set colors (useful when there are a lot of categories) colors <- c("springgreen", "gold", "red3", "lightskyblue", "darkmagenta", "midnightblue") #Assign each color to each unique "Bacteria" type colors <- colors[as.numeric(toy.data$Bacteria)] #Basic 3d plot with plotly p.1 <- plot_ly(toy.data, x = ~Factor1, y = ~Factor2, z = ~Factor3, color = ~Bacteria, #Each Taxa colors = colors ) %>% #Assign colors according to "colors" item above # Note: Use "%>%" to pipe from one section of plot attributes to another add_markers() # Add markers #Adjust the figure axes: # Add a title to axes (note these are made up) #and manually set minimum/maximum to account for different ranges of each axis, #so that they don't appear to be on the same scale p.2 <- plot_ly(toy.data, x = ~Factor1, y = ~Factor2, z = ~Factor3, color = ~Bacteria, #Each Taxa colors = colors ) %>% add_markers() %>% layout(scene = list(xaxis = list(title = 'Geographic range', range = c(0,20)), #"range" sets the min/max on axis yaxis = list(title = 'Size', range=c(0,10)), zaxis = list(title = 'Speed', range=c(0,20)), aspectratio = list(x=1, y=0.5, z=1))) #Sets the ratio of axis length #Now make the marker sizes proportional to a characteristic ("Point_size" column) p.3 <- plot_ly(toy.data, x = ~Factor1, y = ~Factor2, z = ~Factor3, color = ~Bacteria, #Each Taxa colors = colors , marker = list(sizeref=0.05), opacity=0.8, #Use sizeref if `marker.size` is set to a numerical array. Sets the scale factor used to determine the rendered size of marker points. #Opacity allows overlapping symbols to be visible. Value = 0-1. size = ~Point_size, sizemode="diameter") %>% #Setting size to value in column, sizemode can be diameter or area add_markers() %>% layout(scene = list(xaxis = list(title = 'Geographic range', range = c(0,20)), #"range" sets the min/max on axis yaxis = list(title = 'Size', range=c(0,10)), zaxis = list(title = 'Speed', range=c(0,20)), aspectratio = list(x=1, y=0.5, z=1))) #Sets the ratio of axis length ##Note on transparency: "opacity" argument better when everything's a different color, "alpha" if they're the same color ##Now set symbols based on a characteristic ##Shows some options for sizemode and symbol type p.4 <- plot_ly(toy.data, x = ~Factor1, y = ~Factor2, z = ~Factor3, color = ~Bacteria, #Each Taxa colors = colors , marker = list(sizeref=0.05), opacity=0.8, #Use sizeref if `marker.size` is set to a numerical array. Sets the scale factor used to determine the rendered size of marker points. #Opacity allows overlapping symbols to be visible. Value = 0-1. size = ~Point_size, sizemode="diameter", #Setting size to value in column, sizemode can be diameter or area opacity=0.8, #Opacity allows overlapping symbols to be visible. Value = 0-1. symbol = ~Resistance, symbols = c('circle', 'O')) %>% #Setting symbols based on "Resistance" add_markers() %>% layout(scene = list(xaxis = list(title = 'Geographic range', range = c(0,20)), #"range" sets the min/max on axis yaxis = list(title = 'Size', range=c(0,10)), zaxis = list(title = 'Speed', range=c(0,20)), aspectratio = list(x=1, y=0.5, z=1))) #Sets the ratio of axis length ### Final step: a short, edited label p.5 <- plot_ly(toy.data, x = ~Factor1, y = ~Factor2, z = ~Factor3, color = ~Bacteria, #Each Taxa colors = colors , marker = list(sizeref=0.05), opacity=0.8, #Use sizeref if `marker.size` is set to a numerical array. Sets the scale factor used to determine the rendered size of marker points. #Opacity allows overlapping symbols to be visible. Value = 0-1. size = ~Point_size, sizemode="diameter", #Setting size to value in column, sizemode can be diameter or area opacity=0.8, #Opacity allows overlapping symbols to be visible. Value = 0-1. symbol = ~Resistance, symbols = c('circle', 'O'), #Setting symbols based on "Resistance" text = paste(toy.data$Bacteria, toy.data$Resistance, sep=" "), #Sets what the hover label will say textposition = "top", #Sets position, there are several options hoverinfo = "text", mode = "markers + text") %>% add_markers() %>% layout(scene = list(xaxis = list(title = 'Geographic range', range = c(0,20)), #"range" sets the min/max on axis yaxis = list(title = 'Size', range=c(0,10)), zaxis = list(title = 'Speed', range=c(0,20)), aspectratio = list(x=1, y=0.5, z=1))) #Sets the ratio of axis length #You can upload your plot as an interactive html file, but you must make #an account with Plotly first. #Plotly can do a lot. For more information: https://plot.ly/r/ #### ---------------------- End ---------------------- ####