Skip to content

Commit

Permalink
peng-ggpcp.R
Browse files Browse the repository at this point in the history
  • Loading branch information
friendly committed Nov 17, 2023
1 parent 53826a9 commit 83b0067
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
11 changes: 9 additions & 2 deletions R/crime-scatmat.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
74 changes: 74 additions & 0 deletions R/penguin/peng-ggpcp.R
Original file line number Diff line number Diff line change
@@ -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]))


0 comments on commit 83b0067

Please sign in to comment.