Skip to content

Instantly share code, notes, and snippets.

View thomas-neitmann's full-sized avatar

Thomas Neitmann thomas-neitmann

View GitHub Profile
@thomas-neitmann
thomas-neitmann / sarcomere_length_tension_curve.R
Created December 9, 2016 21:24
Plot the length-tension relationship of a sarcomere
# Plot length-tension curve of a sarcomere
length = c(1.2, 1.7, 2, 2.2, 3.65) # µm
force = c(0, 85, 100, 100, 0) # % of maximum force
xLabel = "Sarcomere Length (µm)"
yLabel = expression("Force (% "*P[0]*")")
svg("sarcomere_length_tension_curve.svg", width = 10, height = 5.5)
par(mar = c(4, 4, 0, 0.5) + 0.25) # decrease bottom, top and right margin
plot(length, force, xlab = xLabel, ylab = yLabel, type = "l", col = "steelblue",
bty = "n", xlim = c(0, 4), ylim = c(0, 105), lwd = 2, yaxs = "i", xaxs = "i")
@thomas-neitmann
thomas-neitmann / dplyr-strings.R
Last active December 8, 2019 06:52
If you ever tried to pass a string as an argument to a dplyr function you probably got frustrated. This just doesn't work. But don't give up, instead have a look at the solution below.
suppressMessages(library(dplyr))
data("mtcars")
mtcars %>% group_by(cyl) %>% summarise(n = n())
#> # A tibble: 3 x 2
#> cyl n
#> <dbl> <int>
#> 1 4 11
#> 2 6 7
#> 3 8 14
library(dplyr)
data(mtcars)
filter_a <- TRUE; filter_b <- TRUE; filter_c <- TRUE
### Variant 1 ###
d <- mtcars
if (filter_a) d <- filter(d, am == 0)
if (filter_b) d <- filter(d, hp > 10)
if (filter_c) d <- filter(d, cyl == 4)
library(ggcharts)
library(dplyr)
dreaded_lang <- tibble::tribble(
~language, ~pct,
"VBA", 75.2,
"Objective-C", 68.7,
"Assembly", 64.4,
"C", 57.5,
"PHP", 54.2,
library(ggplot2)
library(dplyr)
electrial_consumption <- tibble::tribble(
~country, ~pct,
"Italy", -27,
"Spain", -21,
"Belgium", -17,
"Austria", -16,
"France", -16,
@thomas-neitmann
thomas-neitmann / plot_ggcharts_star_history.R
Created May 19, 2020 17:21
A Data Visualization of the GitHub Star History of the {ggcharts} R Package created with {ggplot2}
library(ggplot2)
Sys.setlocale("LC_TIME", "English")
stars <- ghstars::get_pkg_star_history("ggcharts")
stars <- stars[stars$date <= as.Date("2020-05-19"), ]
ggplot(stars, aes(date, cumulative_stars)) +
geom_step(color = "darkorange", size = 1.5) +
annotate(
geom = "label",
label = " 100th Star by @JagersbergKnut",
`:=` <- function(lhs, rhs) {
var <- deparse(substitute(lhs))
if (exists(var, parent.frame(), inherits = FALSE)) {
stop("Variable `", var, "` is already defined.", call. = FALSE)
} else {
assign(var, rhs, parent.frame())
}
}
# An alternative version making use of `lockBinding`. This prevents users from overwriting
do_if <- function(data, predicate, expr) {
if (predicate) {
eval.parent(insert_data(
substitute(expr),
substitute(data)
))
} else {
data
}
}
library(ggplot2)
library(dplyr)
data(biomedicalrevenue, package = "ggcharts")
plot <- biomedicalrevenue %>%
filter(company %in% c("Roche", "Novartis")) %>%
ggplot(aes(year, revenue, color = company)) +
geom_line(size = 1.2) +
ggtitle(
paste0(
<span style = 'color:color name or hex code'>Text you want to color</span>