Skip to content

Instantly share code, notes, and snippets.

View yjunechoe's full-sized avatar
🐣

June Choe yjunechoe

🐣
View GitHub Profile
@yjunechoe
yjunechoe / nasty_multi_pivot.R
Created November 4, 2022 18:11
nasty multi pivot
DF1 <- structure(list(Date = structure(c(1451865600, 1451865600, 1451865600,
1451865600, 1452988800, 1455148800, 1455148800, 1455494400, 1455494400,
1455494400, 1455494400, 1455667200, 1455667200, 1455667200, 1457308800,
1457308800, 1457308800, 1457308800, 1457827200, 1457827200, 1458604800,
1458604800, 1458604800, 1458604800, 1458691200, 1458691200, 1458691200,
1458691200, 1463875200, 1463875200, 1463875200, 1468281600, 1468281600,
1468281600, 1468281600, 1473465600, 1476316800, 1476316800, 1476316800,
1476316800, 1482192000, 1482192000, 1482192000, 1482192000), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Salesman = c("Edyta", "Edyta", "Edyta",
"Edyta", "Ewa", "Lena", "Lena", "Michal", "Michal", "Michal",
@yjunechoe
yjunechoe / runs_id.R
Created October 1, 2022 02:01
Assign unique ids to runs
library(dplyr, warn.conflicts = FALSE)
runs_id <- function(...) {
uq <- interaction(..., drop = TRUE)
runs <- rle(as.character(uq))$lengths
rep(seq_along(runs), times = runs)
}
mtcars %>%
select(cyl, am) %>%
@yjunechoe
yjunechoe / gtable_step
Created June 27, 2022 15:00
animating how the plot_table variable changes inside ggplot_gtable()
library(ggplot2)
library(ggtrace)
library(magick)
# Plot code from https://ggplot2-book.org/internals.html
p <- ggplot(mpg, aes(displ, hwy, color = drv)) +
geom_point(position = position_jitter(seed = 2022)) +
geom_smooth(method = "lm", formula = y ~ x) +
facet_wrap(vars(year)) +
ggtitle("A plot for expository purposes")
library(ggplot2)
library(ggtrace)
polar_plot <- ggplot(mtcars ,aes(hp, mpg)) +
geom_point() +
geom_smooth(method = "lm") +
expand_limits(y = c(0, 60)) +
coord_polar(start = 0, theta = "y")
with_ggtrace(
inner_fn <- function() {
cat("-- Entering Inner Function --\n")
# This step of the body is targeted by `at = 3L` in `trace()`
cat("-- Exiting Inner Function --\n")
return("inner_fn")
}
outer_fn <- function() {
cat("---- Entering Outer Function ----\n")
x <- inner_fn()
library(ggtrace)
library(ggplot2)
library(ggh4x)
# Example from - https://teunbrand.github.io/ggh4x/articles/Facets.html ----
## Base plot ====
p <- ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) + geom_point() +
labs(x = "Engine displacement", y = "Highway miles per gallon") +
guides(colour = "none")
library(ggtrace)
library(ggplot2)
library(grid)
p <- ggplot(na.omit(palmerpenguins::penguins)) +
aes(bill_length_mm, bill_depth_mm) +
geom_point(aes(color = sex)) +
facet_wrap(~ species, scales = "free")
angles <- rep(seq(0, 360, by = 15), times = 2)
@yjunechoe
yjunechoe / ggtrace-return
Last active February 7, 2022 21:29
inspect and modify return value of a ggproto method
library(ggtrace) # v0.4.8
library(ggplot2)
set.seed(20220207)
# Step 1: Make plot
barplot_plot <- ggplot(data = palmerpenguins::penguins) +
geom_bar(
aes(x = species, fill = species, group = interaction(species, island)),
color = "white", size = 2
library(ggplot2)
library(ggtrace)
library(grid)
p <- ggplot(mtcars, aes(mpg, hp, color = factor(cyl))) +
geom_point()
new <- with_ggtrace(
x = p,
method = ggplot2:::ggplot_gtable.ggplot_built,
@yjunechoe
yjunechoe / with_ggtrace-showcase.R
Created February 3, 2022 22:46
New with_ggtrace() function to {ggtrace}
library(ggtrace)
library(ggplot2)
boxplot_plot <- ggplot(na.omit(palmerpenguins::penguins)) +
aes(x = island, group = species) +
stat_count(
aes(fill = species),
position = position_fill(),
show.legend = FALSE
) +