-
-
Save pbdanny/a092ac62d15c846a8216711879f8ff11 to your computer and use it in GitHub Desktop.
Revisions
-
cpsievert revised this gist
Jul 8, 2015 . 1 changed file with 22 additions and 70 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,8 +22,6 @@ df <- setNames( # create the plotly plot_ly(df, x = x, y = y, z = z, color = time, type = "scatter3d") # ---------------------------------------------------------------------- # https://plot.ly/r/3d-scatter-plots/ # ---------------------------------------------------------------------- @@ -43,22 +41,19 @@ df <- setNames(data.frame(obs), c("x", "y", "z")) plot_ly(df, x = x, y = y, z = z, type = "scatter3d", mode = "markers") # ---------------------------------------------------------------------- # https://plot.ly/r/3d-surface-plots/ # ---------------------------------------------------------------------- library(plotly) # Note that volcano is a numeric matrix that ships with R plot_ly(z = volcano, type = "surface") # 2D kernel density estimation kd <- with(geyser, MASS::kde2d(duration, waiting, n = 50)) with(kd, plot_ly(x = x, y = y, z = z, type = "surface")) # ---------------------------------------------------------------------- # https://plot.ly/r/filled-area-plots/ @@ -67,7 +62,7 @@ with(kd, plot_ly(x = x, y = y, z = z, type = "surface")) library(plotly) p <- plot_ly(x = c(1, 2, 3, 4), y = c(0, 2, 3, 5), fill = "tozeroy") add_trace(p, x = c(1, 2, 3, 4), y = c(3, 5, 1, 7), fill = "tonexty") # ---------------------------------------------------------------------- # https://plot.ly/r/bar-charts/ @@ -81,37 +76,27 @@ p <- plot_ly( type = "bar" ) p p2 <- add_trace(p, x = c("giraffes", "orangutans", "monkeys"), y = c(12, 18, 29), name = "LA Zoo" ) p2 layout(p2, barmode = "stack") ## customizing colors library(dplyr) ggplot2::diamonds %>% count(cut) %>% plot_ly(x = cut, y = n, type = "bar", marker = list(color = toRGB("black"))) # mapping a color variable ggplot2::diamonds %>% count(cut, clarity) %>% plot_ly(x = cut, y = n, type = "bar", color = clarity) # ---------------------------------------------------------------------- # https://plot.ly/r/box-plots/ # ---------------------------------------------------------------------- @@ -121,23 +106,21 @@ library(plotly) #' basic boxplot plot_ly(y = rnorm(50), type = "box") %>% add_trace(y = rnorm(50, 1)) #' adding jittered points plot_ly(y = rnorm(50), type = "box", boxpoints = "all", jitter = 0.3, pointpos = -1.8) #' several box plots data(diamonds, package = "ggplot2") plot_ly(diamonds, y = price, color = cut, type = "box") #' grouped box plots plot_ly(diamonds, x = cut, y = price, color = clarity, type = "box") %>% layout(boxmode = "group") # ---------------------------------------------------------------------- # https://plot.ly/r/bubble-charts/ @@ -156,7 +139,6 @@ plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity), #' Basic contour library(plotly) plot_ly(z = volcano, type = "contour") #' Advanced x <- rnorm(200) @@ -179,48 +161,41 @@ subplot( library(dplyr) library(plotly) p <- ggplot2::mpg %>% group_by(class) %>% summarise(mn = mean(hwy), sd = 1.96 * sd(hwy)) %>% arrange(desc(mn)) %>% plot_ly(x = class, y = mn, error_y = list(value = sd), mode = "markers", name = "Highway") %>% layout(yaxis = list(title = "Miles Per Gallon")) p df2 <- mpg %>% group_by(class) %>% summarise(mn = mean(cty), sd = 1.96 * sd(cty)) add_trace(p, y = mn, error_y = list(value = sd), name = "City", data = df2) # ---------------------------------------------------------------------- # https://plot.ly/r/heatmaps/ # ---------------------------------------------------------------------- library(plotly) plot_ly(z = volcano, type = "heatmap") #' categorical x/y axis m <- matrix(rnorm(9), nrow = 3, ncol = 3) plot_ly(z = m, x = c("a", "b", "c"), y = c("d", "e", "f"), type = "heatmap") #' Sequential Colorscales (Hot) plot_ly(z = volcano, colorscale = "Hot", type = "heatmap") #' Sequential Colorscales (Greys) plot_ly(z = volcano, colorscale = "Greys", type = "heatmap") #' Sequential Colorscales (Greens) plot_ly(z = volcano, colorscale = "Greens", type = "heatmap") #' Custom colorscale via scales package vals <- unique(scales::rescale(c(volcano))) @@ -237,7 +212,6 @@ library(plotly) s <- matrix(c(1, -.75, -.75, 1), ncol = 2) obs <- mvtnorm::rmvnorm(500, sigma = s) plot_ly(x = obs[,1], y = obs[,2], type = "histogram2d") # ---------------------------------------------------------------------- # https://plot.ly/r/histograms/ @@ -259,29 +233,23 @@ plot_ly(x = rnorm(500), opacity = 0.6, type = "histogram") %>% #' Simple scatterplot plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers") #' Scatterplot with qualitative colorscale plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, color = Species, mode = "markers") #' Scatterplot with sequential colorscale plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, color = Petal.Width, mode = "markers") #' Scatterplot with custom colorscale (TODO: how to add legend entries?) pal <- RColorBrewer::brewer.pal(3, "Set1") names(pal) <- levels(iris$Species) cols <- as.character(pal[iris$Species]) plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, marker = list(color = cols), mode = "markers") #' Basic time-series (line) plot with loess smooth plot_ly(economics, x = date, y = uempmed, name = "unemployment") add_trace(y = fitted(loess(uempmed ~ as.numeric(date)))) #' Density plot dens <- with(diamonds, tapply(price, INDEX = cut, density)) @@ -291,8 +259,6 @@ df <- data.frame( cut = rep(names(dens), each = length(dens[[1]]$x)) ) plot_ly(df, x = x, y = y, color = cut) #' Different line interpolation options x <- 1:5 @@ -303,8 +269,6 @@ plot_ly(x = x, y = y, name = "linear", line = list(shape = "linear")) %>% add_trace(y = y + 15, name = "hvh", line = list(shape = "hvh")) %>% add_trace(y = y + 20, name = "vh", line = list(shape = "vh")) %>% add_trace(y = y + 25, name = "hv", line = list(shape = "hv")) # ---------------------------------------------------------------------- # https://plot.ly/r/log-plot/ @@ -314,12 +278,12 @@ d <- diamonds[sample(nrow(diamonds), 1000), ] #' Without log scales (p <- plot_ly(d, x = carat, y = price, mode = "markers")) #' With log scales layout(p, xaxis = list(type = "log", autorange = T), yaxis = list(type = "log", autorange = T)) # ---------------------------------------------------------------------- # https://plot.ly/r/graphing-multiple-chart-types/ @@ -331,20 +295,14 @@ layout(p, xaxis = list(type = "log", autorange = T), # https://plot.ly/r/polar-chart/ # ---------------------------------------------------------------------- p <- plot_ly(plotly::mic, r = r, t = t, color = nms, mode = "lines") layout(p, title = "Mic Patterns", orientation = -90) p <- plot_ly(plotly::hobbs, r = r, t = t, color = nms, opacity = 0.7, mode = "markers") layout(p, title = "Hobbs-Pearson Trials", plot_bgcolor = toRGB("grey90")) p <- plot_ly(plotly::wind, r = r, t = t, color = nms, type = "area") layout(p, radialaxis = list(ticksuffix = "%"), orientation = 270) # ---------------------------------------------------------------------- # https://plot.ly/r/time-series/ @@ -356,23 +314,20 @@ tm <- seq(0, 600, by = 10) x <- now_lt - tm y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "seconds from now in GMT")) #' POSIXct date/time class now_ct <- as.POSIXct(Sys.time()) tm <- seq(0, 600, by = 10) x <- now_ct - tm y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "seconds from now in", Sys.timezone())) #' Dates today <- Sys.Date() tm <- seq(0, 600, by = 10) x <- today - tm y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "days from today")) # ---------------------------------------------------------------------------- # https://plot.ly/python/choropleth-maps/ @@ -400,7 +355,6 @@ plot_ly(df, z = total.exports, text = hover, locations = code, type = 'choroplet marker = list(line = l)), colorbar = list(title = "Millions USD")) %>% layout(title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)', geo = g) ########################################################################## @@ -425,8 +379,6 @@ plot_ly(df, z = GDP..BILLIONS., text = COUNTRY, locations = CODE, type = 'chorop # TODO: how to add the hyperlink? (<a href=""> doesn't seem to work) layout(title = '2014 Global GDP<br>Source: CIA World Factbook', geo = g) ########################################################################## df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv') @@ -464,7 +416,7 @@ g2 <- c( bgcolor = toRGB("white", alpha = 0), list(domain = list(x = c(0, .6), y = c(0, .6))) ) plot_ly(df, type = 'scattergeo', mode = 'markers', locations = Country, locationmode = 'country names', text = paste(Value, "cases"), @@ -473,7 +425,7 @@ plot_ly(df, type = 'scattergeo', mode = 'markers', locations = Country, # plotly should support "unboxed" constants lon = list(21.0936), lat = list(7.1881), text = list('Africa')) %>% add_trace(type = 'choropleth', locations = Country, locationmode = 'country names', z = Month, colors = "black", showscale = F, geo = 'geo2', data = df9) %>% layout(title = 'Ebola cases reported by month in West Africa 2014<br> Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">HDX</a>', geo = g1, geo2 = g2) -
cpsievert revised this gist
Jul 8, 2015 . 1 changed file with 280 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -374,6 +374,108 @@ y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "days from today")) # result -> https://plot.ly/~botty/3355 # ---------------------------------------------------------------------------- # https://plot.ly/python/choropleth-maps/ # ---------------------------------------------------------------------------- df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv") df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>", "Fruits", total.fruits, "Veggies", total.veggies, "<br>", "Wheat", wheat, "Corn", corn)) # give state boundaries a white border l <- list( color = toRGB("white"), width = 2 ) # specify some map projection/options g <- list( scope = 'usa', projection = list(type = 'albers usa'), showlakes = TRUE, lakecolor = toRGB('white') ) plot_ly(df, z = total.exports, text = hover, locations = code, type = 'choropleth', locationmode = 'USA-states', color = total.exports, colors = 'Purples', marker = list(line = l)), colorbar = list(title = "Millions USD")) %>% layout(title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)', geo = g) # result -> https://plot.ly/~agvd/179 ########################################################################## df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv') # light grey boundaries l <- list( color = toRGB("grey"), width = 0.5 ) # specify map projection/options g <- list( showframe = FALSE, showcoastlines = FALSE, projection = list(type = 'Mercator') ) plot_ly(df, z = GDP..BILLIONS., text = COUNTRY, locations = CODE, type = 'choropleth', color = GDP..BILLIONS., colors = 'Blues', marker = list(line = l), colorbar = list(tickprefix = '$', title = 'GDP Billions US$')) %>% # TODO: how to add the hyperlink? (<a href=""> doesn't seem to work) layout(title = '2014 Global GDP<br>Source: CIA World Factbook', geo = g) # result -> https://plot.ly/~agvd/181 ########################################################################## df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv') # restrict from June to September df <- subset(df, Month %in% 6:9) # ordered factor variable with month abbreviations df$abbrev <- ordered(month.abb[df$Month], levels = month.abb[6:9]) # September totals df9 <- subset(df, Month == 9) # common plot options g <- list( scope = 'africa', showframe = F, showland = T, landcolor = toRGB("grey90") ) # styling for "zoomed in" map g1 <- c( g, resolution = 50, showcoastlines = T, countrycolor = toRGB("white"), coastlinecolor = toRGB("white"), projection = list(type = 'Mercator'), list(lonaxis = list(range = c(-15, -5))), list(lataxis = list(range = c(0, 12))), list(domain = list(x = c(0, 1), y = c(0, 1))) ) g2 <- c( g, showcountries = F, bgcolor = toRGB("white", alpha = 0), list(domain = list(x = c(0, .6), y = c(0, .6))) ) plot_ly(df, type = 'scattergeo', mode = 'markers', locations = Country, locationmode = 'country names', text = paste(Value, "cases"), color = as.ordered(abbrev), marker = list(size = Value/50), inherit = F) %>% add_trace(type = 'scattergeo', mode = 'text', geo = 'geo2', showlegend = F, # plotly should support "unboxed" constants lon = list(21.0936), lat = list(7.1881), text = list('Africa')) %>% add_trace(type = 'choropleth', locations = Country, locationmode = 'country names', z = Month, colors = "black", showscale = F, geo = 'geo2', data = df9) %>% layout(title = 'Ebola cases reported by month in West Africa 2014<br> Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">HDX</a>', geo = g1, geo2 = g2) # ---------------------------------------------------------------------------- # https://plot.ly/python/lines-on-maps/ @@ -402,4 +504,181 @@ plot_ly(air, lon = long, lat = lat, text = airport, type = 'scattergeo', mode = 'lines', line = list(width = 1, color = 'red'), type = 'scattergeo', locationmode = 'USA-states') %>% layout(title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)', geo = geo, showlegend = FALSE) ########################################################################## plot_ly(lat = c(40.7127, 51.5072), lon = c(-74.0059, 0.1275), type = 'scattergeo', mode = 'lines', line = list(width = 2, color = 'blue')) %>% layout( title = 'London to NYC Great Circle', showlegend = FALSE, geo = list( resolution = 50, showland = TRUE, showlakes = TRUE, landcolor = toRGB("grey80"), countrycolor = toRGB("grey80"), lakecolor = toRGB("white"), projection = list(type = "equirectangular"), coastlinewidth = 2, lataxis = list( range = c(20, 60), showgrid = TRUE, tickmode = "linear", dtick = 10 ), lonaxis = list( range = c(-100, 20), showgrid = TRUE, tickmode = "linear", dtick = 20 ) ) ) ########################################################################## df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/globe_contours.csv') df$id <- seq_len(nrow(df)) library(tidyr) d <- df %>% gather(key, value, -id) %>% separate(key, c("l", "line"), "\\.") %>% spread(l, value) p <- plot_ly(type = 'scattergeo', mode = 'lines', line = list(width = 2, color = 'violet')) for (i in unique(d$line)) p <- add_trace(p, lat = lat, lon = lon, data = subset(d, line == i)) geo <- list( showland = TRUE, showlakes = TRUE, showcountries = TRUE, showocean = TRUE, countrywidth = 0.5, landcolor = toRGB("grey90"), lakecolor = toRGB("white"), oceancolor = toRGB("white"), projection = list( type = 'orthographic', rotation = list( lon = -100, lat = 40, roll = 0 ) ), lonaxis = list( showgrid = TRUE, gridcolor = toRGB("gray40"), gridwidth = 0.5 ), lataxis = list( showgrid = TRUE, gridcolor = toRGB("gray40"), gridwidth = 0.5 ) ) layout(p, showlegend = FALSE, geo = geo, title = 'Contour lines over globe<br>(Click and drag to rotate)') # ---------------------------------------------------------------------------- # https://plot.ly/python/scatter-plots-on-maps/ # ---------------------------------------------------------------------------- df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv') df$hover <- with(df, paste(airport, city, state, "Arrivals: ", cnt)) # TODO: rework utils so that marker specs aren't written over plot_ly(df, lat = lat, lon = long, text = hover, color = cnt, type = 'scattergeo', locationmode = 'USA-states', mode = 'markers', marker = list(size = 8, opacity = 0.8, symbol = 'square')) %>% layout( title = 'Most trafficked US airports<br>(Hover for airport names)', geo = list( scope = 'usa', projection = list(type = 'albers usa'), showland = TRUE, landcolor = toRGB("gray95"), subunitcolor = toRGB("gray85"), countrycolor = toRGB("gray85"), countrywidth = 0.5, subunitwidth = 0.5 ) ) ########################################################################## df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2015_06_30_precipitation.csv') df$hover <- paste(df$Globvalue, "inches") # change default color scale title m <- list(colorbar = list(title = "Total Inches")) plot_ly(df, lat = Lat, lon = Lon, text = hover, color = Globvalue, type = 'scattergeo', marker = m) %>% layout(title = 'US Precipitation 06-30-2015<br>Source: NOAA', geo = list( scope = 'north america', showland = TRUE, landcolor = toRGB("grey83"), subunitcolor = toRGB("white"), countrycolor = toRGB("white"), showlakes = TRUE, lakecolor = toRGB("white"), showsubunits = TRUE, showcountries = TRUE, resolution = 50, projection = list( type = 'conic conformal', rotation = list( lon = -100 ) ), lonaxis = list( showgrid = TRUE, gridwidth = 0.5, range= c(-140, -55), dtick = 5 ), lataxis = list( showgrid = TRUE, gridwidth = 0.5, range= c(20, 60), dtick = 5 ) ) ) # ---------------------------------------------------------------------------- # https://plot.ly/python/bubble-maps/ # ---------------------------------------------------------------------------- df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv') df$hover <- paste(df$name, "Population", df$pop/1e6, " million") df$q <- with(df, cut(pop, quantile(pop))) levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile") df$q <- as.ordered(df$q) plot_ly(df, lon = lon, lat = lat, text = hover, marker = list(size = sqrt(pop/10000) + 1), color = q, type = 'scattergeo', locationmode = 'USA-states') %>% layout( title = '2014 US city populations<br>(Click legend to toggle traces)', geo = list( scope = 'usa', projection = list(type = 'albers usa'), showland = TRUE, landcolor = toRGB("gray85"), subunitwidth = 1, countrywidth = 1, subunitcolor = toRGB("white"), countrycolor = toRGB("white") ) ) -
cpsievert revised this gist
Jul 7, 2015 . 1 changed file with 11 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -379,29 +379,11 @@ plot_ly(x = x, y = y, text = paste(tm, "days from today")) # https://plot.ly/python/lines-on-maps/ # ---------------------------------------------------------------------------- # airport locations air <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv') # flights between airports flights <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv') flights$id <- seq_len(nrow(flights)) # map projection geo <- list( @@ -412,6 +394,12 @@ geo <- list( countrycolor = toRGB("gray80") ) plot_ly(air, lon = long, lat = lat, text = airport, type = 'scattergeo', locationmode = 'USA-states', marker = list(size = 2, color = 'red'), inherit = FALSE) %>% add_trace(lon = list(start_lon, end_lon), lat = list(start_lat, end_lat), group = id, opacity = cnt/max(cnt), data = flights, mode = 'lines', line = list(width = 1, color = 'red'), type = 'scattergeo', locationmode = 'USA-states') %>% layout(title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)', geo = geo, showlegend = FALSE) -
cpsievert revised this gist
Jul 3, 2015 . 1 changed file with 43 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -372,4 +372,46 @@ tm <- seq(0, 600, by = 10) x <- today - tm y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "days from today")) # result -> https://plot.ly/~botty/3355 # ---------------------------------------------------------------------------- # https://plot.ly/python/lines-on-maps/ # ---------------------------------------------------------------------------- air <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv') # airport locations p <- plot_ly(air, lon = long, lat = lat, text = airport, type = 'scattergeo', locationmode = 'USA-states', marker = list(size = 2, color = 'red'), inherit = FALSE) # flights between airports flights <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv') # add a trace for each flight m <- max(flights$cnt) for (i in seq_len(nrow(flights))) { f <- flights[i, ] lon <- c(f$start_lon, f$end_lon) lat <- c(f$start_lat, f$end_lat) p <- add_trace(p, lon = lon, lat = lat, opacity = f$cnt/m, mode = 'lines', type = 'scattergeo', locationmode = 'USA-states', line = list(width = 1, color = 'red'), # evaluate arguments everytime add_trace() is called evaluate = TRUE) } # map projection geo <- list( scope = 'north america', projection = list(type = 'azimuthal equal area'), showland = TRUE, landcolor = toRGB("gray95"), countrycolor = toRGB("gray80") ) layout(p, title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)', geo = geo, showlegend = FALSE) # result -> https://plot.ly/~agvd/102 -
cpsievert revised this gist
Jun 23, 2015 . 1 changed file with 15 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -69,16 +69,6 @@ p <- plot_ly(x = c(1, 2, 3, 4), y = c(0, 2, 3, 5), fill = "tozeroy") add_trace(p, x = c(1, 2, 3, 4), y = c(3, 5, 1, 7), fill = "tonexty") # result -> https://plot.ly/~botty/1981 # ---------------------------------------------------------------------- # https://plot.ly/r/bar-charts/ # ---------------------------------------------------------------------- @@ -128,29 +118,25 @@ diamonds %>% count(cut, clarity) %>% library(plotly) #' basic boxplot plot_ly(y = rnorm(50), type = "box") %>% add_trace(y = rnorm(50, 1)) # result -> https://plot.ly/~botty/2000 #' adding jittered points plot_ly(y = rnorm(50), type = "box", boxpoints = "all", jitter = 0.3, pointpos = -1.8) # result -> https://plot.ly/~botty/2006 #' several box plots data(diamonds, package = "ggplot2") plot_ly(diamonds, y = price, color = cut, type = "box") # result -> https://plot.ly/~botty/2004 #' grouped box plots plot_ly(diamonds, x = cut, y = price, color = clarity, type = "box") %>% layout(boxmode = "group") # result -> https://plot.ly/~botty/2012 # ---------------------------------------------------------------------- @@ -161,20 +147,18 @@ plot_ly(diamonds, x = cut, y = price, color = clarity, type = "box") %>% d <- diamonds[sample(nrow(diamonds), 1000), ] plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity), mode = "markers", marker = list(size = depth)) # TODO: automatic scaling for marker size/opacity # ---------------------------------------------------------------------- # https://plot.ly/r/contour-plots/ # ---------------------------------------------------------------------- #' Basic contour library(plotly) plot_ly(z = volcano, type = "contour") # result -> https://plot.ly/~botty/2024 #' Advanced x <- rnorm(200) y <- rnorm(200) p1 <- plot_ly(x = x, type = "histogram") @@ -187,7 +171,6 @@ subplot( layout(p2, xaxis = a1, yaxis = a1), layout(p3, xaxis = a2, yaxis = a1) ) #TODO: fix this -> https://plot.ly/~botty/2038 # ---------------------------------------------------------------------- @@ -222,24 +205,24 @@ library(plotly) plot_ly(z = volcano, type = "heatmap") # result -> https://plot.ly/~botty/3253 #' categorical x/y axis m <- matrix(rnorm(9), nrow = 3, ncol = 3) plot_ly(z = m, x = c("a", "b", "c"), y = c("d", "e", "f"), type = "heatmap") # result -> https://plot.ly/~botty/3269 #' Sequential Colorscales (Hot) plot_ly(z = volcano, colorscale = "Hot", type = "heatmap") # result -> https://plot.ly/~botty/3255 #' Sequential Colorscales (Greys) plot_ly(z = volcano, colorscale = "Greys", type = "heatmap") # result -> https://plot.ly/~botty/3257 #' Sequential Colorscales (Greens) plot_ly(z = volcano, colorscale = "Greens", type = "heatmap") # result -> https://plot.ly/~botty/3259 #' Custom colorscale via scales package vals <- unique(scales::rescale(c(volcano))) o <- order(vals, decreasing = FALSE) cols <- scales::col_numeric("Blues", domain = NULL)(vals) @@ -328,8 +311,12 @@ plot_ly(x = x, y = y, name = "linear", line = list(shape = "linear")) %>% # ---------------------------------------------------------------------- d <- diamonds[sample(nrow(diamonds), 1000), ] #' Without log scales (p <- plot_ly(d, x = carat, y = price, mode = "markers")) # result -> https://plot.ly/~botty/3433 #' With log scales layout(p, xaxis = list(type = "log", autorange = T), yaxis = list(type = "log", autorange = T)) # result -> https://plot.ly/~botty/3431 -
cpsievert created this gist
Jun 23, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,388 @@ # install the new/experimental plotly R package # devtools::install_github("ropensci/plotly@carson-dsl") # ---------------------------------------------------------------------- # https://plot.ly/r/3d-line-plots/ # ---------------------------------------------------------------------- library(plotly) # initiate a 100 x 3 matrix filled with zeros m <- matrix(numeric(300), ncol = 3) # simulate a 3D random-walk for (i in 2:100) m[i, ] <- m[i-1, ] + rnorm(3) # collect everything in a data-frame df <- setNames( data.frame(m, seq(1, 100)), c("x", "y", "z", "time") ) # create the plotly plot_ly(df, x = x, y = y, z = z, color = time, type = "scatter3d") # result -> https://plot.ly/~botty/1973 # ---------------------------------------------------------------------- # https://plot.ly/r/3d-scatter-plots/ # ---------------------------------------------------------------------- library(plotly) # variance-covariance matrix for a multivariate normal distribution s <- matrix(c(1, .5, .5, .5, 1, .5, .5, .5, 1), ncol = 3) # use the mvtnorm package to sample 200 observations obs <- mvtnorm::rmvnorm(200, sigma = s) # collect everything in a data-frame df <- setNames(data.frame(obs), c("x", "y", "z")) plot_ly(df, x = x, y = y, z = z, type = "scatter3d", mode = "markers") # result -> https://plot.ly/~botty/1975 # ---------------------------------------------------------------------- # https://plot.ly/r/3d-surface-plots/ # ---------------------------------------------------------------------- library(plotly) # Note that volcano is a numeric matrix that ships with R plot_ly(z = volcano, type = "surface") # result -> https://plot.ly/~botty/1979 # 2D kernel density estimation kd <- with(geyser, MASS::kde2d(duration, waiting, n = 50)) with(kd, plot_ly(x = x, y = y, z = z, type = "surface")) # result -> https://plot.ly/~botty/3275 # ---------------------------------------------------------------------- # https://plot.ly/r/filled-area-plots/ # ---------------------------------------------------------------------- library(plotly) p <- plot_ly(x = c(1, 2, 3, 4), y = c(0, 2, 3, 5), fill = "tozeroy") add_trace(p, x = c(1, 2, 3, 4), y = c(3, 5, 1, 7), fill = "tonexty") # result -> https://plot.ly/~botty/1981 dfs <- diamonds %>% group_by(cut) %>% do(density = broom::tidy(density(.$price))) m <- Reduce(rbind, Map(function(x, y) cbind(cut = x, y), d, dfs$density)) jsonlite::rbind.pages(dfs$derp) %>% str select(derp) %>% Reduce(rbind, .) tidyr::unnest(derp) # ---------------------------------------------------------------------- # https://plot.ly/r/bar-charts/ # ---------------------------------------------------------------------- library(plotly) p <- plot_ly( x = c("giraffes", "orangutans", "monkeys"), y = c(20, 14, 23), name = "SF Zoo", type = "bar" ) p # result -> https://plot.ly/~botty/1983 p2 <- add_trace(p, x = c("giraffes", "orangutans", "monkeys"), y = c(12, 18, 29), name = "LA Zoo", ) p2 # result -> https://plot.ly/~botty/1987 layout(p2, barmode = "stack") # result -> https://plot.ly/~botty/1989 ## customizing colors library(dplyr) diamonds %>% count(cut) %>% plot_ly(x = cut, y = n, type = "bar", marker = list(color = toRGB("black"))) # mapping a color variable diamonds %>% count(cut, clarity) %>% plot_ly(x = cut, y = n, type = "bar", color = clarity) # TODO: fix legend title # https://plot.ly/~botty/1994 # ---------------------------------------------------------------------- # https://plot.ly/r/box-plots/ # ---------------------------------------------------------------------- library(plotly) # basic boxplot plot_ly(y = rnorm(50), type = "box") %>% add_trace(y = rnorm(50, 1)) # result -> https://plot.ly/~botty/2000 # adding jittered points plot_ly(y = rnorm(50), type = "box", boxpoints = "all", jitter = 0.3, pointpos = -1.8) # result -> https://plot.ly/~botty/2006 # several box plots data(diamonds, package = "ggplot2") plot_ly(diamonds, y = price, color = cut, type = "box") # result -> https://plot.ly/~botty/2004 # grouped box plots plot_ly(diamonds, x = cut, y = price, color = clarity, type = "box") %>% layout(boxmode = "group") # result -> https://plot.ly/~botty/2012 # ---------------------------------------------------------------------- # https://plot.ly/r/bubble-charts/ # ---------------------------------------------------------------------- # why do we need a separate page from this?? -> https://plot.ly/r/line-and-scatter/ d <- diamonds[sample(nrow(diamonds), 1000), ] plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity), mode = "markers", marker = list(size = depth)) # TODO: automatic scaling for marker size/opacity # ---------------------------------------------------------------------- # https://plot.ly/r/contour-plots/ # ---------------------------------------------------------------------- # Basic contour library(plotly) plot_ly(z = volcano, type = "contour") # result -> https://plot.ly/~botty/2024 # Advanced x <- rnorm(200) y <- rnorm(200) p1 <- plot_ly(x = x, type = "histogram") p2 <- plot_ly(x = x, y = y, type = "histogram2dcontour") p3 <- plot_ly(y = y, type = "histogram") a1 <- list(domain = c(0, .85)) a2 <- list(domain = c(.85, 1)) subplot( layout(p1, xaxis = a1, yaxis = a2), layout(p2, xaxis = a1, yaxis = a1), layout(p3, xaxis = a2, yaxis = a1) ) #TODO: fix this -> https://plot.ly/~botty/2038 # ---------------------------------------------------------------------- # https://plot.ly/r/error-bars/ # ---------------------------------------------------------------------- library(dplyr) library(plotly) data(mpg, package = "ggplot2") p <- mpg %>% group_by(class) %>% summarise(mn = mean(hwy), sd = 1.96 * sd(hwy)) %>% arrange(desc(mn)) %>% plot_ly(x = class, y = mn, error_y = list(value = sd), mode = "markers", name = "Highway") %>% layout(yaxis = list(title = "Miles Per Gallon")) p # result -> https://plot.ly/~botty/2070 df2 <- mpg %>% group_by(class) %>% summarise(mn = mean(cty), sd = 1.96 * sd(cty)) add_trace(p, y = mn, error_y = list(value = sd), name = "City", data = df2) # result -> https://plot.ly/~botty/2072 # ---------------------------------------------------------------------- # https://plot.ly/r/heatmaps/ # ---------------------------------------------------------------------- library(plotly) plot_ly(z = volcano, type = "heatmap") # result -> https://plot.ly/~botty/3253 # categorical x/y axis m <- matrix(rnorm(9), nrow = 3, ncol = 3) plot_ly(z = m, x = c("a", "b", "c"), y = c("d", "e", "f"), type = "heatmap") # result -> https://plot.ly/~botty/3269 # Sequential Colorscales (Hot) plot_ly(z = volcano, colorscale = "Hot", type = "heatmap") # result -> https://plot.ly/~botty/3255 # Sequential Colorscales (Greys) plot_ly(z = volcano, colorscale = "Greys", type = "heatmap") # result -> https://plot.ly/~botty/3257 # Sequential Colorscales (Greens) plot_ly(z = volcano, colorscale = "Greens", type = "heatmap") # result -> https://plot.ly/~botty/3259 # Custom colorscale via scales package vals <- unique(scales::rescale(c(volcano))) o <- order(vals, decreasing = FALSE) cols <- scales::col_numeric("Blues", domain = NULL)(vals) colz <- setNames(data.frame(vals[o], cols[o]), NULL) plot_ly(z = volcano, colorscale = colz, type = "heatmap") # ---------------------------------------------------------------------- # https://plot.ly/r/2D-Histogram/ # ---------------------------------------------------------------------- library(plotly) s <- matrix(c(1, -.75, -.75, 1), ncol = 2) obs <- mvtnorm::rmvnorm(500, sigma = s) plot_ly(x = obs[,1], y = obs[,2], type = "histogram2d") # result -> https://plot.ly/~botty/3279 # ---------------------------------------------------------------------- # https://plot.ly/r/histograms/ # ---------------------------------------------------------------------- #' Basic histogram plot_ly(x = rnorm(50), type = "histogram") #' Vertical histogram plot_ly(y = rnorm(50), type = "histogram") #' Overlayed histograms plot_ly(x = rnorm(500), opacity = 0.6, type = "histogram") %>% add_trace(x = rnorm(500)) # ---------------------------------------------------------------------- # https://plot.ly/r/line-and-scatter/ # ---------------------------------------------------------------------- #' Simple scatterplot plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers") # result -> https://plot.ly/~botty/3419 #' Scatterplot with qualitative colorscale plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, color = Species, mode = "markers") # result -> https://plot.ly/~botty/3421 #' Scatterplot with sequential colorscale plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, color = Petal.Width, mode = "markers") # result -> https://plot.ly/~botty/3423 #' Scatterplot with custom colorscale (TODO: how to add legend entries?) pal <- RColorBrewer::brewer.pal(3, "Set1") names(pal) <- levels(iris$Species) cols <- as.character(pal[iris$Species]) plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, marker = list(color = cols), mode = "markers") # result -> https://plot.ly/~botty/3427 #' Basic time-series (line) plot plot_ly(economics, x = date, y = uempmed, name = "unemployment") # result -> https://plot.ly/~botty/3319 add_trace(y = fitted(loess(uempmed ~ as.numeric(date)))) # result -> https://plot.ly/~botty/3331 #' Density plot dens <- with(diamonds, tapply(price, INDEX = cut, density)) df <- data.frame( x = unlist(lapply(dens, "[[", "x")), y = unlist(lapply(dens, "[[", "y")), cut = rep(names(dens), each = length(dens[[1]]$x)) ) plot_ly(df, x = x, y = y, color = cut) # result -> https://plot.ly/~botty/3325 #' Different line interpolation options x <- 1:5 y <- c(1, 3, 2, 3, 1) plot_ly(x = x, y = y, name = "linear", line = list(shape = "linear")) %>% add_trace(y = y + 5, name = "spline", line = list(shape = "spline")) %>% add_trace(y = y + 10, name = "vhv", line = list(shape = "vhv")) %>% add_trace(y = y + 15, name = "hvh", line = list(shape = "hvh")) %>% add_trace(y = y + 20, name = "vh", line = list(shape = "vh")) %>% add_trace(y = y + 25, name = "hv", line = list(shape = "hv")) # result -> https://plot.ly/~botty/3297 # ---------------------------------------------------------------------- # https://plot.ly/r/log-plot/ # ---------------------------------------------------------------------- d <- diamonds[sample(nrow(diamonds), 1000), ] (p <- plot_ly(d, x = carat, y = price, mode = "markers")) # result -> https://plot.ly/~botty/3433 layout(p, xaxis = list(type = "log", autorange = T), yaxis = list(type = "log", autorange = T)) # result -> https://plot.ly/~botty/3431 # ---------------------------------------------------------------------- # https://plot.ly/r/graphing-multiple-chart-types/ # ---------------------------------------------------------------------- # necessary? # ---------------------------------------------------------------------- # https://plot.ly/r/polar-chart/ # ---------------------------------------------------------------------- data(mic, package = "plotly") p <- plot_ly(mic, r = r, t = t, color = nms, mode = "lines") layout(p, title = "Mic Patterns", orientation = -90) # result -> https://plot.ly/~botty/3417 data(hobbs, package = "plotly") p <- plot_ly(hobbs, r = r, t = t, color = nms, opacity = 0.7, mode = "markers") layout(p, title = "Hobbs-Pearson Trials", plot_bgcolor = toRGB("grey90")) # result -> https://plot.ly/~botty/3415 data(wind, package = "plotly") p <- plot_ly(wind, r = r, t = t, color = nms, type = "area") layout(p, radialaxis = list(ticksuffix = "%"), orientation = 270) # result -> https://plot.ly/~botty/3413 # ---------------------------------------------------------------------- # https://plot.ly/r/time-series/ # ---------------------------------------------------------------------- #' POSIXlt date/time class now_lt <- as.POSIXlt(Sys.time(), tz = "GMT") tm <- seq(0, 600, by = 10) x <- now_lt - tm y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "seconds from now in GMT")) # result -> https://plot.ly/~botty/3361 #' POSIXct date/time class now_ct <- as.POSIXct(Sys.time()) tm <- seq(0, 600, by = 10) x <- now_ct - tm y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "seconds from now in", Sys.timezone())) # result -> https://plot.ly/~botty/3363 #' Dates today <- Sys.Date() tm <- seq(0, 600, by = 10) x <- today - tm y <- rnorm(length(x)) plot_ly(x = x, y = y, text = paste(tm, "days from today")) # result -> https://plot.ly/~botty/3355