diff --git a/R/crime-scatmat.R b/R/crime-scatmat.R index de5f83de..db16a940 100644 --- a/R/crime-scatmat.R +++ b/R/crime-scatmat.R @@ -2,12 +2,19 @@ library(dplyr) library(ggplot2) library(car) -load(here("data", "crime.RData")) +load(here::here("data", "crime.RData")) scatterplotMatrix(crime[, 2:8], - ellipse = list(levels = 0.68), + ellipse = list(levels = 0.68, fill=FALSE), # regLine = list(lwd=3), # diagonal = list(method = "boxplot"), smooth = FALSE, plot.points = FALSE) + +scatterplotMatrix(crime[, 2:8], + plot.points = FALSE, + ellipse = list(levels = 0.68, fill=FALSE), + smooth = list(spread = FALSE, + lwd.smooth=2, lty.smooth = 1, col.smooth = "red"), + cex.labels = 2) diff --git a/R/penguin/peng-ggpcp.R b/R/penguin/peng-ggpcp.R new file mode 100644 index 00000000..1222a205 --- /dev/null +++ b/R/penguin/peng-ggpcp.R @@ -0,0 +1,74 @@ +#' --- +#' title: Penguin data, parallel coordinate plots +#' --- +#' + +library(ggplot2) +library(ggpcp) + +load(here::here("data", "peng.RData")) +source("R/penguin/penguin-colors.R") +cols <- peng.colors() + +# change levels of sex +peng <- peng |> + mutate(sex = factor(sex, labels = c("Female", "Male"))) + +# Fig 2 +# peng |> +# pcp_select(4,3,5:6, sex, species) |> # var selection (sec 3.1) +# pcp_scale(method="uniminmax") |> # scale values (sec 3.2) +# pcp_arrange() |> # arrange categ. data +# ggplot(aes_pcp()) + # create chart layers: +# geom_pcp_axes() + # vertical lines for axes +# geom_pcp(aes(colour = species), # line segments +# alpha = 0.8, overplot="none") + +# geom_pcp_labels() + # label categories +# labs(x = "", y = "") + +# theme_penguins() + +peng |> + pcp_select(4, 3, 5:6, sex, species) |> + pcp_scale(method = "uniminmax") |> + pcp_arrange() |> + ggplot(aes_pcp()) + + geom_pcp_axes() + + geom_pcp(aes(colour = species), alpha = 0.8, overplot = "none") + + geom_pcp_labels() + + scale_color_manual("Species", values = cols) + + theme_bw(base_size = 14) + + scale_x_discrete( + expand = expansion(add = 0.3), + labels = c( + "Bill Depth", "Bill Length", + "Flipper Length", "Body Mass", + "Sex", "Species" + ) + ) + + labs(x = "", y = "") + + theme(axis.title.y = element_blank(), axis.text.y = element_blank(), + axis.ticks.y = element_blank(), legend.position = "none") + +# reorder levels to put species and islands together +peng1 <- peng %>% + mutate(species = factor(species, levels = c("Chinstrap", "Adelie", "Gentoo"))) %>% + mutate(island = factor(island, levels = c("Dream", "Torgersen", "Biscoe"))) + +peng1 %>% + pcp_select(species, island, bill_length:body_mass) %>% + pcp_scale() %>% + pcp_arrange(method = "from-left") %>% + ggplot(aes_pcp()) + + geom_pcp_axes() + + geom_pcp(aes(colour = species), alpha = 0.6, overplot = "none") + + geom_pcp_boxes(fill = "white", alpha = 0.5) + + geom_pcp_labels() + + theme_bw() + + labs(x = "", y = "") + + theme( + axis.text.y = element_blank(), axis.ticks.y = element_blank(), + legend.position = "none" + ) + + scale_colour_manual(values = c(cols[2], cols[1], cols[3])) + +