data(Guerry, package = "Guerry") library(dplyr) library(ggplot2)
In his discussion, Guerry often referred to differences among the Regions of France.
One useful display to address this is a profile plot of the means of the moral
variables for each Region
, typically shown as a line plot of the mean value
over the variables. Another term used for this is a parallel coordinates plot.
First, find the means of each variable.
guerry_sumry <- Guerry |> select(Region, Crime_pers:Suicides) |> filter(!is.na(Region)) |> mutate(Region = factor(Region, labels =c("Central", "East", "North", "South", "West"))) |> group_by(Region) |> summarise(Crime_pers= mean(Crime_pers), Crime_prop = mean(Crime_prop), Literacy = mean(Literacy), Donations = mean(Donations), Infants = mean(Infants), Suicides = mean(Suicides), ) |> ungroup() |> mutate_at(vars(-Region), scales::rescale) guerry_sumry |> knitr::kable(digits = 2)
Even in this simple table, the regions that stand out as best (1) and worst (0) can be seen. In particular, the Central, North and West are highest on personal crime, while personal crime is highest in the Central region and lowest in the South.
A plot of these means by region can be produced with the ggpcp
package.
#| fig.width = 8 library(ggpcp) guerry_sumry |> pcp_select(Region, Crime_pers:Suicides) |> pcp_scale(method="uniminmax") |> pcp_arrange() |> ggplot(aes_pcp()) + geom_pcp_axes() + geom_pcp(aes(colour = Region), linewidth = 2) + geom_pcp_labels() + xlab("Variable") + ylab("Percent of maximum") + theme_bw(base_size = 14) + theme(legend.position = 'bottom')
A more attractive form for this display shows the axes for each variable in polar coordinates,
known as a radar chart. Because the variables are on different scales, a first step is to
normalize them, by scaling each to a range of (0,1). The scales::rescale()
function handles this.
Here, I use the ggradar
package by Ricardo Bion. It is not on CRAN, so you may need to install
it to reproduce the figure here.
#| fig.width = 8 if(!require(ggradar)) remotes::install_github("ricardo-bion/ggradar") library(ggradar) # Create radar charts using ggplot2 guerry_sumry |> ggradar( grid.label.size = 4, axis.label.size = 3, axis.label.offset = 1.2, group.point.size = 4, plot.extent.x.sf = 1, plot.extent.y.sf = 1.2, fill = TRUE, fill.alpha = 0.2, legend.position = "bottom", legend.title = "Region", legend.text.size = 12, plot.title = "Guerry data: Means by Region")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.