head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
dim(iris)
## [1] 150   5

Basic scatter plot matrix

ggplotly - scatter plot

  library(ggplot2)
  library(plotly)
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) + geom_point()
p

ggplotly(p)

p <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + 
  geom_point() + 
  geom_smooth(method = lm)

ggplotly(p)

taucharts - scatter plot with trend line

  library(taucharts)   #if (!require("taucharts")) devtools::install_github("hrbrmstr/taucharts")
tauirisplot=iris %>% tauchart(height=500,width=800)%>% 
  tau_point(x="Petal.Length",y="Petal.Width",color="Species")%>%
  tau_tooltip(c("Petal.Length", "Petal.Width", "Species"))%>% 
  tau_trendline( )%>% 
  tau_guide_y(auto_scale=TRUE)%>% 
  tau_guide_x(auto_scale=TRUE)%>%tau_legend()
tauirisplot

threejs - 3D Graphs

library(threejs)
library(plyr)
colormap <- mapvalues(iris$Species, from=unique(iris$Species), to=c("blue","red","green"))

#size=Petal.Width
labels_str <- paste(iris[,5], "<br/>", 
                    "Sepal.Length - ", iris[,1], "<br/>", 
                    "Sepal.Width - " , iris[,2], "<br/>", 
                    "Petal.Length - ", iris[,3], "<br/>", 
                    "Petal.Width - ", iris[,4],sep=" ")

scatterplot3js(iris[,c("Sepal.Width","Sepal.Length","Petal.Length")], size=iris$Petal.Width, color=colormap, labels=labels_str, renderer="canvas")

parcoords - parallel-coordinates

Parallel coordinates simple examples

# devtools::install_github("timelyportfolio/parcoords")
library(parcoords)
parcoords(
  iris[,1:5]
  ,reorderable = T
  ,rownames = F
  ,brushMode = "2d-strums"
  , color = list(
    colorBy="Species"
    ,colorScale=htmlwidgets::JS('d3.scale.category10()')
  )
)

Useful data manipulations

reshape2 - Creating long format

iris_long <- reshape2::melt(data.frame(ID = 1:150, iris), id.vars = c("ID","Species"))
head(iris_long)
##   ID Species     variable value
## 1  1  setosa Sepal.Length   5.1
## 2  2  setosa Sepal.Length   4.9
## 3  3  setosa Sepal.Length   4.7
## 4  4  setosa Sepal.Length   4.6
## 5  5  setosa Sepal.Length   5.0
## 6  6  setosa Sepal.Length   5.4
dim(iris_long)
## [1] 600   4

reshape2 - Creating wide format from long format

iris_wide <- reshape2::dcast(iris_long, ID + Species ~ variable)
head(iris_wide)
##   ID Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1  1  setosa          5.1         3.5          1.4         0.2
## 2  2  setosa          4.9         3.0          1.4         0.2
## 3  3  setosa          4.7         3.2          1.3         0.2
## 4  4  setosa          4.6         3.1          1.5         0.2
## 5  5  setosa          5.0         3.6          1.4         0.2
## 6  6  setosa          5.4         3.9          1.7         0.4
dim(iris_wide)
## [1] 150   6

plotly - Box plot

library(plotly)
plot_ly(iris_long, x = variable, y = value, color = Species, type = "box") %>%
  layout(boxmode = "group")