I need to plot multiple explicit functions definitions to compare them visually.
Consider the family of functions:
fun.1 <- function(x) { 1 / ( 0.01 + x) }
fun.2 <- function(x) { 1 / ( 0.1 + x) }
fun.3 <- function(x) { 1 / ( 0.3 + x) }
The idea is to plot the result of evaluating these functions on the following inputs:
xs = c(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048)
Currently, my code is:
library(ggplot2)
library(dplyr)
xs = c(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048)
fun.1 <- function(x) { 1 / ( 0.01 + x) }
fun.2 <- function(x) { 1 / ( 0.1 + x) }
fun.3 <- function(x) { 1 / ( 0.3 + x) }
base <- ggplot(data.frame(x = xs), aes(x=as.factor(x)))
base + theme_classic() +
theme(
legend.position = c(.95, .95),
legend.justification = c("right", "top"),
legend.box.just = "right",
legend.margin = margin(6, 6, 6, 6)
) +
geom_function(fun = fun.1, colour = "red") +
geom_function(fun = fun.2, colour = "blue") +
geom_function(fun = fun.3, colour = "yellow") +
scale_x_discrete(name = "Input",
breaks = xs) +
scale_y_discrete(name = "Growth")
Now this looks ugly among other things, i can't make the legend to appear and there are warnings:
Multiple drawing groups in `geom_function()`. Did you use the correct `group`, `colour`, or `fill` aesthetics?
Multiple drawing groups in `geom_function()`. Did you use the correct `group`, `colour`, or `fill` aesthetics?
Multiple drawing groups in `geom_function()`. Did you use the correct `group`, `colour`, or `fill` aesthetics?
The style i'm trying to get is like this. Ideally, the points should be visible and connected by a continuous curve.
The question is, what is the best way to create a plot with multiple functions with a style like this ?

