r - Why can't I fit a curve to this plot?
问题描述
I am trying to fit a curve to this plot, but am having trouble.
Code:
library(ggplot2)
data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")
lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))
lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")
Result:
解决方案
使用 ggplot2:geom_smooth lm method
library(ggplot2)
df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
# Fit a regression line. Change the method if you want the exponential fitting
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_smooth(method = "lm")
# If you just want to connect the dots
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_line()
推荐阅读
- react-native - react-native 中 mouseenter 和 mouseleave 事件的等价物是什么
- azure-resource-manager - 如何使用 ARM 在 Azure Databricks 中添加 RBAC
- javascript - 将 IF 语句压缩为似乎杀死了
- apache-kafka - 配置 kafka 监听器
- oracle - Is it possible to create master-detail relationship in Oracle Forms when both of the blocks are based on procedure?
- php - Need to insert in products table from more than one tables with dynamic user id
- selenium - 如何在 xpath 表达式中添加 OR 逻辑?
- c# - Oauth2 使用 ASP .net 核心 MVC 登录到 Google api
- javascript - Show all DOM nodes in html div
- sql - SequoiaDB查询时如何选择索引扫描或表扫描?