Skip to content

regression line on boxplots

3 messages · Yan Jiao, David Winsemius, Bernd Weiss

#
On Apr 1, 2011, at 11:24 AM, Yan Jiao wrote:

            
df <- structure(list(grp = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3),
   val = c(1L, 2L, 3L, 4L, 5L, 6L, 5L, 6L, 7L, 8L)),
  .Names = c("grp", "val"),
  row.names = c(NA, -10L),
  class = "data.frame")

  boxplot(val~grp,data=df)
  lm(val~grp, data=df)

#Call:
#lm(formula = val ~ grp, data = df)

#Coefficients:
#(Intercept)          grp
#    0.04348      2.21739

  ?abline
  abline(reg=lm(val~grp, data=df) )
#
Am 01.04.2011 11:24, schrieb Yan Jiao:
Your sample data does not make any sense (at least to me). I would do it 
as follows:

set.seed(8)
df <- data.frame(y = rnorm(100), x = factor(rep(1:4)))
boxplot(y ~ x, data = df)
regline <- lm(y ~ as.numeric(x), data = df)
abline(regline)

HTH,

Bernd