library(data.table)
library(reshape2)
library(ggplot2)
library(gridExtra)
library(extrafont)
data <- fread("data.csv", header = T)
data.h <- data[Brand == "HXXXX", !"Brand", with = F] # Hiding the brand name here for confidentiality
data.a <- data[Brand == "AXXXX", !"Brand", with = F]
# relevel the factor so that they will appear in the order I want
data.h[, Factor := factor(Factor, levels = c("Performance", "Approval", "Authority"))]
data.a[, Factor := factor(Factor, levels = c("Performance", "Approval", "Authority"))]
data.h.melt <- melt(data.h, id = "Factor")
data.a.melt <- melt(data.a, id = "Factor")
# One challenge here:
# I only want to show the label for the "Factor" variable (e.g. "Performance")
# If I do "facet_wrap( ~ Factor + Brand)" then the strip label would be like "Performance, HXXXX"
# And there is no labeller for facet_wrap right now so I had to create two sets of plots and then line them up
plot.h <- ggplot(data.h.melt, aes(variable, value, group = Factor)) + geom_point(size = 0.5, alpha = 0.01) +
geom_rect(aes(fill = Factor), colour = "gray10", size = 0.5, xmin = -Inf,xmax = Inf, ymin = 0, ymax = 1, alpha = 0.3, show_guide =FALSE) +
# Add bg color for the facet differing by the value of "Factor" variable
# Have to come before geom_line to make sure the line colors stay the same across facets
# A more complicated application can be found in:
# stackoverflow.com/questions/9847559/conditionally-change-panel-background-with-facet-grid
geom_line(size = 1, colour = "#6495ED") +
facet_wrap( ~ Factor, ncol = 1, scales = "free_x") +
theme_bw() +
labs(x = "", y = "") +
scale_x_discrete(labels = c("JAN-\nFEB", "MAR-\nAPR", "MAY-\nJUN", "JUL-\nAUG", "SEP-\nOCT", "NOV-\nDEC")) +
scale_y_continuous(limits=c(0, 1.01), expand = c(0,0)) +
# don't want the extra space below 0 and above 1.
scale_fill_manual(values = c("#A6C3DD", "#F6D5B9", "#FFFF00")) +
# bg colors for the facets
theme(
strip.text = element_text(size = 16, family = "Segoe UI", face = "bold"),
# Have to install the windows fonts first. See the github page for "extrafont" package:
# github.com/wch/extrafont
# Or find the supported font in R here:
# www.cookbook-r.com/Graphs/Fonts/
axis.text.x = element_text(size = 10, family = "Segoe UI"),
axis.text.y = element_text(size = 9, family = "Segoe UI"),
panel.grid.major = element_line(color = "gray10", size = 0.6),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(fill = NA, color = "gray10", size = 0.5, linetype = "solid"),
# Solid border for the panel. Have to add color for the geom_rect with same color and line size
# to make the top border appear.
strip.background = element_rect(fill = "white", colour = "white")
# Don't want the strip background color
)
# same plot with the other dataset
plot.a <- ggplot(data.a.melt, aes(variable, value, group = Factor)) + geom_point(size = 0.5, alpha = 0.01) +
geom_rect(aes(fill = Factor), colour = "gray10", size = 0.5, xmin = -Inf,xmax = Inf, ymin = 0, ymax = 1, alpha = 0.3, show_guide =FALSE) +
geom_line(size = 1, colour = "#6495ED") +
facet_wrap( ~ Factor, ncol = 1, scales = "free_x") +
theme_bw() +
labs(x = "", y = "") +
scale_x_discrete(labels = c("JAN-\nFEB", "MAR-\nAPR", "MAY-\nJUN", "JUL-\nAUG", "SEP-\nOCT", "NOV-\nDEC")) +
scale_y_continuous(limits=c(0, 1.01), expand = c(0,0)) +
scale_fill_manual(values = c("#A6C3DD", "#F6D5B9", "#FFFF00")) +
theme(
strip.text = element_text(size = 16, family = "Segoe UI", face = "bold"),
axis.text.x = element_text(size = 10, family = "Segoe UI"),
axis.text.y = element_text(size = 9, family = "Segoe UI"),
panel.grid.major = element_line(color = "gray10", size = 0.6),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(fill = NA, color = "gray10", size = 0.5, linetype = "solid"),
strip.background = element_rect(fill = "white", colour = "white")
)
# Do some manipulation to make the plot lined up.
# These steps are essential if the scale of Y variables differ greatly by plot.
# E.g. 0 - 1 and 0 - 1000000 (in this case the plot widths will be different)
p.h <- ggplot_gtable(ggplot_build(plot.h))
p.a <- ggplot_gtable(ggplot_build(plot.a))
pwidth.h <- p.h$widths[2:3]
pwidth.a <- p.a$widths[2:3]
max_width <- unit.pmax(pwidth.h, pwidth.a)
p.h$widths[2:3] <- max_width
p.a$widths[2:3] <- max_width
# Final step -- plot them as one set.
plot.all <- arrangeGrob(p.h, p.a, ncol = 2)
print(plot.all)
# A reminder -- Windows metafile (.EMF) does not support transparency so the background color
# will go away if the plot is saved in that format.
# Also, some fonts only work when saved as PDF, as mentioned in Cookbook for R
As a comparison, here is what the base plot looks like without all the tweaks.
ggplot(data2.melt, aes(variable, value, group = Brand)) + geom_point(size = 0.5, alpha = 0.01) +
geom_line(size = 1, colour = "#6495ED") + ylim(0, 1) +
facet_wrap( ~ Factor + Brand, ncol = 2, scales = "free") + theme_bw()