# NOT RUN {
pc <- prcomp(USArrests, scale = TRUE)
# information about rotation
head(tidy(pc))
# information about samples (states)
head(tidy(pc, "samples"))
# information about PCs
tidy(pc, "pcs")
# state map
library(dplyr)
library(ggplot2)
pc %>%
tidy(matrix = "samples") %>%
mutate(region = tolower(row)) %>%
inner_join(map_data("state"), by = "region") %>%
ggplot(aes(long, lat, group = group, fill = value)) +
geom_polygon() +
facet_wrap(~ PC) +
theme_void() +
ggtitle("Principal components of arrest data")
au <- augment(pc, data = USArrests)
head(au)
ggplot(au, aes(.fittedPC1, .fittedPC2)) +
geom_point() +
geom_text(aes(label = .rownames), vjust = 1, hjust = 1)
# }
Run the code above in your browser using DataLab