Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created April 16, 2020 00:39
Show Gist options
  • Save thoughtfulbloke/d4d1a66e7d8b25074a340ffe7517c682 to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/d4d1a66e7d8b25074a340ffe7517c682 to your computer and use it in GitHub Desktop.
library(dplyr)
library(ggplot2)
library(scales)
library(patchwork)
# random data, ordered
set.seed(2020)
series1_ind <- sort(sample(0:100, 25, replace=TRUE))
series2_ind <- sort(sample(1000:30000, 25, replace=TRUE))
example <- data_frame(step=1:25, series1_ind, series2_ind)
example$s1_cumulative = cumsum(example$series1_ind)
example$s2_cumulative = cumsum(example$series2_ind)
custom_y_axes <- theme(axis.text.y.left = element_text(color="#fdae61"),
axis.text.y.right = element_text(color="#2c7bb6"),
axis.title.y.left= element_text(margin =
margin(r=20),
color="#fdae61"),
axis.title.y.right= element_text(margin =
margin(l=20),
color="#2c7bb6")
)
# best linear match for some starting values
matchup <- lm(s1_cumulative ~ s2_cumulative, data=example)
coeff1 = matchup$coefficients[1]
coeff2 = matchup$coefficients[2]
# graph 1
g1c1 = coeff1
g1c2 = coeff2
g1 <- ggplot(example, aes(x = step)) + ylab("Series 1 cummulative") +
geom_line(aes(y = s1_cumulative), size = 1, color = "#fdae61") +
geom_line(aes(y= g1c1 + s2_cumulative * g1c2), colour= "#2c7bb6") +
scale_y_continuous(label=comma,
sec.axis = sec_axis(~./g1c2 - g1c1,
name = "Series 2 cummulative",
label=comma)) +
theme_minimal() + custom_y_axes + ggtitle("graph A")
# graph 2
g2c1 = 0
g2c2 = coeff2 * 0.2
g2 <- ggplot(example, aes(x = step)) + ylab("Series 1 cummulative") +
geom_line(aes(y = s1_cumulative), size = 1, color = "#fdae61") +
geom_line(aes(y= g2c1 + s2_cumulative * g2c2), colour= "#2c7bb6") +
scale_y_continuous(label=comma,
sec.axis = sec_axis(~./g2c2 - g2c1,
name = "Series 2 cummulative",
label=comma)) +
theme_minimal() + custom_y_axes + ggtitle("graph B")
# graph 3
g3c1 = 600
g3c2 = coeff2 * 0.1
g3 <- ggplot(example, aes(x = step)) + ylab("Series 1 cummulative") +
geom_line(aes(y = s1_cumulative), size = 1, color = "#fdae61") +
geom_line(aes(y= g3c1 + s2_cumulative * g3c2), colour= "#2c7bb6") +
scale_y_continuous(label=comma,
sec.axis = sec_axis(~./g3c2 - g3c1,
name = "Series 2 cummulative",
label=comma)) +
theme_minimal() + custom_y_axes + ggtitle("graph C")
# graph 4
example$log_s1_cumulative <- log(example$s1_cumulative, 10)
example$log_s2_cumulative <- log(example$s2_cumulative, 10)
logmatchup <- lm(log_s1_cumulative ~ log_s2_cumulative, data=example)
logcoeff1 = 3
logcoeff2 = matchup$coefficients[2]
g4 <- ggplot(example, aes(x = step)) + ylab("Series 1 cummulative (log10)") +
geom_line(aes(y = log_s1_cumulative), size = 1, color = "#fdae61") +
geom_line(aes(y= logcoeff1 + log_s2_cumulative * logcoeff2), colour= "#2c7bb6") +
scale_y_continuous(label=comma,
sec.axis = sec_axis(~./logcoeff2 - logcoeff1,
name = "Series 2 cummulative (log10)",
label=comma)) +
theme_minimal() + custom_y_axes + ggtitle("graph D")
(g1 + g2) / (g3 + g4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment