From 45ab0c743961758b44829aaeb64ef8e0ba605649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Neto?= Date: Mon, 11 Nov 2019 21:44:26 +0000 Subject: [PATCH] added minor stuff added minor stuff --- GraphicalTools/ggplot2.Rmd | 143 +- GraphicalTools/ggplot2.html | 864 +++- GraphicalTools/graphicalTools.Rmd | 39 +- GraphicalTools/graphicalTools.html | 501 +- GraphicalTools/waterfall.Rmd | 193 + GraphicalTools/waterfall.html | 577 +++ .../waterfall_cache/html/__packages | 11 + ...ell_49cf397245ab786ac2cc0048b0980740.RData | Bin 0 -> 2840 bytes ...skell_49cf397245ab786ac2cc0048b0980740.rdb | 0 ...skell_49cf397245ab786ac2cc0048b0980740.rdx | Bin 0 -> 126 bytes animation/animation manual.pdf | Bin 353155 -> 0 bytes animation/animation vignette.pdf | Bin 6785203 -> 0 bytes animation/animation.gif | Bin 0 -> 428551 bytes animation/dnorm_animation.gif | Bin 19298 -> 19298 bytes animation/dnorm_test.html | 10 +- animation/grad_animation.gif | Bin 197553 -> 199727 bytes animation/index.Rmd | 210 +- animation/index.html | 501 +- animation/schelling_animation.gif | Bin 0 -> 345873 bytes animation/sine2D_animation.gif | Bin 0 -> 560026 bytes animation/surface3D_animation.gif | Bin 0 -> 1460304 bytes bootstrap/buckets.html | 2 +- index.htm | 21 +- odes/Differential Equations in R.pdf | Bin 4063832 -> 0 bytes ...al Solutions to ODEs Connor Johnson.html | 555 --- .../5a3168b82ab435b01767d070b74a7c96.png | Bin 2912 -> 0 bytes .../821b8bf222958f83097ac40befc9edd7.jpg | Bin 1340 -> 0 bytes .../a548de75fdd4ee3cba7a92370d1905ec.png | Bin 1358 -> 0 bytes .../blocks.css | 400 -- .../comment-reply.js | 1 - .../css.css | 112 - .../d4164d26264f27e45e05a8db2c53f7fe.png | Bin 2376 -> 0 bytes .../euler_example_n10.html | 270 - .../form.js | 30 - .../functions.js | 210 - .../genericons.css | 197 - .../heun_example_n10.html | 270 - .../jquery-migrate.js | 2 - .../jquery.js | 6 - .../midpoint_example_n10.html | 270 - .../modified_euler_example_n10.html | 270 - .../quicklatex-format.css | 93 - .../quicklatex.svg | 78 - .../quicklatex_002.svg | 43 - .../quicklatex_003.svg | 21 - .../quicklatex_004.svg | 27 - .../quicklatex_005.svg | 51 - .../quicklatex_006.svg | 15 - .../quicklatex_007.svg | 11 - .../quicklatex_008.svg | 15 - .../quicklatex_009.svg | 91 - .../quicklatex_010.svg | 65 - .../quicklatex_011.svg | 126 - .../quicklatex_012.svg | 21 - .../quicklatex_013.svg | 11 - .../quicklatex_014.svg | 17 - .../quicklatex_015.svg | 48 - .../quicklatex_016.svg | 46 - .../quicklatex_017.svg | 63 - .../quicklatex_018.svg | 19 - .../quicklatex_019.svg | 95 - .../quicklatex_020.svg | 98 - .../quicklatex_021.svg | 11 - .../quicklatex_022.svg | 11 - .../quicklatex_023.svg | 38 - .../quicklatex_024.svg | 18 - .../quicklatex_025.svg | 137 - .../quicklatex_026.svg | 46 - .../quicklatex_027.svg | 13 - .../quicklatex_028.svg | 37 - .../quicklatex_029.svg | 40 - .../quicklatex_030.svg | 17 - .../quicklatex_031.svg | 78 - .../quicklatex_032.svg | 36 - .../quicklatex_033.svg | 11 - .../quicklatex_034.svg | 54 - .../quicklatex_035.svg | 90 - .../quicklatex_036.svg | 51 - .../quicklatex_037.svg | 23 - .../quicklatex_038.svg | 87 - .../rk4_example_n10.html | 270 - .../shBrushPython.js | 64 - .../shCore.css | 237 - .../shCore.js | 17 - .../shThemeDefault.css | 117 - .../style.css | 1 - .../style_002.css | 4436 ----------------- .../taylor_example_n10.html | 270 - .../theme.css | 1 - .../wp-embed.js | 1 - .../wp-emoji-release.js | 4 - .../wp-quicklatex-frontend.js | 14 - ...rg - A Tutorial on Spectral Clustering.pdf | Bin 446055 -> 0 bytes variational_inference/variational.Rmd | 90 +- variational_inference/variational.html | 482 +- 95 files changed, 3143 insertions(+), 10378 deletions(-) create mode 100644 GraphicalTools/waterfall.Rmd create mode 100644 GraphicalTools/waterfall.html create mode 100644 GraphicalTools/waterfall_cache/html/__packages create mode 100644 GraphicalTools/waterfall_cache/html/test-haskell_49cf397245ab786ac2cc0048b0980740.RData create mode 100644 GraphicalTools/waterfall_cache/html/test-haskell_49cf397245ab786ac2cc0048b0980740.rdb create mode 100644 GraphicalTools/waterfall_cache/html/test-haskell_49cf397245ab786ac2cc0048b0980740.rdx delete mode 100644 animation/animation manual.pdf delete mode 100644 animation/animation vignette.pdf create mode 100644 animation/animation.gif create mode 100644 animation/schelling_animation.gif create mode 100644 animation/sine2D_animation.gif create mode 100644 animation/surface3D_animation.gif delete mode 100644 odes/Differential Equations in R.pdf delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson.html delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/5a3168b82ab435b01767d070b74a7c96.png delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/821b8bf222958f83097ac40befc9edd7.jpg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/a548de75fdd4ee3cba7a92370d1905ec.png delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/blocks.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/comment-reply.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/css.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/d4164d26264f27e45e05a8db2c53f7fe.png delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/euler_example_n10.html delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/form.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/functions.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/genericons.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/heun_example_n10.html delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/jquery-migrate.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/jquery.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/midpoint_example_n10.html delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/modified_euler_example_n10.html delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex-format.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_002.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_003.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_004.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_005.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_006.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_007.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_008.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_009.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_010.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_011.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_012.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_013.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_014.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_015.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_016.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_017.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_018.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_019.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_020.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_021.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_022.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_023.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_024.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_025.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_026.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_027.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_028.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_029.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_030.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_031.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_032.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_033.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_034.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_035.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_036.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_037.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/quicklatex_038.svg delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/rk4_example_n10.html delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/shBrushPython.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/shCore.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/shCore.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/shThemeDefault.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/style.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/style_002.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/taylor_example_n10.html delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/theme.css delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/wp-embed.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/wp-emoji-release.js delete mode 100644 odes/Numerical Solutions to ODEs Connor Johnson_ficheiros/wp-quicklatex-frontend.js delete mode 100644 spectralclustering/Luxburg - A Tutorial on Spectral Clustering.pdf diff --git a/GraphicalTools/ggplot2.Rmd b/GraphicalTools/ggplot2.Rmd index ad71edf..7fa1329 100644 --- a/GraphicalTools/ggplot2.Rmd +++ b/GraphicalTools/ggplot2.Rmd @@ -32,7 +32,7 @@ The most important concepts are: + Coordinate systems, **coord**, a map from the data coordinates to the graph coordinates (eg, cartesian, log-log, polar) -+ **Facets** describing how to plit the data into subsets ++ **Facets** describing how to split the data into subsets ```{r, comment=FALSE} library(ggplot2) @@ -213,16 +213,15 @@ Layers are responsible for creating the objects that we perceive on the plot. A `ggplot` receives two arguments, the data which must be a data frame and which aesthetics mappings we want. An eg: ```{r, error=TRUE} -p <- ggplot(d, aes(carat, price, colour=cut)) +p <- ggplot(d, aes(carat, price, colour=cut)) p ``` -It gave an error because we still not defined geoms so that there is something to see. -To add new layers we should use operator `+`: +It gave an error because we still not defined geoms so that there is something to see. To add new layers we should use operator `+`: ```{r} -plot1 <- p + layer(geom="point") -plot2 <- p + layer(geom="line") +plot1 <- p + geom_point() +plot2 <- p + geom_line() grid.arrange(plot1, plot2, ncol=2) ``` @@ -230,10 +229,9 @@ grid.arrange(plot1, plot2, ncol=2) ```{r} p <- ggplot(diamonds, aes(x=carat)) -plot1 <- p + layer(geom="bar", - geom_params = list(fill = "steelblue"), - stat = "bin", - stat_params = list(binwidth = 0.25)) +plot1 <- p + geom_bar(geom_params = list(fill = "steelblue"), + stat = "bin", + stat_params = list(binwidth = 0.25)) plot2 <- p + geom_histogram(binwidth = 0.25, fill = "steelblue") # same graph grid.arrange(plot1, plot2, ncol=2) ``` @@ -401,20 +399,59 @@ plot5 <- plot1 + coord_equal() plot6 <- plot1 + coord_trans(x = "log10") grid.arrange(plot1, plot2, plot3, plot4, plot5, plot6, nrow=2, ncol=3) ``` +Plots within plots: + +```{r} +plot1 + annotation_custom(ggplotGrob(plot2), xmin = 100, xmax = 200, + ymin = 50, ymax = 100) +``` + + Eg with map coordinates: ```{r, message=FALSE} -library("maps") +library(maps) +library(mapproj) m <- map_data("italy") p <- ggplot(m, aes(x=long, y=lat, group=group)) + geom_polygon(fill="white", colour="black") -plot1 <- p # Use cartesian coordinates -plot2 <- p + coord_map() # With default mercator projection -grid.arrange(plot1, plot2, ncol=2) +p + coord_map() # With default mercator projection ``` +Source: [https://statisticaloddsandends.wordpress.com/2019/02/24/plots-within-plots-with-ggplot2-and-ggmap/](https://statisticaloddsandends.wordpress.com/2019/02/24/plots-within-plots-with-ggplot2-and-ggmap/) + +```{r} +library(ggmap) + +us_bbox <- c(left = -125, bottom = 25, right = -55, top = 50) +us_main_map <- get_stamenmap(us_bbox, zoom = 5, maptype = "terrain") +p_main <- ggmap(us_main_map) + +alaska_bbox <- c(left = -180, bottom = 50, right = -128, top = 72) +alaska_map <- get_stamenmap(alaska_bbox, zoom = 5, maptype = "terrain") +p_alaska <- ggmap(alaska_map) + + #labs(subtitle = "Alaska") + + theme(axis.title = element_blank(), + axis.text = element_blank(), + axis.ticks = element_blank()) + +hawaii_bbox <- c(left = -160, bottom = 18.5, right = -154.5, top = 22.5) +hawaii_map <- get_stamenmap(hawaii_bbox, zoom = 6, maptype = "terrain") +p_hawaii <- ggmap(hawaii_map) + + #labs(subtitle = "Hawaii") + + theme(axis.title = element_blank(), + axis.text = element_blank(), + axis.ticks = element_blank()) + +library(grid) +p_main + + inset(ggplotGrob(p_alaska), xmin = -76.7, xmax = -66.7, ymin = 26, ymax = 35) + + inset(ggplotGrob(p_hawaii), xmin = -66.5, xmax = -55.5, ymin = 26, ymax = 35) +``` + + ### Some more complex egs ```{r, include=FALSE} @@ -489,16 +526,17 @@ ggplot(tgc, aes(x=dose, y=len, colour=supp, group=supp)) + legend.position=c(1,0)) # Position legend in bottom right ``` + + + + This next eg is based on Robin Lovelace and James Cheshire's [tutorial](http://www.r-bloggers.com/introduction-to-spatial-data-and-ggplot2/) (the Rpubs page is [here](http://rpubs.com/m_dev/Intro-to-Spatial-Data-and-ggplot2) and also [here](https://rpubs.com/RobinLovelace/ggmap)). @@ -611,10 +654,9 @@ plot3 <- plot2 + geom_path(data = sport.f, aes(x = long, y = lat, group = group), color = "white") + theme_classic() # this line removes the distracting grey background -new_theme <- theme(axis.line = element_blank(), axis.ticks = element_blank(), +new_theme <- theme(axis.line = element_blank(), axis.ticks = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), - axis.text.y = element_blank(), axis.text.x = element_blank(), - axis.text.y = element_blank(), axis.text.y = element_blank(), + axis.text.y = element_blank(), axis.text.x = element_blank(), panel.background = element_rect(fill = "lightgreen")) plot4 <- p + new_theme grid.arrange(plot1, plot2, plot3, plot4, nrow=2, ncol=2) @@ -638,5 +680,60 @@ plot3 <- p + geom_polygon(data = sport.f, grid.arrange(plot1, plot2, plot3, nrow=2, ncol=2) ``` +## Ploting many points: + +A view of Clifford's strange attractors, with 1e6 points: + +```{r} +library(Rcpp) +library(ggplot2) +library(dplyr) + +opt = theme(legend.position = "none", + panel.background = element_rect(fill="white"), + axis.ticks = element_blank(), + panel.grid = element_blank(), + axis.title = element_blank(), + axis.text = element_blank()) + +cppFunction('DataFrame createTrajectory(int n, double x0, double y0, + double a, double b, double c, double d) { + + NumericVector x(n); // create the columns + NumericVector y(n); + + x[0]=x0; + y[0]=y0; + for(int i = 1; i < n; ++i) { + x[i] = sin(a*y[i-1])+c*cos(a*x[i-1]); + y[i] = sin(b*x[i-1])+d*cos(b*y[i-1]); + } + // return a new data frame + return DataFrame::create(_["x"]= x, _["y"]= y); + } + ') + +a <- -1.24458046630025 +b <- -1.25191834103316 +c <- -1.81590817030519 +d <- -1.90866735205054 + +df <- createTrajectory(1000000, 0, 0, a, b, c, d) + +#png("Clifford.png", units="px", width=1600, height=1600, res=300) +ggplot(df, aes(x, y)) + geom_point(color="black", shape=46, alpha=.01) + opt +``` + +Source: [https://fronkonstin.com/2017/11/07/drawing-10-million-points-with-ggplot-clifford-attractors/](https://fronkonstin.com/2017/11/07/drawing-10-million-points-with-ggplot-clifford-attractors/) + +## Other Links: + ++ [A Compendium of Clean Graphs in R](http://shinyapps.org/apps/RGraphCompendium/index.php) + ++ [How to map your Google location history with R](https://shiring.github.io/maps/2016/12/30/Standortverlauf_post) + + ++ [ggplot2 - Easy Way to Mix Multiple Graphs on The Same Page](http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/) ++ diff --git a/GraphicalTools/ggplot2.html b/GraphicalTools/ggplot2.html index 22b80bb..716d27b 100644 --- a/GraphicalTools/ggplot2.html +++ b/GraphicalTools/ggplot2.html @@ -4,42 +4,258 @@ - + + + Using ggplot2 - + - - - - + + + + + + + - - - + + + - + + + + + + + + + + + + + + + + +
- -
-

Using with maps

-

This next eg in taken from ggmap, Spatial Visualization with ggplot2 vignette:

-
library(ggmap)
-
-head(crime)  # compiled from Houston Police Department's website
-
##                      time     date hour premise            offense  beat
-## 82729 2010-01-01 06:00:00 1/1/2010    0     18A             murder 15E30
-## 82730 2010-01-01 06:00:00 1/1/2010    0     13R            robbery 13D10
-## 82731 2010-01-01 06:00:00 1/1/2010    0     20R aggravated assault 16E20
-## 82732 2010-01-01 06:00:00 1/1/2010    0     20R aggravated assault  2A30
-## 82733 2010-01-01 06:00:00 1/1/2010    0     20A aggravated assault 14D20
-## 82734 2010-01-01 06:00:00 1/1/2010    0     20R           burglary 18F60
-##           block    street type suffix number   month    day
-## 82729 9600-9699   marlive   ln      -      1 january friday
-## 82730 4700-4799 telephone   rd      -      1 january friday
-## 82731 5000-5099  wickview   ln      -      1 january friday
-## 82732 1000-1099   ashland   st      -      1 january friday
-## 82733 8300-8399    canyon           -      1 january friday
-## 82734 9300-9399     rowan   ln      -      1 january friday
-##                       location           address       lon      lat
-## 82729    apartment parking lot   9650 marlive ln -95.43739 29.67790
-## 82730 road / street / sidewalk 4750 telephone rd -95.29888 29.69171
-## 82731        residence / house  5050 wickview ln -95.45586 29.59922
-## 82732        residence / house   1050 ashland st -95.40334 29.79024
-## 82733                apartment       8350 canyon -95.37791 29.67063
-## 82734        residence / house     9350 rowan ln -95.54830 29.70223
-
murders <- crime[crime$offense == "murder",]
-
-# get map of Houston (this is also a ggplot object)
-houstonMap <- qmap("houston", zoom = 11, color = "bw", legend = "topleft")
-
-plot1 <- houstonMap +
-          geom_point(aes(x = lon, y = lat, colour = "red", size = 2), data = murders) +
-          theme(legend.position="none")
-
-plot2 <- houstonMap +
-          stat_bin2d(aes(x = lon, y = lat, colour = offense, fill = offense),
-                     size = .5, bins = 30, alpha = 1/2, data = murders)
-
-plot3 <- houstonMap +
-          stat_density2d(aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
-                         size = 2, bins = 4, data = murders, geom = "polygon") +
-          guides(alpha=FALSE) # remove alpha legend
-grid.arrange(plot1, plot2, plot3, ncol=3)
-

