Plotly Examples

Example 1: iris data

library(plotly)
head(iris)
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
p
No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#scatter
No scatter mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#scatter
No scatter mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species)
p
No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#scatter
No scatter mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#scatter
No scatter mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

Example 2: cars data

head(cars)
summary(cars)
     speed           dist       
 Min.   : 4.0   Min.   :  2.00  
 1st Qu.:12.0   1st Qu.: 26.00  
 Median :15.0   Median : 36.00  
 Mean   :15.4   Mean   : 42.98  
 3rd Qu.:19.0   3rd Qu.: 56.00  
 Max.   :25.0   Max.   :120.00  
plot(cars)

p <- plot_ly(cars, x = ~speed, y = ~dist)
p
No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#scatter
No scatter mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#scatter
No scatter mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

Example 3: midwest data

head(midwest)
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p

Example 4: gapmider data

library(plotly)
library(gapminder)
p <- gapminder %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  ) %>%
  layout(
    xaxis = list(
      type = "log"
    )
  )
p

Example 5: gapmider data

p <- ggplot(gapminder, aes(x = year, y = lifeExp, text = paste("country:",country))) + 
     geom_point() +
     facet_wrap(~ continent) 
p

p <- ggplotly(p)
p

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
api_create(p, filename = "gapmider2")
Found a grid already named: 'gapmider2 Grid'. Since fileopt='overwrite', I'll try to update it
Found a plot already named: 'gapmider2'. Since fileopt='overwrite', I'll try to update it
# Note: api_create() replaced the plotly_POST()

Example 6:

plot_ly(z = ~volcano)
No trace type specified:
  Based on info supplied, a 'heatmap' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#heatmap
No trace type specified:
  Based on info supplied, a 'heatmap' trace seems appropriate.
  Read more about this trace type -> https://plot.ly/r/reference/#heatmap
LS0tCnRpdGxlOiAicGxvdGx5IGV4YW1wbGVzIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgojIyMgUGxvdGx5IEV4YW1wbGVzCgojIEV4YW1wbGUgMTogaXJpcyBkYXRhCgpgYGB7cn0KbGlicmFyeShwbG90bHkpCgpoZWFkKGlyaXMpCgpwIDwtIHBsb3RfbHkoZGF0YSA9IGlyaXMsIHggPSB+U2VwYWwuTGVuZ3RoLCB5ID0gflBldGFsLkxlbmd0aCkKcAoKcCA8LSBwbG90X2x5KGRhdGEgPSBpcmlzLCB4ID0gflNlcGFsLkxlbmd0aCwgeSA9IH5QZXRhbC5MZW5ndGgsIGNvbG9yID0gflNwZWNpZXMpCnAKCmBgYAoKIyBFeGFtcGxlIDI6IGNhcnMgZ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