This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

data(Oxboys, package = "nlme")
head(Oxboys)
library(ggplot2)
ggplot(Oxboys, aes(age, height, group = Subject)) + 
  geom_point() + 
  geom_line()

ggplot(Oxboys, aes(age, height)) + 
  geom_point() + 
  geom_line()

ggplot(Oxboys, aes(age, height, group = Subject)) + 
  geom_line() + 
  geom_smooth(method = "lm", se = FALSE)
## `geom_smooth()` using formula 'y ~ x'

#> `geom_smooth()` using formula 'y ~ x'
ggplot(Oxboys, aes(age, height)) + 
  geom_line(aes(group = Subject)) + 
  geom_smooth(method = "lm", size = 2, se = FALSE)
## `geom_smooth()` using formula 'y ~ x'

#> `geom_smooth()` using formula 'y ~ x'
ggplot(Oxboys, aes(Occasion, height)) + 
  geom_boxplot()

ggplot(Oxboys, aes(Occasion, height)) + 
  geom_boxplot() +
  geom_line(colour = "#3366FF", alpha = 0.5)

ggplot(Oxboys, aes(Occasion, height)) + 
  geom_boxplot() +
  geom_line(aes(group = Subject), colour = "#3366FF", alpha = 0.5)

df <- data.frame(x = 1:3, y = 1:3, colour = c(1,3,5))

ggplot(df, aes(x, y, colour = factor(colour))) + 
  geom_line(aes(group = 1), size = 2) +
  geom_point(size = 5)

ggplot(df, aes(x, y, colour = colour)) + 
  geom_line(aes(group = 1), size = 2) +
  geom_point(size = 5)

xgrid <- with(df, seq(min(x), max(x), length = 50))
interp <- data.frame(
  x = xgrid,
  y = approx(df$x, df$y, xout = xgrid)$y,
  colour = approx(df$x, df$colour, xout = xgrid)$y  
)
ggplot(interp, aes(x, y, colour = colour)) + 
  geom_line(size = 2) +
  geom_point(data = df, size = 5)

ggplot(mpg, aes(class)) + 
  geom_bar()

ggplot(mpg, aes(class, fill = drv)) + 
  geom_bar()

ggplot(mpg, aes(class, fill = hwy)) + 
  geom_bar()

ggplot(mpg, aes(class, fill = hwy, group = hwy)) + 
  geom_bar()

ggplot(mpg, aes(displ, cty,group=displ)) + 
  geom_boxplot()

library(babynames)
## Warning: package 'babynames' was built under R version 4.0.5
hadley <- dplyr::filter(babynames, name == "Hadley")
ggplot(hadley, aes(year, n)) + 
  geom_line()

y <- c(18, 11, 16)
df <- data.frame(x = 1:3, y = y, se = c(1.2, 0.5, 1.0))

base <- ggplot(df, aes(x, y, ymin = y - se, ymax = y + se))
base + geom_crossbar()

base + geom_pointrange()

base + geom_smooth(stat = "identity")

base + geom_errorbar()

base + geom_linerange()

base + geom_ribbon()

# Unweighted
ggplot(midwest, aes(percwhite, percbelowpoverty)) + 
  geom_point()

# Weight by population
ggplot(midwest, aes(percwhite, percbelowpoverty)) + 
  geom_point(aes(size = poptotal / 1e6)) + 
  scale_size_area("Population\n(millions)", breaks = c(0.5, 1, 2, 4))

# Unweighted
ggplot(midwest, aes(percwhite, percbelowpoverty)) + 
  geom_point() + 
  geom_smooth(method = lm, size = 1)
## `geom_smooth()` using formula 'y ~ x'

#> `geom_smooth()` using formula 'y ~ x'

# Weighted by population
ggplot(midwest, aes(percwhite, percbelowpoverty)) + 
  geom_point(aes(size = poptotal / 1e6)) + 
  geom_smooth(aes(weight = poptotal), method = lm, size = 1) +
  scale_size_area(guide = "none")
## `geom_smooth()` using formula 'y ~ x'

#> `geom_smooth()` using formula 'y ~ x'
ggplot(diamonds, aes(depth)) + 
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(diamonds, aes(depth)) + 
  geom_histogram(binwidth = 0.1) + 
  xlim(55, 70)
## Warning: Removed 45 rows containing non-finite values (stat_bin).
## Warning: Removed 2 rows containing missing values (geom_bar).

#> Warning: Removed 45 rows containing non-finite values (stat_bin).
#> Warning: Removed 2 rows containing missing values (geom_bar).
ggplot(diamonds, aes(depth)) + 
  geom_freqpoly(aes(colour = cut), binwidth = 0.1, na.rm = TRUE) +
  xlim(58, 68) + 
  theme(legend.position = "none")

