Skip to content

Commit d26c8ac

Browse files
committed
moar improvements to documentation examples
1 parent 90f4aa0 commit d26c8ac

File tree

1 file changed

+84
-74
lines changed

1 file changed

+84
-74
lines changed

inst/docs.R

Lines changed: 84 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ mtcars <- mtcars[order(mtcars$disp), ]
296296
p <- plot_ly(mtcars, x = disp, y = mpg, mode = "markers",
297297
text = rownames(mtcars), showlegend = FALSE)
298298
add_trace(p, y = fitted(loess(mpg ~ disp)), mode = "lines",
299-
name = "loess smoother", showlegend = TRUE) %>% offline
299+
name = "loess smoother", showlegend = TRUE)
300300

301301
#' Scatterplot with loess smoother and it's uncertaincy estimates
302302
m <- loess(mpg ~ disp, data = mtcars)
@@ -479,6 +479,8 @@ plot_ly(air, lon = long, lat = lat, text = airport, type = 'scattergeo',
479479
layout(title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
480480
geo = geo, showlegend = FALSE)
481481

482+
#' London to NYC Great Circle
483+
library(plotly)
482484
plot_ly(lat = c(40.7127, 51.5072), lon = c(-74.0059, 0.1275), type = 'scattergeo',
483485
mode = 'lines', line = list(width = 2, color = 'blue')) %>%
484486
layout(
@@ -508,6 +510,8 @@ plot_ly(lat = c(40.7127, 51.5072), lon = c(-74.0059, 0.1275), type = 'scattergeo
508510
)
509511
)
510512

513+
#' Contour lines on globe
514+
library(plotly)
511515
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/globe_contours.csv')
512516
df$id <- seq_len(nrow(df))
513517

@@ -556,107 +560,120 @@ layout(p, showlegend = FALSE, geo = geo,
556560
title = 'Contour lines over globe<br>(Click and drag to rotate)')
557561

558562
# ----------------------------------------------------------------------------
559-
# https://plot.ly/python/scatter-plots-on-maps/
563+
# https://plot.ly/r/scatter-plots-on-maps/ (new)
560564
# ----------------------------------------------------------------------------
561565

566+
#' US Airports Map
567+
library(plotly)
562568
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
563569
df$hover <- with(df, paste(airport, city, state, "Arrivals: ", cnt))
564570

565-
# TODO: rework utils so that marker specs aren't written over
571+
# marker styling
572+
m <- list(
573+
colorbar = list(title = "Incoming flights February 2011"),
574+
size = 8, opacity = 0.8, symbol = 'square'
575+
)
576+
577+
# geo styling
578+
g <- list(
579+
scope = 'usa',
580+
projection = list(type = 'albers usa'),
581+
showland = TRUE,
582+
landcolor = toRGB("gray95"),
583+
subunitcolor = toRGB("gray85"),
584+
countrycolor = toRGB("gray85"),
585+
countrywidth = 0.5,
586+
subunitwidth = 0.5
587+
)
588+
566589
plot_ly(df, lat = lat, lon = long, text = hover, color = cnt,
567590
type = 'scattergeo', locationmode = 'USA-states', mode = 'markers',
568-
marker = list(size = 8, opacity = 0.8, symbol = 'square')) %>%
569-
layout(
570-
title = 'Most trafficked US airports<br>(Hover for airport names)',
571-
geo = list(
572-
scope = 'usa',
573-
projection = list(type = 'albers usa'),
574-
showland = TRUE,
575-
landcolor = toRGB("gray95"),
576-
subunitcolor = toRGB("gray85"),
577-
countrycolor = toRGB("gray85"),
578-
countrywidth = 0.5,
579-
subunitwidth = 0.5
580-
)
581-
)
582-
583-
##########################################################################
591+
marker = m) %>%
592+
layout(title = 'Most trafficked US airports<br>(Hover for airport)', geo = g)
584593

594+
#' North American Precipitation Map
595+
library(plotly)
585596
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2015_06_30_precipitation.csv')
586597
df$hover <- paste(df$Globvalue, "inches")
587598

588599
# change default color scale title
589600
m <- list(colorbar = list(title = "Total Inches"))
590601

