【发布时间】:2019-01-01 22:32:48
【问题描述】:
我正在绘制关于暴露于不同处理的幼虫密度的数据。我有两个采样日(第 4 天和第 7 天)。我一直在尝试进行线性回归,但在绘图时遇到了一些问题。
four <- read.csv("four.csv", header = T)
df1 <- structure(list(day = structure(c(1L, 1L, 1L, 1L), .Label = "four", class = "factor"),
treat = c(0L, 10L, 100L, 300L), dens = c(1.2, 1.6, 1.883333333,
1.216666667)), .Names = c("day", "treat", "dens"), class = "data.frame", row.names = c(NA,
-4L))
mfour = lm(four$treat ~ four$dens)
plot(four$treat, four$dens)
abline(lm(four$treat ~ four$dens))
通过这段代码,我得到了这个 - 一条似乎没有相关性的回归线:
beads <- read.csv("beads.csv", header = T)
df2 <- structure(list(day = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("four", "seven"), class = "factor"), treat = c(0L,
10L, 100L, 300L, 0L, 10L, 100L, 300L), dens = c(1.2, 1.6, 1.883333333,
1.216666667, 1.833333333, 1.766666667, 1.4, 1.55)), .Names = c("day",
"treat", "dens"), row.names = c(NA, 8L), class = "data.frame")
p1 <- ggplot(beads, aes(x=treat, y=dens, col=day)) + geom_point() +
geom_smooth(method="lm", se=FALSE) +
ylab("dens") +
xlab("treat")
p1 <- p1 + theme_few() +
scale_colour_discrete(name="Day",
breaks=c("four", "seven"),
labels=c("Four", "Seven"))
p1
但是,当我使用 ggplot 时(因为我想将我的两个采样日与回归线一起包含在同一个数字上)我得到了一些看起来像相关性的东西:
我很困惑为什么会发生这种情况......有人有什么想法吗?
【问题讨论】:
-
您能否提供输入数据以便重现您的问题?您使用 ggplot2 显示的回归线肯定比使用基本 R 图形显示的回归线更有意义。
-
好吧,你在
abline(lm(four$treat ~ four$dens))中混淆了 x 和 y。切换treat和dens以获得正确的线路。公式是lm(y ~ x),而不是x ~ y。 -
@LAP 可能 OP 混淆了
plot(*)中的 x 和 y。
标签: r ggplot2 regression linear-regression