+

+ +

This next eg is based on Robin Lovelace and James Cheshire’s tutorial (the Rpubs page is here and also here).

library(rgdal)
 
 sport <- readOGR(dsn = "files", "london_sport") # open files/london_sport
## OGR data source with driver: ESRI Shapefile 
-## Source: "files", layer: "london_sport"
+## Source: "C:\Users\jpn3t\Documents\GitHub\Markdowns\GraphicalTools\files", layer: "london_sport"
 ## with 33 features
-## It has 4 fields
+## It has 4 fields +## Integer64 fields read as strings: Pop_2001
# sport.f <- fortify(sport, region = "ons_label") # format shapefile to plot
 # head(sport.f)
 # # add back attribute information associated with sport object (merge is a data join)
@@ -563,13 +931,13 @@ 

Using with maps

sport$id <- row.names(sport) # provide same column names for join sport.f <- join(sport.f, sport@data) head(sport.f)
-
##         long      lat order  hole piece group id ons_label    name
-## 1 0.03163909 51.44288     1 FALSE     1   0.1  0      00AF Bromley
-## 2 0.04152608 51.44046     2 FALSE     1   0.1  0      00AF Bromley
-## 3 0.06333280 51.42321     3 FALSE     1   0.1  0      00AF Bromley
-## 4 0.07694588 51.43151     4 FALSE     1   0.1  0      00AF Bromley
-## 5 0.10922623 51.41360     5 FALSE     1   0.1  0      00AF Bromley
-## 6 0.13119092 51.41437     6 FALSE     1   0.1  0      00AF Bromley
+
##         long      lat order  hole piece id group ons_label    name
+## 1 0.03163909 51.44288     1 FALSE     1  0   0.1      00AF Bromley
+## 2 0.04152608 51.44046     2 FALSE     1  0   0.1      00AF Bromley
+## 3 0.06333280 51.42321     3 FALSE     1  0   0.1      00AF Bromley
+## 4 0.07694588 51.43151     4 FALSE     1  0   0.1      00AF Bromley
+## 5 0.10922623 51.41360     5 FALSE     1  0   0.1      00AF Bromley
+## 6 0.13119092 51.41437     6 FALSE     1  0   0.1      00AF Bromley
 ##   Partic_Per Pop_2001
 ## 1       21.7   295535
 ## 2       21.7   295535
@@ -584,7 +952,7 @@ 

Using with maps

labs(x = "Easting (m)", y = "Northing (m)", fill = "% Sport Partic.") + ggtitle("London Sports Participation") p + scale_fill_gradient(low = "white", high = "black") # black&white version
-

+

# ggsave("plot.png", scale = 3, dpi = 400) # to save image

Several operations over the London map:

p <- ggplot() + 
@@ -596,14 +964,13 @@ 

Using with maps

geom_path(data = sport.f, aes(x = long, y = lat, group = group), color = "white") + theme_classic() # this line removes the distracting grey background -new_theme <- theme(axis.line = element_blank(), axis.ticks = element_blank(), +new_theme <- theme(axis.line = element_blank(), axis.ticks = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), - axis.text.y = element_blank(), axis.text.x = element_blank(), - axis.text.y = element_blank(), axis.text.y = element_blank(), + axis.text.y = element_blank(), axis.text.x = element_blank(), panel.background = element_rect(fill = "lightgreen")) plot4 <- p + new_theme grid.arrange(plot1, plot2, plot3, plot4, nrow=2, ncol=2)
-

+

These objects can also be layered in ggmap objects:

library(ggmap) 
 b <- bbox(sport)
@@ -618,27 +985,120 @@ 

Using with maps

plot3 <- p + geom_polygon(data = sport.f, aes(x=long, y=lat, group=group, fill=Partic_Per), alpha = 0.5) grid.arrange(plot1, plot2, plot3, nrow=2, ncol=2)
-

+

+
+
+
+

Ploting many points:

+

A view of Clifford’s strange attractors, with 1e6 points:

+
library(Rcpp)
+library(ggplot2)
+library(dplyr)
+
## 
+## Attaching package: 'dplyr'
+
## The following objects are masked from 'package:plyr':
+## 
+##     arrange, count, desc, failwith, id, mutate, rename, summarise,
+##     summarize
+
## The following object is masked from 'package:MASS':
+## 
+##     select
+
## The following object is masked from 'package:gridExtra':
+## 
+##     combine
+
## The following objects are masked from 'package:stats':
+## 
+##     filter, lag
+
## The following objects are masked from 'package:base':
+## 
+##     intersect, setdiff, setequal, union
+
opt = theme(legend.position  = "none",
+            panel.background = element_rect(fill="white"),
+            axis.ticks       = element_blank(),
+            panel.grid       = element_blank(),
+            axis.title       = element_blank(),
+            axis.text        = element_blank())
+ 
+cppFunction('DataFrame createTrajectory(int n, double x0, double y0, 
+                             double a, double b, double c, double d) {
+              
+              NumericVector x(n);  // create the columns
+              NumericVector y(n);
+            
+              x[0]=x0;
+              y[0]=y0;
+              for(int i = 1; i < n; ++i) {
+                x[i] = sin(a*y[i-1])+c*cos(a*x[i-1]);
+                y[i] = sin(b*x[i-1])+d*cos(b*y[i-1]);
+              }
+              // return a new data frame
+              return DataFrame::create(_["x"]= x, _["y"]= y);
+            }
+      ')
+  
+a <- -1.24458046630025
+b <- -1.25191834103316 
+c <- -1.81590817030519 
+d <- -1.90866735205054
+ 
+df <- createTrajectory(1000000, 0, 0, a, b, c, d)
+ 
+#png("Clifford.png", units="px", width=1600, height=1600, res=300)
+ggplot(df, aes(x, y)) + geom_point(color="black", shape=46, alpha=.01) + opt
+

