library(ggplot2)
library(ragg)
library(palmerpenguins)
devtools::session_info(c("ggplot2", "rag", "palmerpenguins"))
## Warning in system("timedatectl", intern = TRUE): running command 'timedatectl'
## had status 1
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 3.6.3 (2020-02-29)
## os Ubuntu 16.04.6 LTS
## system x86_64, linux-gnu
## ui X11
## language (EN)
## collate C.UTF-8
## ctype C.UTF-8
## tz Etc/UTC
## date 2020-08-30
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## ! package * version date lib source
## assertthat 0.2.1 2019-03-21 [1] RSPM (R 3.6.0)
## backports 1.1.8 2020-06-17 [1] RSPM (R 3.6.0)
## callr 3.4.3 2020-03-28 [1] RSPM (R 3.6.0)
## cli 2.0.2 2020-02-28 [1] RSPM (R 3.6.0)
## colorspace 1.4-1 2019-03-18 [1] RSPM (R 3.6.0)
## crayon 1.3.4 2017-09-16 [1] RSPM (R 3.6.0)
## desc 1.2.0 2018-05-01 [1] RSPM (R 3.6.0)
## digest 0.6.25 2020-02-23 [1] RSPM (R 3.6.0)
## ellipsis 0.3.1 2020-05-15 [1] RSPM (R 3.6.0)
## evaluate 0.14 2019-05-28 [1] RSPM (R 3.6.0)
## fansi 0.4.1 2020-01-08 [1] RSPM (R 3.6.0)
## farver 2.0.3 2020-01-16 [1] RSPM (R 3.6.0)
## ggplot2 * 3.3.2 2020-06-19 [1] RSPM (R 3.6.0)
## glue 1.4.1 2020-05-13 [1] RSPM (R 3.6.0)
## gtable 0.3.0 2019-03-25 [1] RSPM (R 3.6.0)
## isoband 0.2.2 2020-06-20 [1] RSPM (R 3.6.0)
## labeling 0.3 2014-08-23 [1] RSPM (R 3.6.0)
## lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.3)
## lifecycle 0.2.0 2020-03-06 [1] RSPM (R 3.6.0)
## magrittr 1.5 2014-11-22 [1] RSPM (R 3.6.0)
## MASS 7.3-51.5 2019-12-20 [2] CRAN (R 3.6.3)
## Matrix 1.2-18 2019-11-27 [2] CRAN (R 3.6.3)
## mgcv 1.8-31 2019-11-09 [2] CRAN (R 3.6.3)
## munsell 0.5.0 2018-06-12 [1] RSPM (R 3.6.0)
## nlme 3.1-144 2020-02-06 [2] CRAN (R 3.6.3)
## palmerpenguins * 0.1.0 2020-07-23 [1] RSPM (R 3.6.0)
## pillar 1.4.6 2020-07-10 [1] RSPM (R 3.6.0)
## pkgbuild 1.1.0 2020-07-13 [1] RSPM (R 3.6.0)
## pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 3.6.0)
## pkgload 1.1.0 2020-05-29 [1] RSPM (R 3.6.0)
## praise 1.0.0 2015-08-11 [1] RSPM (R 3.6.0)
## prettyunits 1.1.1 2020-01-24 [1] RSPM (R 3.6.0)
## processx 3.4.3 2020-07-05 [1] RSPM (R 3.6.0)
## ps 1.3.4 2020-08-11 [1] RSPM (R 3.6.0)
## R6 2.4.1 2019-11-12 [1] RSPM (R 3.6.0)
## R rag <NA> <NA> [?] <NA>
## RColorBrewer 1.1-2 2014-12-07 [1] RSPM (R 3.6.0)
## rlang 0.4.7 2020-07-09 [1] RSPM (R 3.6.0)
## rprojroot 1.3-2 2018-01-03 [1] RSPM (R 3.6.0)
## rstudioapi 0.11 2020-02-07 [1] RSPM (R 3.6.0)
## scales 1.1.1 2020-05-11 [1] RSPM (R 3.6.0)
## testthat 2.3.2 2020-03-02 [1] RSPM (R 3.6.0)
## tibble 3.0.3 2020-07-10 [1] RSPM (R 3.6.0)
## utf8 1.1.4 2018-05-24 [1] RSPM (R 3.6.0)
## vctrs 0.3.2 2020-07-15 [1] RSPM (R 3.6.0)
## viridisLite 0.3.0 2018-02-01 [1] RSPM (R 3.6.0)
## withr 2.2.0 2020-04-20 [1] RSPM (R 3.6.0)
##
## [1] /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6
## [2] /opt/R/3.6.3/lib/R/library
##
## R ── Package was removed from disk.
Medidas del tamaño de los pingüinos adultos en busca de alimento cerca de la estación Palmer, en la Antártida
Incluye medidas para especies de pingüinos, isla en el archipiélago de Palmer, tamaño (longitud de la aleta, masa corporal, dimensiones del pico) y sexo.
Una archivo de datos con 344 individuos y 8 variables:
Partes del cuerpo de los pingÜinos
species: Un factor que denota especies de pingüinos (Adelia, Chinstrap y Gentoo)
island: un factor que denota una isla en el archipiélago de Palmer, Antártida (Biscoe, Dream o Torgersen)
bill_length_mm: un número que indica la longitud del pico (en milÃmetros)
bill_depth_mm: un número que indica la ancho del pico (en milÃmetros)
flipper_length_mm: un número entero que denota la longitud de la aleta (milÃmetros)
body_mass_g: un número entero que denota la masa corporal (gramos)
sexo: un factor que denota el sexo del pingüino (hembra, macho)
año: un número entero que denota el año del estudio (2007, 2008 o 2009)
penguins <- palmerpenguins::penguins
penguins <- transform(na.omit(penguins),
species = factor(species),
island = factor(island),
sex = factor(sex),
year = factor(year)
)
ggplot(data = penguins) # data
ggplot(data = penguins) + # data
aes(x = bill_length_mm, y = bill_depth_mm) # variables
ggplot(data = penguins) + # data
aes(x = bill_length_mm, y = bill_depth_mm) + # variables
geom_point() # type of plot
ggplot(data = penguins, mapping = aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point()
ggplot(data = penguins) +
aes(x = bill_length_mm, y = bill_depth_mm, colour = species) +
geom_line()
ggplot(penguins) +
aes(x = bill_length_mm, y = bill_depth_mm) +
geom_line(aes(colour = species)) +
geom_point(aes(shape = sex)) # add line
ggplot(penguins) +
aes(x = flipper_length_mm, colour = species) +
geom_histogram(bins = 30) +
theme(legend.position = "bottom")
ggplot(penguins) +
aes(x = flipper_length_mm, fill = species) +
geom_histogram(bins = sqrt(nrow(penguins))) +
theme(legend.position = "bottom")
ggplot(penguins, aes(flipper_length_mm)) + geom_histogram(bins = sqrt(nrow(penguins)), aes(colour = species)) + theme(legend.position = "bottom")
ggplot(penguins, aes(flipper_length_mm, colour = species)) + geom_histogram(bins = sqrt(nrow(penguins)))
ggplot(penguins, aes(flipper_length_mm, colour = species)) + geom_freqpoly(bins = sqrt(nrow(penguins)))
ggplot(penguins, aes(bill_depth_mm, colour = species)) +
geom_freqpoly(binwidth = 0.5)
ggplot(penguins, aes(bill_depth_mm, colour = species)) +
geom_freqpoly(binwidth = 1)
ggplot(penguins, aes(flipper_length_mm, colour = species)) +
geom_freqpoly(binwidth = 0.5)
ggplot(penguins, aes(flipper_length_mm, colour = sex)) +
geom_histogram(binwidth = 0.5) +
facet_wrap(~species, ncol = 3)
ggplot(penguins, aes(flipper_length_mm, colour = species)) +
geom_histogram(binwidth = 0.5) +
facet_wrap(~sex, ncol = 2)
ggplot(penguins, aes(body_mass_g)) +
geom_freqpoly(aes(colour = species), binwidth = 100, na.rm = TRUE) +
xlim(2500, 6500) +
theme(legend.position = "bottom")
ggplot(penguins, aes(body_mass_g)) +
geom_histogram(aes(fill = species), binwidth = 100, position = "fill",
na.rm = TRUE) +
xlim(2500, 6500) +
theme(legend.position = "top")
ggplot(penguins) +
aes(x = flipper_length_mm, colour = species) +
geom_density(aes(fill = species))
ggplot(penguins, aes(body_mass_g, fill = species, colour = species)) +
geom_density(na.rm = TRUE) +
xlim(2500, 6500) +
theme(legend.position = "none")
ggplot(penguins, aes(body_mass_g, fill = species, colour = species)) +
geom_density(alpha = 0.7, na.rm = TRUE) +
xlim(2500, 6500) +
theme(legend.position = "left")
El conjunto de datos comprende longitudes (en minutos) de erupciones del géiser Old Faithful en el Parque Nacional Yellowstone, EE. UU. Los datos están dentro del intervalo [1,67,4,93].
Un marco de datos con 5625 observaciones y 2 variables:
eruptions: Tiempo de erupción en minutos
waiting: Tiempo de espera para la próxima erupción en minutos
Partes del cuerpo de los pingÜinos
ggplot(faithfuld, aes(eruptions, waiting)) +
geom_contour(aes(z = density, colour = ..level..))
ggplot(faithful, aes(waiting, eruptions)) +
geom_density_2d()
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled()
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(bins = 3)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(bins = 7)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(bins = 13)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(bins = 19)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(binwidth = 0.003)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(binwidth = 0.007)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(binwidth = 0.013)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(binwidth = 0.019)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(aes(colour = after_stat(level)))
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour(colour = "red")
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_raster(aes(fill = density)) +
geom_contour(colour = "white")
ggplot(faithfuld, aes(eruptions, waiting)) +
geom_raster(aes(fill = density))
small <- faithfuld[seq(1, nrow(faithfuld), by = 10), ]
ggplot(small, aes(eruptions, waiting)) +
geom_point(aes(size = density), alpha = 1/3) +
scale_size_area()
small <- faithfuld[seq(1, nrow(faithfuld), by = 10), ]
ggplot(small, aes(eruptions, waiting)) +
geom_point(aes(size = density), alpha = 1/3) +
scale_size_binned()
small <- faithfuld[seq(1, nrow(faithfuld), by = 10), ]
ggplot(small, aes(eruptions, waiting)) +
geom_point(aes(size = density), alpha = 1/3) +
scale_radius()
ggplot(penguins, aes(bill_length_mm, bill_depth_mm)) +
geom_point(alpha = 1 / 3)
ggplot(penguins, aes(bill_length_mm, bill_depth_mm)) +
geom_point(alpha = 1 / 3) +
xlab("Largo del pico del pingüino (mm)") +
ylab("Ancho del pico del pingüino (mm)")
ggplot(penguins, aes(bill_length_mm, bill_depth_mm)) +
geom_point(alpha = 1 / 3) +
xlab(NULL) +
ylab(NULL)
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, colour = species)) +
geom_jitter(width = 0.75, height = 0.75)
ggplot(penguins, aes(x = species, y = flipper_length_mm, colour = species)) +
geom_jitter(width = 0.50, height = 0.50) +
xlim("Adelie", "Chinstrap", "Gentoo") +
ylim(160, 240)
ggplot(penguins, aes(x = sex, y = body_mass_g, colour = species)) +
geom_jitter(width = 0.25, height = 0.50, na.rm = TRUE) +
ylim(NA, 6500)
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, colour = species)) +
geom_point(position = position_jitter(width = 0.75, height = 0.75))
ggplot(penguins, aes(x = species, y = flipper_length_mm, colour = species)) +
geom_point(position = position_jitter(width = 0.50, height = 0.50)) +
xlim("Adelie", "Chinstrap", "Gentoo") +
ylim(160, 240)
ggplot(penguins, aes(x = sex, y = body_mass_g, colour = species)) +
geom_point(position = position_jitter(width = 0.25, height = 0.25)) +
ylim(NA, 6500)
p <- ggplot(penguins, aes(flipper_length_mm, body_mass_g, colour = factor(species))) +
geom_point()
print(p)
# Save png to disk
ggsave("plot.png", p, width = 5, height = 5)
summary(p)
## data: species, island, bill_length_mm, bill_depth_mm,
## flipper_length_mm, body_mass_g, sex, year [333x8]
## mapping: x = ~flipper_length_mm, y = ~body_mass_g, colour = ~factor(species)
## faceting: <ggproto object: Class FacetNull, Facet, gg>
## compute_layout: function
## draw_back: function
## draw_front: function
## draw_labels: function
## draw_panels: function
## finish_data: function
## init_scales: function
## map_data: function
## params: list
## setup_data: function
## setup_params: function
## shrink: TRUE
## train_scales: function
## vars: function
## super: <ggproto object: Class FacetNull, Facet, gg>
## -----------------------------------
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
saveRDS(p, "plot.rds")
q <- readRDS("plot.rds")
df <- data.frame(
x = runif(n=5, min=1, max=5),
y = runif(n=5, min=2, max=6),
label = c("a","b","c","d","e")
)
p <- ggplot(df, aes(x, y, label = label)) +
labs(x = NULL, y = NULL) + # Hide axis label
theme(plot.title = element_text(size = 12)) # Shrink plot title
p + geom_point() + ggtitle("point")
p + geom_text() + ggtitle("text")
p + geom_bar(stat = "identity") + ggtitle("bar")
p + geom_tile() + ggtitle("raster")
p + geom_line() + ggtitle("line")
p + geom_area() + ggtitle("area")
p + geom_path() + ggtitle("path")
p + geom_polygon() + ggtitle("polygon")
ggplot(penguins, aes(sex, body_mass_g, group = species)) +
geom_point(aes(colour = species)) +
geom_line(aes(colour = species))
ggplot(penguins, aes(sex, body_mass_g, group = species)) +
geom_jitter(aes(colour = species)) +
geom_step(aes(colour = species))
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
PENGUINS <- data_summary(penguins, varname="body_mass_g",
groupnames=c("species", "sex"))
## Loading required package: plyr
head(PENGUINS)
ggplot(PENGUINS, aes(sex, body_mass_g, group = species)) +
geom_errorbar(aes(ymin=body_mass_g-sd, ymax=body_mass_g+sd), width=.1,
position=position_dodge(0.05)) +
geom_path(aes(colour = species))
ggplot(penguins, aes(flipper_length_mm, body_mass_g, group = species)) +
geom_point(aes(colour = species)) +
geom_smooth(method = "loess", se = TRUE)
## `geom_smooth()` using formula 'y ~ x'
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, colour = species)) +
geom_line(aes(group = island)) +
geom_smooth(formula = y ~ x, method = "lm", size = 2, se = FALSE)
ggplot(penguins, aes(fill = species)) +
aes(x = flipper_length_mm, y = ..density..) +
geom_histogram(bins = sqrt(nrow(penguins))) +
geom_density()
ggplot(penguins) +
aes(x = flipper_length_mm, color = species, fill = species) +
geom_density(alpha = 0.25) # add transparency
# Boxplot for one variable
ggplot(penguins) +
aes(x = "Longitud del pico", y = bill_length_mm) +
geom_boxplot() +
stat_summary(fun = mean, geom = "point", shape = rnorm(n = 1), size = runif(n = 1, min = 1, max = 10))
# Boxplot by factor
ggplot(penguins) +
aes(x = species, y = bill_length_mm) +
geom_boxplot(outlier.colour = rpois(n = 1, lambda = 100), outlier.shape = rnorm(n = 1), outlier.size = runif(n = 1,min = 1, max = 10)) +
stat_summary(fun = mean, geom = "point", shape = runif(1,1,10), size = rnorm(1))
ggplot(penguins) +
aes(x = species, y = bill_length_mm) +
geom_boxplot(varwidth = TRUE)
geom_jitter(alpha = 0.25, width = 0.2)
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_jitter
ggplot(penguins) +
aes(x = species, y = bill_length_mm) +
geom_boxplot(varwidth = TRUE) + # vary boxes width according to n obs.
geom_jitter(alpha = 0.25, width = 0.2) + # adds random noise and limit its width
facet_wrap(~year) # divide into 2 panels
ggplot(penguins) +
aes(x = species, y = bill_length_mm, fill = species) + # add color to boxes with fill
geom_boxplot(varwidth = TRUE) + # vary boxes width according to n obs.
geom_jitter(alpha = 0.25, width = 0.2) + # adds random noise and limit its width
facet_wrap(~year) + # divide into 2 panels
theme(legend.position = "none") # remove legend
ggplot(penguins, aes(species, flipper_length_mm)) +
geom_boxplot()
ggplot(penguins, aes(species, flipper_length_mm)) +
geom_boxplot() +
geom_line(colour = "#3366FF", alpha = 0.5)
ggplot(penguins, aes(species, flipper_length_mm)) +
geom_boxplot() +
geom_line(aes(group = sex), colour = "#3366FF", alpha = 0.5)
ggplot(penguins, aes(species, body_mass_g, color = sex)) + geom_jitter()
ggplot(penguins, aes(island, body_mass_g, color = species)) + geom_boxplot()
ggplot(penguins, aes(species, body_mass_g, color = sex)) + geom_violin()
ggplot(penguins) +
aes(x = species) +
geom_bar()
ggplot(penguins) +
aes(x = species, fill = species) + # add colors to bars
geom_bar() +
theme(legend.position = "none") # remove legend
ggplot(penguins) +
aes(x = species, fill = year) + # fill by years
geom_bar()
ggplot(penguins) +
geom_bar(aes(x = species, fill = year), position = "fill")
ggplot(penguins) +
geom_bar(aes(x = species, fill = year), position = "dodge")
p <- ggplot(data = penguins) +
aes(x = flipper_length_mm, y = body_mass_g, colour = species, shape = sex, alpha = bill_depth_mm) +
geom_point()
p + labs(
title = "Body mass for penguins species",
subtitle = "Period 2007-2009",
caption = "Data: palmerpenguins::penguins. See more at www.statsandr.com",
x = "flipper length (milimeters)",
y = "Body mass (grams)"
)
# Adjust ticks
p + scale_x_continuous(breaks = seq(from = 1, to = 60, by = 5.0)) + # x-axis
scale_y_continuous(breaks = seq(from = 2500, to = 6500, by = 1000)) # y-axis
p + scale_x_log10() +
scale_y_log10()
p + scale_x_continuous(limits = c(40, 50)) +
scale_y_continuous(limits = c(3000, 5000))
## Warning: Removed 333 rows containing missing values (geom_point).
p + aes(color = species) +
theme(legend.position = "top")
p + aes(color = species) +
theme(
legend.title = element_blank(),
legend.position = "bottom"
)
# Change shape of all points
ggplot(penguins) +
aes(x = bill_depth_mm, y = body_mass_g) +
geom_point(shape = 4)
# Change shape of points based on a categorical variable
ggplot(penguins) +
aes(x = flipper_length_mm, y = body_mass_g, shape = species) +
geom_point()
p <- ggplot(penguins) +
aes(x = flipper_length_mm, y = body_mass_g) +
geom_point()
# Change color for all points
p + geom_point(color = "steelblue")
# Change color based on a qualitative variable
p + aes(color = species)
# Change color based on a quantitative variable
p + aes(color = bill_length_mm)
# Change color based on a criterion (median of cty variable)
p + aes(color = bill_length_mm > median(bill_length_mm))
# Change size of all points
p + geom_point(size = 4)
# Change size of points based on a quantitative variable
p + aes(size = bill_depth_mm)
# Change transparency based on a quantitative variable
p + aes(alpha = bill_length_mm)
p + geom_point(size = 0.5) +
aes(color = bill_length_mm, shape = year, alpha = bill_depth_mm)
p + geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
p + geom_smooth(method = lm)
## `geom_smooth()` using formula 'y ~ x'
ggplot(penguins, aes(bill_depth_mm, body_mass_g)) +
geom_point() +
geom_smooth(span = 0.2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(penguins, aes(bill_length_mm, body_mass_g)) +
geom_point() +
geom_smooth(span = 1)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(penguins, aes(flipper_length_mm, body_mass_g)) +
geom_point() +
geom_smooth(span = 1.8)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(penguins, aes(bill_length_mm, body_mass_g)) +
geom_point() +
geom_smooth(method = "gam", formula = y ~ s(x))
p + aes(color = species, shape = species) +
geom_smooth(method = lm, se = FALSE)
## `geom_smooth()` using formula 'y ~ x'
# According to one variable
p + facet_grid(. ~ species)
# According to 2 variables
p + facet_grid(species ~ year)
p + facet_grid(. ~ species) +
geom_smooth(method = lm)
## `geom_smooth()` using formula 'y ~ x'
# Black and white theme
p + theme_bw()
# Minimal theme
p + theme_minimal()
# Classic theme
p + theme_classic()
theme_set(theme_minimal())
library(plotly)
##
## Attaching package: 'plotly'
## The following objects are masked from 'package:plyr':
##
## arrange, mutate, rename, summarise
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ggplotly(p + aes(color = year))
p_a <- ggplot(penguins) +
aes(x = bill_depth_mm, y = body_mass_g) +
geom_point()
p_b <- ggplot(penguins) +
aes(x = body_mass_g) +
geom_histogram()
p_c <- ggplot(penguins) +
aes(x = sex, y = body_mass_g) +
geom_boxplot()
library(patchwork)
p_a + p_b + p_c
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p_a / p_b / p_c
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
p_a + p_b / p_c
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
(p_a + p_b) / p_c
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# without flipping coordinates
p1 <- ggplot(penguins) +
aes(x = species, y = flipper_length_mm) +
geom_boxplot()
# with flipping coordinates
p2 <- ggplot(penguins) +
aes(x = species, y = flipper_length_mm) +
geom_boxplot() +
coord_flip()
library(patchwork)
p1 + p2 # left: without flipping, right: with flipping
ggplot(penguins) +
aes(x = species) +
geom_bar() +
coord_flip()
ggplot(penguins) +
aes(x = sex, y = bill_length_mm) +
geom_point()
ggsave("plot1.pdf")
## Saving 7 x 5 in image
library(maps)
##
## Attaching package: 'maps'
## The following object is masked from 'package:plyr':
##
## ozone
mi_counties <- map_data("county", "michigan") %>%
select(lon = long, lat, group, id = subregion)
head(mi_counties)
ggplot(mi_counties, aes(lon, lat)) +
geom_point(size = .25, show.legend = FALSE) +
coord_quickmap()
ggplot(mi_counties, aes(lon, lat, group = group)) +
geom_polygon(fill = "white", colour = "grey50") +
coord_quickmap()
library(ozmaps)
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2
library(rmapshaper)
## Registered S3 method overwritten by 'geojsonlint':
## method from
## print.location dplyr
## Warning: v8 Engine is version 3.14.5.9 but version >=6 is required for full functionality. Some rmapshaper functions, notably ms_clip() and ms_erase(), may not work. See https://github.com/jeroen/V8 for help installing a modern version of v8 on your operating system.
oz_states <- ozmaps::ozmap_states
oz_states
plot(quakes[c("long", "lat")], xlim = c(120, 190))
ozmap(add = TRUE)
ggplot(oz_states) +
geom_sf() +
coord_sf()
sf_oz <- ozmap_data("states")
## plot directly with ggplot2
library(sf)
if (utils::packageVersion("paletteer") < '1.0.0') {
pal <- paletteer::paletteer_d(package = "ochRe", palette = "namatjira_qual")
} else {
pal <- paletteer::paletteer_d(palette = "ochRe::namatjira_qual")
}
opal <- colorRampPalette(pal)
nmjr <- opal(nrow(sf_oz))
plot(st_geometry(sf_oz), col = nmjr)
library(ggplot2)
ggplot(sf_oz, aes(fill = NAME)) + geom_sf() + coord_sf(crs = "+proj=lcc +lon_0=135 +lat_0=-30 +lat_1=-10 +lat_2=-45 +datum=WGS84") + scale_fill_manual(values = nmjr)
ozmap("abs_ced", col = opal(nrow(abs_ced)))
ozmap("abs_ste", col = opal(nrow(abs_ste)))
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
kbor <- abs_lga %>% dplyr::filter(grepl("Kingborough", NAME))
bb <- st_bbox(kbor)
layout(matrix(c(1, 1, 1, 2, 2, 2, 2, 2, 2), nrow = 3))
plot(kbor, reset = FALSE, main = "Kingborough (TAS)")
rect(bb["xmin"], bb["ymin"], bb["xmax"], bb["ymax"])
library(mapdata)
#> Loading required package: maps
par(mar = rep(0, 4))
plot(c(145, 148.5), c(-43.6, -40.8), type = "n", asp = 1/cos(mean(bb[c(2, 4)]) * pi/180), axes = FALSE, xlab = "", ylab = "")
maps::map(database = "worldHires", regions = "australia", xlim = c(145, 148.5), ylim = c(-43.6, -40.8), add = TRUE)
rect(bb["xmin"], bb["ymin"], bb["xmax"], bb["ymax"])
# 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
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_states, colour = "black", fill = NA) +
geom_point(data = oz_capitals, mapping = aes(x = lon, y = lat), colour = "red") +
coord_sf()