# one function
df1 <- data.frame(min = 0, max = pi, fun = I(list(sin)))
ggplot(df1, aes(xmin = min, xmax = max, y = fun)) +
stat_functions()
ggplot(df1, aes(xmin = min, xmax = max, y = fun)) +
stat_functions(geom = "point", n = 20)
# two functions
df2 <- data.frame(min = -pi, max = pi,
fun = I(list(sin, cos)), name = c("sin", "cos"))
# each function must be in a separate group for correct plotting of lines
ggplot(df2, aes(xmin = min, xmax = max, y = fun, group = after_stat(idx))) +
stat_functions()
ggplot(df2, aes(xmin = min, xmax = max, y = fun, colour = name)) +
stat_functions()
ggplot(df2, aes(xmin = min, xmax = max, y = fun)) +
stat_functions() +
facet_grid(~ name)
# two curves with same function
df3 <- data.frame(min = c(-pi, 0),
max = c(0,pi),
fun = I(list(sin, sin)),
name = c("negative", "positive"))
ggplot(df3, aes(xmin = min, xmax = max, y = fun, colour = name)) +
stat_functions()
# We use geom_debug() to see the computed values
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed) {
library(gginnards)
ggplot(df1, aes(xmin = min, xmax = max, y = fun)) +
stat_functions(geom = "debug")
}
Run the code above in your browser using DataLab