ggplot(diamonds, aes(depth)) + 
  geom_histogram(aes(fill = cut), binwidth = 0.1, position = "fill",
    na.rm = TRUE) +
  xlim(58, 68) + 
  theme(legend.position = "none")

ggplot(diamonds, aes(depth)) +
  geom_density(na.rm = TRUE) + 
  xlim(58, 68) + 
  theme(legend.position = "none")

ggplot(diamonds, aes(depth, fill = cut, colour = cut)) +
  geom_density(alpha = 0.2, na.rm = TRUE) + 
  xlim(58, 68) + 
  theme(legend.position = "none")

ggplot(diamonds, aes(clarity, depth)) + 
  geom_boxplot()

ggplot(diamonds, aes(carat, depth)) + 
  geom_boxplot(aes(group = cut_width(carat, 0.1))) + 
  xlim(NA, 2.05)
## Warning: Removed 997 rows containing missing values (stat_boxplot).

#> Warning: Removed 997 rows containing missing values (stat_boxplot).
ggplot(diamonds, aes(clarity, depth)) + 
  geom_violin()

ggplot(diamonds, aes(carat, depth)) + 
  geom_violin(aes(group = cut_width(carat, 0.1))) + 
  xlim(NA, 2.05)
## Warning: Removed 997 rows containing non-finite values (stat_ydensity).

#> Warning: Removed 997 rows containing non-finite values (stat_ydensity).
df <- data.frame(x = rnorm(2000), y = rnorm(2000))
norm <- ggplot(df, aes(x, y)) + xlab(NULL) + ylab(NULL)
norm + geom_point()

norm + geom_point(shape = 1) # Hollow circles

norm + geom_point(shape = ".") # Pixel sized

norm + geom_point(alpha = 1 / 3)

norm + geom_point(alpha = 1 / 5)

norm + geom_point(alpha = 1 / 10)

norm + geom_bin2d()

norm + geom_bin2d(bins = 10)

norm + geom_hex()

norm + geom_hex(bins = 10)

ggplot(diamonds, aes(color)) + 
  geom_bar()

ggplot(diamonds, aes(color, price)) + 
  geom_bar(stat = "summary_bin", fun = mean)

ggplot(diamonds, aes(table, depth)) + 
  geom_bin2d(binwidth = 1, na.rm = TRUE) + 
  xlim(50, 70) + 
  ylim(50, 70)

ggplot(diamonds, aes(table, depth, z = price)) + 
  geom_raster(binwidth = 1, stat = "summary_2d", fun = mean, 
    na.rm = TRUE) + 
  xlim(50, 70) + 
  ylim(50, 70)
## Warning: Raster pixels are placed at uneven horizontal intervals and will be
## shifted. Consider using geom_tile() instead.
## Warning: Raster pixels are placed at uneven vertical intervals and will be
## shifted. Consider using geom_tile() instead.

#> Warning: Raster pixels are placed at uneven horizontal intervals and will be
#> shifted. Consider using geom_tile() instead.
#> Warning: Raster pixels are placed at uneven vertical intervals and will be
#> shifted. Consider using geom_tile() instead.
ggplot(faithfuld, aes(eruptions, waiting)) + 
  geom_contour(aes(z = density, colour = ..level..))

ggplot(faithfuld, aes(eruptions, waiting)) + 
  geom_raster(aes(fill = density))

# Bubble plots work better with fewer observations
small <- faithfuld[seq(1, nrow(faithfuld), by = 10), ]
ggplot(small, aes(eruptions, waiting)) + 
  geom_point(aes(size = density), alpha = 1/3) + 
  scale_size_area()

library(ozmaps)
## Warning: package 'ozmaps' was built under R version 4.0.5
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1

oz_states <- ozmaps::ozmap_states
oz_states
ggplot(oz_states) + 
  geom_sf() + 
  coord_sf()

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.1     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## v purrr   0.3.4
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
oz_states <- ozmaps::ozmap_states %>% filter(NAME != "Other Territories")
oz_votes <- rmapshaper::ms_simplify(ozmaps::abs_ced)
## Registered S3 method overwritten by 'geojsonlint':
##   method         from 
##   print.location dplyr
#> Registered S3 method overwritten by 'geojsonlint':
#>   method         from 
#>   print.location dplyr
ggplot() + 
  geom_sf(data = oz_states, mapping = aes(fill = NAME), show.legend = FALSE) +
  geom_sf(data = oz_votes, fill = NA) + 
  coord_sf()

# filter electorates in the Sydney metropolitan region
sydney_map <- ozmaps::abs_ced %>% filter(NAME %in% c(
  "Sydney", "Wentworth", "Warringah", "Kingsford Smith", "Grayndler", "Lowe", 
  "North Sydney", "Barton", "Bradfield", "Banks", "Blaxland", "Reid", 
  "Watson", "Fowler", "Werriwa", "Prospect", "Parramatta", "Bennelong", 
  "Mackellar", "Greenway", "Mitchell", "Chifley", "McMahon"
))

# draw the electoral map of Sydney
ggplot(sydney_map) + 
  geom_sf(aes(fill = NAME), show.legend = FALSE) + 
  coord_sf(xlim = c(150.97, 151.3), ylim = c(-33.98, -33.79)) + 
  geom_sf_label(aes(label = NAME), label.padding = unit(1, "mm"))
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data

#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
#> give correct results for longitude/latitude data
oz_capitals <- tibble::tribble( 
  ~city,           ~lat,     ~lon,
  "Sydney",    -33.8688, 151.2093,  
  "Melbourne", -37.8136, 144.9631, 
  "Brisbane",  -27.4698, 153.0251, 
  "Adelaide",  -34.9285, 138.6007, 
  "Perth",     -31.9505, 115.8605, 
  "Hobart",    -42.8821, 147.3272, 
  "Canberra",  -35.2809, 149.1300, 
  "Darwin",    -12.4634, 130.8456, 
)

ggplot() + 
  geom_sf(data = oz_votes) + 
  geom_sf(data = oz_states, colour = "black", fill = NA) + 
  geom_point(data = oz_capitals, mapping = aes(x = lon, y = lat), colour = "red") + 
  coord_sf()

st_crs(oz_votes)
## Coordinate Reference System:
##   User input: EPSG:4283 
##   wkt:
## GEOGCRS["GDA94",
##     DATUM["Geocentric Datum of Australia 1994",
##         ELLIPSOID["GRS 1980",6378137,298.257222101,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["Horizontal component of 3D system."],
##         AREA["Australia including Lord Howe Island, Macquarie Islands, Ashmore and Cartier Islands, Christmas Island, Cocos (Keeling) Islands, Norfolk Island. All onshore and offshore."],
##         BBOX[-60.56,93.41,-8.47,173.35]],
##     ID["EPSG",4283]]
#> Coordinate Reference System:
#>   User input: EPSG:4283 
#>   wkt:
#> GEOGCRS["GDA94",
#>     DATUM["Geocentric Datum of Australia 1994",
#>         ELLIPSOID["GRS 1980",6378137,298.257222101,
#>             LENGTHUNIT["metre",1]]],
#>     PRIMEM["Greenwich",0,
#>         ANGLEUNIT["degree",0.0174532925199433]],
#>     CS[ellipsoidal,2],
#>         AXIS["geodetic latitude (Lat)",north,
#>             ORDER[1],
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         AXIS["geodetic longitude (Lon)",east,
#>             ORDER[2],
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>     USAGE[
#>         SCOPE["unknown"],
#>         AREA["Australia - GDA"],
#>         BBOX[-60.56,93.41,-8.47,173.35]],
#>     ID["EPSG",4283]]
st_crs(oz_votes) == st_crs(4283)
## [1] TRUE
#> [1] TRUE
ggplot(oz_votes) + geom_sf()

ggplot(oz_votes) + geom_sf() + coord_sf(crs = st_crs(3112))

edenmonaro <- ozmaps::abs_ced %>% filter(NAME == "Eden-Monaro")

p <- ggplot(edenmonaro) + geom_sf()
p + coord_sf(xlim = c(147.75, 150.25), ylim = c(-37.5, -34.5)) 

p + coord_sf(xlim = c(150, 150.25), ylim = c(-36.3, -36)) 

edenmonaro <- edenmonaro %>% pull(geometry)
st_bbox(edenmonaro)
##      xmin      ymin      xmax      ymax 
## 147.68741 -37.50503 150.23068 -34.53558
edenmonaro 
## Geometry set for 1 feature 
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 147.6874 ymin: -37.50503 xmax: 150.2307 ymax: -34.53558
## Geodetic CRS:  GDA94
## MULTIPOLYGON (((150.2307 -36.24468, 150.2287 -3...
st_cast(edenmonaro, "POLYGON")
## Geometry set for 2 features 
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 147.6874 ymin: -37.50503 xmax: 150.2307 ymax: -34.53558
## Geodetic CRS:  GDA94
## POLYGON ((150.2307 -36.24468, 150.2287 -36.2490...
## POLYGON ((148.1345 -36.74374, 148.1366 -36.7393...
dawson <- ozmaps::abs_ced %>% 
  filter(NAME == "Dawson") %>% 
  pull(geometry)
dawson
## Geometry set for 1 feature 
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 146.761 ymin: -21.21307 xmax: 149.9114 ymax: -19.18582
## Geodetic CRS:  GDA94
## MULTIPOLYGON (((147.8981 -19.85285, 147.899 -19...
#> Geometry set for 1 feature 
#> Geometry type: MULTIPOLYGON
#> MULTIPOLYGON (((148 -19.9, 148 -19.8, 148 -19.8...

ggplot(dawson) + 
  geom_sf() +
  coord_sf()

dawson <- st_cast(dawson, "POLYGON")
which.max(st_area(dawson))
## [1] 69
ggplot(dawson[-69]) + 
  geom_sf() + 
  coord_sf()

library(tidygraph)
## Warning: package 'tidygraph' was built under R version 4.0.5
## 
## Attaching package: 'tidygraph'
## The following object is masked from 'package:stats':
## 
##     filter
graph <- play_erdos_renyi(n = 10, p = 0.2) %>% 
  activate(nodes) %>% 
  mutate(class = sample(letters[1:4], n(), replace = TRUE)) %>% 
  activate(edges) %>% 
  arrange(.N()$class[from])

graph
## # A tbl_graph: 10 nodes and 16 edges
## #
## # A directed simple graph with 1 component
## #
## # Edge Data: 16 x 2 (active)
##    from    to
##   <int> <int>
## 1     8     2
## 2     8     7
## 3     8    10
## 4     8     9
## 5     2    10
## 6     2     5
## # ... with 10 more rows
## #
## # Node Data: 10 x 1
##   class
##   <chr>
## 1 d    
## 2 b    
## 3 b    
## # ... with 7 more rows
data(highschool, package = "ggraph")
head(highschool)
hs_graph <- as_tbl_graph(highschool, directed = FALSE)
hs_graph
## # A tbl_graph: 70 nodes and 506 edges
## #
## # An undirected multigraph with 1 component
## #
## # Node Data: 70 x 1 (active)
##   name 
##   <chr>
## 1 1    
## 2 2    
## 3 3    
## 4 4    
## 5 5    
## 6 6    
## # ... with 64 more rows
## #
## # Edge Data: 506 x 3
##    from    to  year
##   <int> <int> <dbl>
## 1     1    13  1957
## 2     1    14  1957
## 3     1    20  1957
## # ... with 503 more rows
luv_clust <- hclust(dist(luv_colours[, 1:3]))
luv_graph <- as_tbl_graph(luv_clust)
luv_graph
## # A tbl_graph: 1313 nodes and 1312 edges
## #
## # A rooted tree
## #
## # Node Data: 1,313 x 4 (active)
##   height leaf  label members
##    <dbl> <lgl> <chr>   <int>
## 1     0  TRUE  "101"       1
## 2     0  TRUE  "427"       1
## 3   778. FALSE ""          2
## 4     0  TRUE  "571"       1
## 5     0  TRUE  "426"       1
## 6     0  TRUE  "424"       1
## # ... with 1,307 more rows
## #
## # Edge Data: 1,312 x 2
##    from    to
##   <int> <int>
## 1     3     1
## 2     3     2
## 3     8     6
## # ... with 1,309 more rows
graph %>% 
  activate(nodes) %>% 
  mutate(centrality = centrality_pagerank()) %>% 
  arrange(desc(centrality))
## # A tbl_graph: 10 nodes and 16 edges
## #
## # A directed simple graph with 1 component
## #
## # Node Data: 10 x 2 (active)
##   class centrality
##   <chr>      <dbl>
## 1 c         0.234 
## 2 c         0.221 
## 3 b         0.216 
## 4 c         0.204 
## 5 c         0.0259
## 6 d         0.0244
## # ... with 4 more rows
## #
## # Edge Data: 16 x 2
##    from    to
##   <int> <int>
## 1    10     7
## 2    10     8
## 3    10     1
## # ... with 13 more rows
library(ggraph)
## Warning: package 'ggraph' was built under R version 4.0.5
ggraph(hs_graph) + 
  geom_edge_link() + 
  geom_node_point()
## Using `stress` as default layout

ggraph(hs_graph, layout = "drl") + 
  geom_edge_link() + 
  geom_node_point()

hs_graph <- hs_graph %>% 
  activate(edges) %>% 
  mutate(edge_weights = runif(n()))
ggraph(hs_graph, layout = "stress", weights = edge_weights) + 
  geom_edge_link(aes(alpha = edge_weights)) + 
  geom_node_point() + 
  scale_edge_alpha_identity()

ggraph(luv_graph, layout = 'dendrogram', circular = TRUE) + 
  geom_edge_link() + 
  coord_fixed()

ggraph(luv_graph, layout = 'dendrogram') + 
  geom_edge_link() + 
  coord_polar() + 
  scale_y_reverse()

ggraph(hs_graph, layout = "stress") + 
  geom_edge_link() + 
  geom_node_point(
    aes(filter = centrality_degree() > 2, 
        colour = centrality_power()),
    size = 4
  )

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.