scatterplot()
当我结合以下内容绘制我的计算时,我遇到了一个看起来很丑的重叠回归线的问题abline()
:
该错误似乎仅在线条切割上框架时发生。不幸的是,它发生在我的数据上。我给出了以下代码示例,重现了上面的情节:
# setting seed vector which reproduces the error
set.seed(684654)
# create data-frame
value1 <- rnorm(500,40,10)
value2 <- sqrt(rnorm(500,25,15)^2)
value3 <- sqrt(rnorm(500,10,15)^4)
df <- data.frame(value1, value2, value3)
# categorize quantils of value2
library(dplyr)
q <- quantile(value2)
df <- df %>%
mutate(cat=ifelse(value2 < q[2], "1st Qu.", NA),
cat=ifelse(value2 >= q[2] & value2 < q[3], "2nd Qu.", cat),
cat=ifelse(value2 >= q[3] & value2 < q[4], "3rd Qu.", cat),
cat=ifelse(value2 >= q[4], "4th Qu.", cat))
# regress model and save outcome to vector
lmf <- lm(log(value3) ~ value1 + factor(cat) - 1, data=df)
y.hat <- lmf$fitted
# scatterplot
library(car)
scatterplot(y.hat ~ value1 | cat,
smooth=FALSE, boxplots=FALSE, data=df,
grid=FALSE)
# regression line
library(graphics)
abline(lm(y.hat ~ df$value1), lwd=2, col="red")
矛盾的是,当我简单地绘制以下内容时,错误不会发生,所以我假设有点累积问题:
a <- log(rnorm(100,25,9))
b <- rnorm(100,1,.5)
scatterplot(a ~ b, smooth=FALSE, boxplots=FALSE, grid=FALSE)
abline(a=1, b=2, lwd=2, col="red")
有没有人有线索来解决这个问题并制作一个整洁的情节而无需 Photoshop?谢谢!
PS:我正在使用 R 3.4.0 和汽车 2.1-4