@@ -296,7 +296,7 @@ mtcars <- mtcars[order(mtcars$disp), ]
296
296
p <- plot_ly(mtcars , x = disp , y = mpg , mode = " markers" ,
297
297
text = rownames(mtcars ), showlegend = FALSE )
298
298
add_trace(p , y = fitted(loess(mpg ~ disp )), mode = " lines" ,
299
- name = " loess smoother" , showlegend = TRUE ) % > % offline
299
+ name = " loess smoother" , showlegend = TRUE )
300
300
301
301
# ' Scatterplot with loess smoother and it's uncertaincy estimates
302
302
m <- loess(mpg ~ disp , data = mtcars )
@@ -479,6 +479,8 @@ plot_ly(air, lon = long, lat = lat, text = airport, type = 'scattergeo',
479
479
layout(title = ' Feb. 2011 American Airline flight paths<br>(Hover for airport names)' ,
480
480
geo = geo , showlegend = FALSE )
481
481
482
+ # ' London to NYC Great Circle
483
+ library(plotly )
482
484
plot_ly(lat = c(40.7127 , 51.5072 ), lon = c(- 74.0059 , 0.1275 ), type = ' scattergeo' ,
483
485
mode = ' lines' , line = list (width = 2 , color = ' blue' )) %> %
484
486
layout(
@@ -508,6 +510,8 @@ plot_ly(lat = c(40.7127, 51.5072), lon = c(-74.0059, 0.1275), type = 'scattergeo
508
510
)
509
511
)
510
512
513
+ # ' Contour lines on globe
514
+ library(plotly )
511
515
df <- read.csv(' https://raw.githubusercontent.com/plotly/datasets/master/globe_contours.csv' )
512
516
df $ id <- seq_len(nrow(df ))
513
517
@@ -556,107 +560,120 @@ layout(p, showlegend = FALSE, geo = geo,
556
560
title = ' Contour lines over globe<br>(Click and drag to rotate)' )
557
561
558
562
# ----------------------------------------------------------------------------
559
- # https://plot.ly/python /scatter-plots-on-maps/
563
+ # https://plot.ly/r /scatter-plots-on-maps/ (new)
560
564
# ----------------------------------------------------------------------------
561
565
566
+ # ' US Airports Map
567
+ library(plotly )
562
568
df <- read.csv(' https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv' )
563
569
df $ hover <- with(df , paste(airport , city , state , " Arrivals: " , cnt ))
564
570
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
+
566
589
plot_ly(df , lat = lat , lon = long , text = hover , color = cnt ,
567
590
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 )
584
593
594
+ # ' North American Precipitation Map
595
+ library(plotly )
585
596
df <- read.csv(' https://raw.githubusercontent.com/plotly/datasets/master/2015_06_30_precipitation.csv' )
586
597
df $ hover <- paste(df $ Globvalue , " inches" )
587
598
588
599
# change default color scale title
589
600
m <- list (colorbar = list (title = " Total Inches" ))
590
601
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
+
591
634
plot_ly(df , lat = Lat , lon = Lon , text = hover , color = Globvalue ,
592
635
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 )
625
637
626
638
# ----------------------------------------------------------------------------
627
- # https://plot.ly/python /bubble-maps/
639
+ # https://plot.ly/r /bubble-maps/ (new)
628
640
# ----------------------------------------------------------------------------
629
641
642
+ # ' United States Bubble Map
643
+ library(plotly )
630
644
df <- read.csv(' https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv' )
631
645
df $ hover <- paste(df $ name , " Population" , df $ pop / 1e6 , " million" )
632
646
633
647
df $ q <- with(df , cut(pop , quantile(pop )))
634
648
levels(df $ q ) <- paste(c(" 1st" , " 2nd" , " 3rd" , " 4th" , " 5th" ), " Quantile" )
635
649
df $ q <- as.ordered(df $ q )
636
650
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
+
637
662
plot_ly(df , lon = lon , lat = lat , text = hover ,
638
663
marker = list (size = sqrt(pop / 10000 ) + 1 ),
639
664
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
653
670
654
671
# ----------------------------------------------------------------------------
655
- # https://plot.ly/python /map-subplots-and-small-multiples/
672
+ # https://plot.ly/r /map-subplots-and-small-multiples/ (new)
656
673
# ----------------------------------------------------------------------------
657
674
675
+ # ' US map small multiples
658
676
library(plotly )
659
-
660
677
df <- read.csv(' https://raw.githubusercontent.com/plotly/datasets/master/1962_2006_walmart_store_openings.csv' )
661
678
662
679
# common map properties
@@ -696,13 +713,6 @@ p <- plot_ly(df, type = 'scattergeo', lon = LON, lat = LAT, group = YEAR,
696
713
697
714
subplot(p , nrows = 9 )
698
715
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
-
706
716
# ###############################################################################
707
717
# Multiple Axes, Subplots, and Insets (https://plot.ly/r/#multiple-axes-subplots-and-insets)
708
718
# ###############################################################################
0 commit comments