我在R中的以下步骤中得到两个输出: ggplot(mario_kart, aes(y = totalPr, x = duration, color=cond)) + geom_point() + geom_smooth(method = "lm", se = 0) ggplot( ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在根据拟合的lm模型使用geom_smooth方法lm和geom_line绘制一条线性线,但是它们没有绘制相同的曲线。
哪一个是对的? 我的代码中有错误吗?
下面的代码:
sales <- structure(list(NET_PRICE_TTC = c(59.988, 54.448, 54.99, 59.9895,
59.990666, 59.994, 54.454, 55.707428, 59.992, 49.3044, 59.988,
60.504, 44.9064, 40.336, 55.5855, 57.989599, 56.983999, 59.989714,
59.989714, 60.501, 60.504, 56.18057, 57.265091, 60.504, 59.9925,
47.077, 59.988, 60.504, 60.504, 53.97, 60.504, 59.988, 57.847714,
56.467999, 56.211199, 59.989999, 60.504, 57.48, 52.683, 49.898117,
60.504, 49.896, 59.997, 48.9, 57.49, 60.504, 49.483999, 59.9928,
60.504, 60.504, 60.504, 58.488, 50.55857, 47.136, 54.4512, 60.504,
59.988, 60.4968, 49.896, 48.9, 58.483999, 59.988, 60.5064, 52.459999,
49.901142, 59.991, 53.988, 51.900799, 60.375, 60.42857, 59.988,
52.504999, 59.988, 55.488, 59.988, 49.8975, 59.9892, 59.988,
60.5064, 56.718, 59.988, 59.989714, 59.623636, 59.988, 47.547157,
60.50057, 60.3492, 59.988, 59.991, 59.9928, 59.988, 59.988, 60.504,
59.988, 48.01457, 43.4904, 48.9, 59.988, 48.901333, 44.994, 56.990768,
56.2395, 59.988, 60.498, 59.989091, 59.988, 59.989999, 48.881777,
55.491, 59.9904, 59.986285, 56.443636, 52.9395, 59.988, 59.989714,
59.988, 45.86516, 51.310956, 60.504, 56.182285, 60.499636, 48.900666,
56.574666, 52.1415, 51.159, 59.988, 59.827999, 57.262908, 59.988,
60.5016, 59.988, 60.445333, 58.74, 57.48, 59.991, 50.349332,
60.282545, 52.941, 60.4992, 44.374736, 59.748, 59.988, 55.705714
), QTY = c(10, 6, 12, 16, 18, 4, 12, 14, 6, 20, 16, 12, 10, 6,
16, 30, 6, 14, 14, 8, 22, 14, 22, 12, 16, 48, 12, 2, 8, 20, 2,
6, 28, 12, 30, 12, 10, 8, 8, 34, 6, 10, 8, 18, 24, 10, 24, 10,
12, 6, 6, 6, 28, 56, 10, 4, 16, 10, 16, 28, 6, 10, 10, 24, 14,
16, 10, 30, 8, 14, 2, 24, 12, 4, 4, 16, 20, 18, 10, 16, 4, 14,
22, 10, 38, 14, 20, 24, 8, 10, 16, 2, 10, 16, 56, 40, 24, 12,
18, 4, 26, 16, 4, 4, 22, 10, 24, 54, 16, 10, 14, 22, 16, 10,
14, 14, 62, 46, 8, 14, 22, 36, 18, 16, 16, 2, 18, 22, 10, 10,
8, 18, 32, 6, 8, 18, 22, 8, 10, 38, 8, 8, 14)), .Names = c("NET_PRICE_TTC",
"QTY"), class = "data.frame", row.names = c(NA, -143L))
lin <- lm(QTY ~ NET_PRICE_TTC, sales)
lin_intercept = coef(lin)["(Intercept)"]
lin_price_coef = coef(lin)["NET_PRICE_TTC"]
demand_curve = data.frame(price_point = seq(45, 60, by = 1))
demand_curve %<>% mutate(quantity = lin_intercept + lin_price_coef * price_point)
ggplot(sales, aes(x = QTY, y = NET_PRICE_TTC)) +
geom_point() +
geom_line(data = demand_curve, aes(x = quantity, y = price_point)) +
geom_smooth(
method = lm,
color = "black",
se = FALSE,
fullrange = TRUE
)
您的代码中有错误。 在回归中,将QTY
用作NET_PRICE_TTC
的函数,而在ggplot
调用中,将x=QTY
和y=NET_PRICE_TTC
设置为(对geom_line
调用也是geom_line
)。
在ggplot
调用中翻转x
和y
ggplot
解决。 在这里,我将lm
拟合线lm
蓝色。
ggplot(sales, aes(y = QTY, x = NET_PRICE_TTC)) +
geom_point() +
geom_line(data = demand_curve, aes(y = quantity, x = price_point), col='blue', size=2) +
geom_smooth(
method = lm,
color = "black",
se = FALSE,
fullrange = TRUE
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.