566 HOMEWORK 1
# 566 HOMEWORK 1
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.5 ✔ purrr 0.3.4
## ✔ tibble 3.1.6 ✔ dplyr 1.0.8
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggthemes)
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
library(ggrepel)
library(viridis)
## Loading required package: viridisLite
##
## Attaching package: 'viridis'
## The following object is masked from 'package:scales':
##
## viridis_pal
library(geomtextpath)
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
# help(Boston)
boston <- Boston %>%
filter(factor(rad) != "24")
points.boston <- ggplot(data = boston,
mapping = aes(x = crim,
y = medv*1000,
color = factor(rad))) +
geom_point(alpha = 0.5,
shape = 16) +
geom_smooth(method = "loess", se = FALSE, size = 3) +
theme_minimal(base_size = 12) +
scale_x_log10() +
scale_y_log10() +
xlab("Crime rate (per 100,000 people)") +
ylab("Median value of homes (1978)") +
scale_y_continuous(trans = "log10",
label = scales::dollar_format()) +
scale_x_continuous(trans = "log10") +
theme(legend.position = "none")
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
# find line ends for direct labeling
line.ends <- ggplot_build(points.boston)$data[[2]] %>%
group_by(colour) %>%
filter(x==max(x))
## `geom_smooth()` using formula 'y ~ x'
# add label for index for access to radial highways
line.ends$rad <-
boston %>%
pull(rad) %>%
unique() %>%
as.character() %>%
sort()
# points.boston
# add direct labels to graph
points.boston2 <- points.boston + ggrepel::geom_label_repel(data = line.ends,
aes(x = 10^line.ends$x,
y = 10^line.ends$y,
label = rad),
nudge_x = 2,
label.size = NA,
fill = alpha(c("white"),0)) +
scale_color_viridis(discrete = TRUE) +
ggtitle("Boston Crime Rate by Median Home Value and \nIndex (1 = Low, 8 = High) of Access to Highways")
points.boston2
## `geom_smooth()` using formula 'y ~ x'
pdf("HW1_geom_points.pdf", height = 4.5, width = 7)
print(points.boston2)
## `geom_smooth()` using formula 'y ~ x'
dev.off()
## quartz_off_screen
## 2