+

Source: https://fronkonstin.com/2017/11/07/drawing-10-million-points-with-ggplot-clifford-attractors/

+
+ + + + + + + + + + diff --git a/GraphicalTools/graphicalTools.Rmd b/GraphicalTools/graphicalTools.Rmd index c3443fc..ba5f999 100644 --- a/GraphicalTools/graphicalTools.Rmd +++ b/GraphicalTools/graphicalTools.Rmd @@ -1,6 +1,6 @@ --- title: "Graphical Tools" -author: "Joăo Neto" +author: "JoĂ£o Neto" date: "May 2014" output: html_document: @@ -119,16 +119,15 @@ sd <- 1 # sd parameter lb <- -1.5 # lower bound ub <- 1.25 # upper bound -x <- seq(-4,4,length=100)*sd + mean -hx <- dnorm(x,mean,sd) +x <- seq(-4, 4, length=101) +hx <- dnorm(x, mean, sd) -plot(x, hx, type="n", xlab="y", ylab="Density", - main="Normal Distribution") - -i <- x >= lb & x <= ub +plot(x, hx, type="n", xlab="y", ylab="Density", main="Normal Distribution") lines(x, hx) # polygon draws the polygons whose vertices are given in x and y -polygon(c(lb,x[i],ub), c(0,hx[i],0), col="red") +i <- x >= lb & x <= ub +polygon(c(lb, lb, x[i], ub, ub), + c(0,dnorm(lb, mean, sd),hx[i],dnorm(ub, mean, sd),0), col="red") area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd) result <- paste("P(",lb,"< Y <",ub,") =", signif(area, digits=3)) @@ -142,20 +141,40 @@ ub <- 60 # upper bound x <- seq(lb,ub*1.5,length=100) hx <- dexp(x,lambda) + plot(x, hx, type="n", xlab="y", ylab="Density") -i <- x >= lb & x <= ub lines(x, hx) # polygon draws the polygons whose vertices are given in x and y +i <- x >= lb & x <= ub polygon(c(lb,x[i],ub), c(0,hx[i],0), col="red") area <- pexp(ub, 1/20) - pexp(lb, 1/20) result <- paste("P(",lb,"< Y <",ub,") =", signif(area, digits=6)) # place label on top of graph mtext(result,3) - title(bquote(paste("Exponential Distribution with ", lambda, "= 1/20"))) ``` +Using a gradient [[ref](http://www.alisonsinclair.ca/2011/03/shading-between-curves-in-r/)]: + +```{r} +x <- seq(-3,3,0.01) +y1 <- dnorm(x,0,1) +y2 <- 0.5*dnorm(x,0,1) + +x.shade <- seq(-2,1,0.01) + +plot(x,y1,type="l",bty="L",xlab="X",ylab="dnorm(X)") +points(x,y2,type="l",col="gray") +l <- length(x.shade) +color <- heat.colors(l) +for (i in 1:l) { + polygon(c(x.shade[i],rev(x.shade[i])), + c(dnorm(x.shade[i],0,1),0.5*dnorm(rev(x.shade[i]),0,1)), + border=color[i],col=NA) +} +``` + Histogram, Boxplot and Barplot ------------------------------ diff --git a/GraphicalTools/graphicalTools.html b/GraphicalTools/graphicalTools.html index a6f6623..3a196f5 100644 --- a/GraphicalTools/graphicalTools.html +++ b/GraphicalTools/graphicalTools.html @@ -4,43 +4,259 @@ - + + + Graphical Tools - + - - - - + + + + + + + - - - + + + - + + + + + + + + + + + + + + + + +
-