خرید بک لینک

Vote count: 0

I'm using R to do an analysis of the mtcars data set and I was surprised when I couldn't add a regression line for a factor variable with ggplot2. Specifically the following code takes the automatic transmission column am and creates an additional column of type, factor.

library(ggplot2)
mtcars$automaticTransmission <- as.factor(mtcars$am)
levels(mtcars$automaticTransmission) <- c('automatic', 'manual')

The name of the new column is manualTransmission and when I run the following code:

g <- ggplot(data=mtcars)
g <- g + geom_point() + aes(x=automaticTransmission, y=mpg)
g <- g + geom_smooth(method=lm)
g

geom_smooth fails to produce a regression line in my plot. However, if I use the original am column, then it's there:

g <- ggplot(data=mtcars)
g <- g + geom_point() + aes(x=am, y=mpg)
g <- g + geom_smooth(method=lm)
g

Can anyone tell me why this is? I'm guessing it has less to do with R/ggplot2 and more with misunderstanding something more fundamental, but I'm just leaing statistics, so I'm not really sure.

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 19 فروردين 1395 ساعت: 23:16

صفحه بندی