简体   繁体   English

无法在R中的ggplot上绘制多条lm行

[英]Unable to Plot Multiple lm Lines on ggplot in R

I am trying to plot multiple lm lines on a ggplot, but not even one seems to show up on it. 我正在尝试在ggplot上绘制多条lm线,但似乎没有出现。 I can't make heads or tails of it. 我不能做它的正面或反面。 I wanted to plot each lm line - representing 'medium', 'high' and 'low' from a categorical field - but not even a singular one is showing up. 我想绘制每条lm线-代表类别字段中的“中”,“高”和“低”,但没有一个出现。 Can someone guide me as to what I'm doing wrong? 有人可以指导我做错什么吗?

I have already tried using the geom_smooth() and stat_smooth() functions along with varying definitions of each function's method formulas, such as (method = "lm", formula = variable ~ value) but to no avail. 我已经尝试过使用geom_smooth()和stat_smooth()函数,以及每个函数的方法公式的不同定义,例如(method =“ lm”,formula = variable〜value),但无济于事。

data1B = data.frame(B_1=c(561.5806, 585.9286, 597.4839), B_2=c(780.5758, 
800.8750, 754.8788), B_3=c(767.4545, 771.6250, 778.6471), B_4=c(868.3448, 
1062.4000, 1184.4242), data.SWASH_group=c("low", "medium", "low"))

library(dplyr)
library(reshape2)

library(ggplot2)

d <- melt(data1B, id.vars="data.SWASH_group")
View(d)

# Everything on the same plot
ggplot(d, aes(variable, value, col=variable)) + geom_smooth(method="lm")+
geom_point(aes(col=data.SWASH_group, size=2)) +  
stat_smooth()

The result I am achieving is the following: 我实现的结果如下: 在此处输入图片说明

The result I want to achieve looks something like this: 我要实现的结果如下所示:

在此处输入图片说明

Note: The output is more dense because it has more data points to plot, instead of the sparse data points I provided in my example for recreation purposes. 注意:输出更加密集,因为它有更多的数据点可以绘制,而不是我在示例中出于休闲目的而提供的稀疏数据点。 Also, each lm line on the plot is representing one type of the categorical variable, such as 'high' and 'low' here more specifcally. 而且,图中的每条lm线代表一种类型的分类变量,例如在这里更具体地表示“高”和“低”。

make sure the variables (low and medium) are recognized as groups 确保将变量(低和中)识别为组

ggplot(d, aes(variable, value, col=variable, group=as.factor(variable)) +
geom_point(aes(col=data.SWASH_group, size=2)) +  
stat_smooth()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM