我需要绘制多个显式函数定义以直观地比较它们。
考虑函数族:
fun.1 <- function(x) { 1 / ( 0.01 + x) }
fun.2 <- function(x) { 1 / ( 0.1 + x) }
fun.3 <- function(x) { 1 / ( 0.3 + x) }
这个想法是绘制评估这些函数的结果
关于以下输入:
xs = c(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048)
目前,我的代码是:
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")
结果是这样的
现在这看起来很难看,我无法让图例出现并且有警告:
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?
我想要的风格就像this。理想情况下,这些点应该是可见的
并由连续曲线连接。
问题是,创建具有多个函数且样式如下的绘图的最佳方法是什么this ?