From time to time we all need some fun, so why not make some funny graphs since we are R users, yeah! This post is based on the tutorials on R CHARTS, do not forget to pick a look.
To add Doggos to an old fashioned bar chart need to install
ggdogs, run the next code we need
devtools.
#devtools::install_github("R-CoderDotCom/ggdogs@main")
Create some fake data, as sometimes real data is overrated
# Packages
library(ggplot2)
library(ggdogs)
## ~~ Package ggdogs
## Visit https://r-charts.com/ for R dataviz tutorials :) ~~
# Some fake data
fake_data <- data.frame(
treatment = c("1", "2", "3"),
mean = c(0.3, 10, 5),
de = c(.05, 0.1, 3)
)
Represent data with bar chart. There are things that even columnsand
deviation bars cannot hide, so we use geom_dog() to
represent significant differences, geom_errorbar to include
deviation bars and geom_label() to put some text:
ggplot(fake_data, aes(x = treatment, y = mean)) +
geom_col(fill = "lightskyblue4", color = "black", width = 0.4, size = 0.6) +
geom_errorbar(
aes(ymin = mean - de, ymax = mean + de), width = 0.1, color = "black", size = 0.6
) +
geom_dog(
aes(
dog = c("chihuahua", "surprised", "thisisfine"),
y = c(1.6, 11.1, 9)
),
size = 4
) +
geom_label(aes(x = "3", y = 10.5, label = "this is fine")) +
ylim(c(0, 12)) +
labs(
x = element_blank(),
y = "The average response"
) +
scale_x_discrete(labels = c("Control (-)", "Control (+)", "My best treatment")) +
theme_classic() +
theme(
axis.text.x = element_text(color = "black", size = 11, face = "bold"),
axis.text.y = element_text(color = "black", size = 11),
axis.title = element_text(face = "bold", color = "black", size = 13)
)
We can even save our graph in working folder using
ggsave().
ggsave(
filename = "dogo_bar_en.png",
dpi = 300,
height = 5,
width = 7.5,
units = "cm")
Moving further let’s do something more advanced, let’s animate graph
and include some cats. For this we require to load ggcats
and gganimatepackages. To install ggcats run
the next code:
#devtools::install_github("R-CoderDotCom/ggcats@main")
Now let’s simulate data, for this generate some easy data sets
# Packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(ggthemes)
library(gganimate)
library(ggcats)
## ~~ Package ggcats
## Visit https://r-coder.com/ for R tutorials ~~
# A function to generate a data frame with simulated data and cats
data_cat <- function(from = 0, to = 80, by = 1, fun = rnorm,
cat = "", sd = 3, category = "") {
tibble(
x = seq(from, to, by),
y = fun(x) + rnorm(length(x), sd = sd),
category = rep(category, length(x)),
cat = rep(cat, length(x))
)
}
The above function allows to generate data within sequence and based
on that sequence and function, numbers with some noise are calculated,
cat used to specify a cat name included in
ggcats and with category one can set anyuthing
of your interest and that will be repeated in the data set in its own
column. Later we generate animation.
The next step is make the data sets, one for the circadian cycle that is my focus during, I don’t know, all time, another for my duties, self-imposed or not, and one more to represent my faithful companion anxiety:
# Data for my focus
concentration <- data_cat(
fun = function(x) 4 * sin(1.5 * x) + 4,
cat = "lil_bub",
sd = 1, category = "focus"
)
# Data for my duties
set.seed(10)
duties <- data_cat(
fun = function(x) 5 + 1.5 * exp(x / 20),
cat = "pusheen_pc",
sd = 2, category = "duties"
)
# Data for my anxiety
anxiety <- data_cat(
fun = function(x) 10 + exp(x / 15) + 4 * sin(x),
cat = "nyancat",
sd = 1, category = "anxiety"
)
# Complete data
full_data <- rbind(concentration, duties, anxiety)
To know more about anonymous functions checkout the link: Anonymous functions
We use rbind() to join all data sets for next step,
i.e., to generate animation itself
ggplot(full_data, aes(x, y)) +
geom_line(aes(color = category), size = 1) +
geom_cat(aes(cat = cat), size = 4) +
labs(
y = element_blank(),
x = "Time",
) +
scale_color_manual(
values = c("#EE2C2C", "#FF8C00", "#68228B"),
labels = c("Anxiety", "My focus", "Duties")) +
theme_fivethirtyeight() +
theme(
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.x = element_text(size = 12, color = "black"),
axis.title.x = element_text(size = 14, face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
legend.position = "top",
legend.title = element_blank()
) +
transition_reveal(x)