Al14
Al14

Reputation: 1814

linear model of columns subset

I am trying lm of a two subsetted column of a data.frame. here my example with mtcars. What's wrong? Thanks for the help

ggplot(mtcars) + 
  geom_jitter(aes(y=mtcars[c(1:10), "mpg"], x=mtcars[c(1:10), "cyl"]), colour="blue") +
  geom_smooth(aes(mtcars[c(1:10), "mpg"], mtcars[c(1:10), "cyl"]), method=lm, se=FALSE)

I get this error

Error in data.frame(x = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6), y = c(21, 21,  : 
  arguments imply differing number of rows: 10, 32

Upvotes: 0

Views: 70

Answers (1)

LyzandeR
LyzandeR

Reputation: 37889

I think this is what you are trying to do:

ggplot(mtcars[1:10,]) + 
  geom_jitter(aes(y=mpg, x=cyl, colour="blue")) +
  stat_smooth(aes(y=mpg, x=cyl), method='lm', se=FALSE)

Basically, you since you 're using ggplot(mtcars) you just need to use the column names later on. Also, I think you meant to use stat_smooth instead of geom_smooth.

Output:

enter image description here

Upvotes: 2

Related Questions