602+
# geo styling
603+
g <- list(
604+
scope = 'north america',
605+
showland = TRUE,
606+
landcolor = toRGB("grey83"),
607+
subunitcolor = toRGB("white"),
608+
countrycolor = toRGB("white"),
609+
showlakes = TRUE,
610+
lakecolor = toRGB("white"),
611+
showsubunits = TRUE,
612+
showcountries = TRUE,
613+
resolution = 50,
614+
projection = list(
615+
type = 'conic conformal',
616+
rotation = list(
617+
lon = -100
618+
)
619+
),
620+
lonaxis = list(
621+
showgrid = TRUE,
622+
gridwidth = 0.5,
623+
range = c(-140, -55),
624+
dtick = 5
625+
),
626+
lataxis = list(
627+
showgrid = TRUE,
628+
gridwidth = 0.5,
629+
range = c(20, 60),
630+
dtick = 5
631+
)
632+
)
633+
591634
plot_ly(df, lat = Lat, lon = Lon, text = hover, color = Globvalue,
592635
type = 'scattergeo', marker = m) %>%
593-
layout(title = 'US Precipitation 06-30-2015<br>Source: NOAA',
594-
geo = list(
595-
scope = 'north america',
596-
showland = TRUE,
597-
landcolor = toRGB("grey83"),
598-
subunitcolor = toRGB("white"),
599-
countrycolor = toRGB("white"),
600-
showlakes = TRUE,
601-
lakecolor = toRGB("white"),
602-
showsubunits = TRUE,
603-
showcountries = TRUE,
604-
resolution = 50,
605-
projection = list(
606-
type = 'conic conformal',
607-
rotation = list(
608-
lon = -100
609-
)
610-
),
611-
lonaxis = list(
612-
showgrid = TRUE,
613-
gridwidth = 0.5,
614-
range= c(-140, -55),
615-
dtick = 5
616-
),
617-
lataxis = list(
618-
showgrid = TRUE,
619-
gridwidth = 0.5,
620-
range= c(20, 60),
621-
dtick = 5
622-
)
623-
)
624-
)
636+
layout(title = 'US Precipitation 06-30-2015<br>Source: NOAA', geo = g)
625637

626638
# ----------------------------------------------------------------------------
627-
# https://plot.ly/python/bubble-maps/
639+
# https://plot.ly/r/bubble-maps/ (new)
628640
# ----------------------------------------------------------------------------
629641

642+
#' United States Bubble Map
643+
library(plotly)
630644
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
631645
df$hover <- paste(df$name, "Population", df$pop/1e6, " million")
632646

633647
df$q <- with(df, cut(pop, quantile(pop)))
634648
levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
635649
df$q <- as.ordered(df$q)
636650

651+
g <- list(
652+
scope = 'usa',
653+
projection = list(type = 'albers usa'),
654+
showland = TRUE,
655+
landcolor = toRGB("gray85"),
656+
subunitwidth = 1,
657+
countrywidth = 1,
658+
subunitcolor = toRGB("white"),
659+
countrycolor = toRGB("white")
660+
)
661+
637662
plot_ly(df, lon = lon, lat = lat, text = hover,
638663
marker = list(size = sqrt(pop/10000) + 1),
639664
color = q, type = 'scattergeo', locationmode = 'USA-states') %>%
640-
layout(
641-
title = '2014 US city populations<br>(Click legend to toggle traces)',
642-
geo = list(
643-
scope = 'usa',
644-
projection = list(type = 'albers usa'),
645-
showland = TRUE,
646-
landcolor = toRGB("gray85"),
647-
subunitwidth = 1,
648-
countrywidth = 1,
649-
subunitcolor = toRGB("white"),
650-
countrycolor = toRGB("white")
651-
)
652-
)
665+
layout(title = '2014 US city populations<br>(Click legend to toggle)', geo = g)
666+
667+
#' Ebola Cases in West Africa
668+
669+
# see 'Choropleth Inset Map' example
653670

654671
# ----------------------------------------------------------------------------
655-
# https://plot.ly/python/map-subplots-and-small-multiples/
672+
# https://plot.ly/r/map-subplots-and-small-multiples/ (new)
656673
# ----------------------------------------------------------------------------
657674

675+
#' US map small multiples
658676
library(plotly)
659-
660677
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/1962_2006_walmart_store_openings.csv')
661678

662679
# common map properties
@@ -696,13 +713,6 @@ p <- plot_ly(df, type = 'scattergeo', lon = LON, lat = LAT, group = YEAR,
696713

697714
subplot(p, nrows = 9)
698715

699-
700-
yr_count <- table(df$YEAR)
701-
plot_ly(x = names(yr_count), y = as.numeric(yr_count)) %>%
702-
layout(xaxis = list(title = ""), yaxis = list(title = ""))
703-
704-
subplot(p, p2, p3, nrows = 9)
705-
706716
################################################################################
707717
# Multiple Axes, Subplots, and Insets (https://plot.ly/r/#multiple-axes-subplots-and-insets)
708718
################################################################################

0 commit comments

Comments
 (0)