library(showtext)
showtext_auto()
set.seed(1)
bg_dots <- data.frame(
x = runif(6000, -20, 20),
y = runif(6000, -50, 100),
s = runif(6000, 1, 8)
)
bg_poly <- data.frame(
x = runif(4000, -20, 20),
y = runif(4000, -50, 100),
group = rep(1:400, each = 10)
)
bg_poly$fill_col <- rep(
sample(c("red", "darkred", "firebrick"), 400, replace = TRUE),
each = 10
)
ggplot(mtcars, aes(x = wt, y = mpg)) +
# Background polygons
geom_polygon(
data = bg_poly,
aes(x = x, y = y, group = group, fill = fill_col),
inherit.aes = FALSE,
alpha = 0.7,
color = "black",
linewidth = 0.3,
show.legend = FALSE
) +
# Background dots
geom_point(
data = bg_dots,
aes(x = x, y = y, size = s),
inherit.aes = FALSE,
color = "darkred",
alpha = 0.6,
show.legend = FALSE
) +
scale_fill_identity() +
# Density layer (separate fill scale)
ggnewscale::new_scale_fill() +
stat_bin2d(
bins = 25,
aes(fill = after_stat(count)),
alpha = 0.4,
show.legend = FALSE
) +
scale_fill_gradient(low = "red", high = "darkred") +
# Real data
geom_smooth(
aes(color = factor(am, levels = c(1, 0))),
se = FALSE,
method = "lm",
linetype = "dotted",
linewidth = 3
) +
geom_jitter(
aes(
color = factor(am, levels = c(1, 0)),
shape = factor(vs),
size = hp
),
width = 0.7,
height = 0.7,
stroke = 5,
alpha = 1
) +
facet_wrap(~gear) +
labs(
title = "1974 Motor Trend UNITED STATES OF THE AMERICAS",
subtitle = "visually aesthetic plot!!!!!",
x = "WEIGHT?????????????????",
y = "MILES per GALLLOOOOONS",
color = "TRANSMISSION CHAOS",
shape = "ENGINE OR SOMETHING",
size = "HORSEPOWER???"
) +
scale_color_manual(
values = c("cyan", "#39FF14"),
labels = c("Manual Mayhem", "Automatic Anarchy")
) +
scale_x_reverse() +
scale_y_continuous(trans = "sqrt") +
coord_cartesian(
xlim = c(6, 1),
ylim = c(35, 10),
clip = "off"
) +
theme(
legend.position = "right",
legend.background = element_rect(
fill = "black",
color = "chartreuse",
linewidth = 4
),
legend.text = element_text(
family = "mono",
color = "white",
size = 16
),
legend.key = element_rect(fill = "orange"),
text = element_text(
family = "Comic Sans MS",
size = 20,
face = "bold"
),
plot.title = element_text(
size = 30,
color = "green",
face = "italic"
),
plot.subtitle = element_text(
size = 25,
color = "yellow"
),
panel.background = element_rect(fill = NA),
plot.background = element_rect(fill = NA),
strip.background = element_rect(fill = NA),
strip.text = element_text(
size = 22,
color = "white",
face = "bold"
),
panel.grid.major = element_line(
color = "yellow",
linewidth = 3,
linetype = "dotdash"
),
panel.grid.minor = element_line(
color = "white",
linewidth = 2
),
axis.text.x = element_text(
angle = 270,
color = "chartreuse",
size = 22
),
axis.text.y = element_text(
angle = 180,
color = "gold",
size = 24
),
axis.ticks = element_line(
linewidth = 3,
color = "purple"
),
axis.ticks.length = unit(1.5, "cm"),
panel.spacing = unit(0.1, "cm")
) +
guides(
size = guide_legend(
override.aes = list(
color = "cyan",
fill = "cyan1",
alpha = 1,
size = 3
)
)
)