r - 如何在不绘制原始数据的情况下向 ggplot 添加图例?

标签 r ggplot2

我绘制了多项式函数的图:y = x^2 - 6*x + 9

序列中的一系列多个点 + y 中的较小标准误差。我使用这些点从原始数据点构建该函数的样条模型,然后使用 R 的 Predict() 函数计算样条模型的导数,然后将两条样条曲线添加到图中。

顺便说一句,预期的导数函数是这样的:dy/dx = 2*x - 6

原始函数 I 为蓝色,一阶导数函数 I 为红色。我希望向这些图添加图例,但我发现这很困难,因为我没有为图分配任何点,因为我在 geom_smooth() 函数中声明了数据帧。

我使用的代码是这样的:

library(ggplot2)

# Plot the function: f(x) = x^2 - 6x + 9
# with a smooth spline:
# And then the deriviative of that function from predicted values of the 
# smoothed spline: f ' (x) = 2*x - 6

# Get a large sequence of x-values:
x <- seq(from = -10, to = 10, by = 0.01)

# The y-values are a function of each x value. 
y <- x^2 - 6*x + 9 + rnorm(length(x), 0, 0.5)

# Fit the curve to a model which is a smoothed spine. 
model <- smooth.spline(x = x, y = y)

# Predict the 1st derivative of this smoothed spline.
f_x <- predict(model, x = seq(from = min(x), to = max(x), by = 1), deriv = 1)

# Plot the smoothed spline of the original function and the derivative with respect to x.
p <- ggplot() + theme_bw() + geom_smooth(data = data.frame(x,y), aes(x = x, y = y), method = "loess", col = "blue", se = TRUE) + geom_smooth(data = data.frame(f_x$x, f_x$y), aes(x = f_x$x, y = f_x$y), method = "loess", col = "red", se = TRUE)

# Set the bounds of the plot.
p <- p + scale_x_continuous(breaks = scales::pretty_breaks(n = 20), limits = c(-5, 10)) + scale_y_continuous(breaks = scales::pretty_breaks(n = 20), limits = c(-10, 10))

# Add some axis labels 
p <- p + labs(x = "x-axis", y = "y-axis", title = "Original Function and predicted derivative function")

p <- p + scale_fill_manual(values = c("blue", "red"), labels = c("Original Function", "Derivative Function with respect to x"))

print(p)

我希望可以使用scale_fill_manual()添加图例,但我的尝试没有在图中添加图例。本质上,我得到的情节通常看起来像这样,减去我在绘画中添加的困惑图例。我想要那个传说,谢谢。

enter image description here

我这样做是因为我想向我的化学老师展示,我可以根据差示扫描量热法数据的点准确测量热容,我相信热容只是热流与温度的一阶导数图与温度有关的微分。

因此,我尝试绘制一个图,显示原始函数与关于 x 的一阶导数函数重叠,表明仅由拟合原始数据点的样条曲线绘制的一阶导数图可靠地生成了预期的线 dy/dx = 2 * x - 6,确实如此。

我只是想添加那个图例。

最佳答案

使用数据创建数据框并在美学范围内使用颜色是最常见的方法。

df <- rbind(
  data.frame(data='f(x)', x=x, y=y),
  data.frame(data='f`(x)', x=f_x$x, y=f_x$y))

p <- ggplot(df, aes(x,y, color=data)) + geom_smooth(method = 'loess')
p <- p + scale_x_continuous(breaks = scales::pretty_breaks(n = 20), limits = c(-5, 10)) + scale_y_continuous(breaks = scales::pretty_breaks(n = 20), limits = c(-10, 10))
p <- p + labs(x = "x-axis", y = "y-axis", title = "Original Function and predicted derivative function")
p <- p + scale_color_manual(name = "Functions", values = c("blue", "red"), labels = c("Original Function", "Derivative Function with respect to x"))
print(p)

关于r - 如何在不绘制原始数据的情况下向 ggplot 添加图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48902214/

相关文章:

r - 将函数应用于列表中的列表

r - 使用 read_excel(na = ) 如何指定多个 NA 字符串?

r - 使用 ggplot 绘制方差和置信区间

r - 从 R 中的 qdap 包结果中获取 all.polity 值

r - 如何在 R 中将所有列重命名为中间分隔符?

function - 评估 R 循环中的变量

r - 如何在 ggplot 中均匀分布 x 轴上的非连续日期?

r - 在条形图中保留未使用的级别

r - ggplot2 - 如何将带有箭头图标的geom_text分配给第二个y轴刻度

r - 使用 ggplot2 排序多面堆叠条形图