Skip to content

Instantly share code, notes, and snippets.

@RHDZMOTA
Last active April 7, 2021 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RHDZMOTA/8d41b5ea01c64d3bbae2 to your computer and use it in GitHub Desktop.
Save RHDZMOTA/8d41b5ea01c64d3bbae2 to your computer and use it in GitHub Desktop.
Create radar plot of the data: Consumption of pure alcohol by type of beverage. The following files are needed in the working directory: alcohol_data_download.R and data_cleaning.R
# Code to generate radar plot.
# Load required data and variables
source("alcohol_data_download.R")
source("data_cleaning.R")
# functions ---------------------------------------------------------------
library(ggplot2)
library(tibble)
# function provided by Erwan Le Pennec for the radar coord.
coord_radar <- function (theta = "x", start = 0, direction = 1) {
theta <- match.arg(theta, c("x", "y"))
r <- if (theta == "x") "y" else "x"
ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start,
direction = sign(direction),
is_linear = function(coord) TRUE)
}
# Data Visualization ------------------------------------------------------
# general palette
palg <- c("#008744", "#0057e7", "#d62d20", "#ffa700",
"#1b85b8", "#5a5255", "#559e83", "#ae5a41", "#c3cb71")
# A palette to identify the average country
pal <- replicate(length(unique(dataset$Country)) ,"#555555")
pal[avr_index] <- "red"
# General radar Plot
ggplot(data = NULL, aes(x = Beverage.Types, y = Numeric) ) + theme_minimal() +
geom_polygon(data = dataset, aes(group = Country, color = Country),
fill = NA) +
geom_line( data = dataset, aes(group = Country, color = Country),
alpha = 0.5, show.legend = F) +
geom_polygon(data = avrg_df, aes(group = Country, color = Country),
fill = NA, alpha = 0.2, size = 1, show.legend = F) +
geom_line( data = avrg_df, aes(group = Country, color = Country),
color = "red", size = 1) +
ggtitle("General Consumption of Alcohol by Type of Beverage in 2010") +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
guides(color = guide_legend(ncol = 4))+
scale_color_manual(values = pal) +
xlab("") + ylab("") +
coord_radar()
# Select some random data
n <- 5
random_index <- floor(runif(n) * (length(countries_unique) - 1)) + 1
country_random <- sapply(random_index, function(x) countries_unique[x])
random_dataset <- numeric()
for(i in country_random){
random_dataset <- rbind(random_dataset, dataset[dataset$Country == i, ])
}
random_dataset <- random_dataset[order(random_dataset$Beverage.Types), ]
# Select even more random data
n <- 12
random_index <- floor(runif(n) * (length(countries_unique) - 1)) + 1
country_random <- sapply(random_index, function(x) countries_unique[x])
randomm_dataset <- numeric()
for(i in country_random){
randomm_dataset <- rbind(randomm_dataset, dataset[dataset$Country == i, ])
}
randomm_dataset <- randomm_dataset[order(randomm_dataset$Beverage.Types), ]
# Select possible interesting countries
country_choice <- c("France", "Germany", "Russia")
selected_dataset <- numeric()
for(i in country_choice){
selected_dataset <- rbind(selected_dataset, dataset[dataset$Country == i, ])
}
selected_dataset <- selected_dataset[order(selected_dataset$Beverage.Types), ]
# randomly selected radar plot
ggplot(random_dataset, aes(x = Beverage.Types, y = Numeric) ) +
geom_polygon(aes(group = Country, color = Country),
fill = NA, show.legend = FALSE,
size = 1.1, alpha = 0.3) +
geom_line( aes(group = Country, color = Country),
size = 1.1, alpha = 0.3) +
guides(color = guide_legend(ncol = 1))+
theme_minimal() +
coord_radar() +
xlab("") + ylab("") +
ggtitle("Preference of 5 random countries: alcohol consumption in 2010") +
scale_color_manual(values = palg[1:length(country_random)]) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)),
axis.ticks.y = element_blank(),
axis.text.y = element_blank())
# selected radar plot
ggplot(selected_dataset, aes(x = Beverage.Types, y = Numeric) ) +
geom_polygon(aes(group = Country, color = Country),
fill = NA, show.legend = FALSE,
size = 1.1, alpha = 0.5) +
geom_line( aes(group = Country, color = Country),
size = 1.1, alpha = 0.5) +
guides(color = guide_legend(ncol = 1))+
theme_minimal() +
coord_radar() +
xlab("") + ylab("") +
ggtitle("Preference of 3 countries: alcohol consumption in 2010") +
scale_color_manual(values = palg[1:length(country_choice)]) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)),
axis.ticks.y = element_blank(),
axis.text.y = element_blank())
# random data plotted in radar plot
ggplot(data = randomm_dataset, aes(x = Beverage.Types, y = Numeric) ) + theme_light() +
geom_polygon(aes(group = Country, color = Country),
fill = NA, size = 2) +
facet_wrap(~ Country) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
guides(color = "none")+
xlab("") + ylab("") +
coord_radar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment