2013-02-19 95 views
1

我有一些来自R课程的数据。教授正在使用基础图形手动添加每种类型的线。我想用ggplot2来做。ggplot2与分类变量的lm行

到目前为止,我已经在不同地区创建一个facet“d情节ggplothungerscatter plots和还分别安装了一个模型来的数据。该特定模型具有图中变量x与变量group/colour之间的交互项。

我现在想要做的是为每个面板绘制一个模型。我可以通过使用geom_abline并将slopeintercept定义为系数2的总和(因为组的分类变量具有0/1值并且在每个面板中只有一些值乘以1) - 但是这似乎是不容易。

我试过在lm中使用的相同方程stat_smooth没有运气,我得到一个错误。

理想情况下,我想人们可以把方程式变成stat_smooth并让ggplot做所有的工作。一个人会怎么做呢?

download.file("https://sparkpublic.s3.amazonaws.com/dataanalysis/hunger.csv", 
        "hunger.csv", method = "curl") 
hunger <- read.csv("hunger.csv") 
hunger <- hunger[hunger$Sex!="Both sexes",] 
hunger_small <- hunger[hunger$WHO.region!="WHO Non Members",c(5,6,8)] 
q<- qplot(x = Year, y = Numeric, data = hunger_small, 
      color = WHO.region) + theme(legend.position = "bottom") 
q <- q + facet_grid(.~WHO.region)+guides(col=guide_legend(nrow=2)) 
q 

# I could add the standard lm line from stat_smooth, but I dont want that 
# q <- q + geom_smooth(method="lm",se=F) 

#I want to add the line(s) from the lm fit below, it is really one line per panel 
lmRegion <- lm(hunger$Numeric ~ hunger$Year + hunger$WHO.region + 
        hunger$Year *hunger$WHO.region) 

# I also used a loop to do it, as below, but all in one panel 
# I am not able to do that 
# with facets, I used a function I found to get the colors 

ggplotColours <- function(n=6, h=c(0, 360) +15) { 
    if ((diff(h)%%360) < 1) h[2] <- h[2] - 360/n 
    hcl(h = (seq(h[1], h[2], length = n)), c = 100, l = 65) 
} 

n <- length(levels(hunger_small$WHO.region)) 
q <- qplot(x = Year, y = Numeric, data = hunger_small, 
     color = WHO.region) + theme(legend.position = "bottom") 
q <- q + geom_abline(intercept = lmRegion$coefficients[1], 
     slope = lmRegion$coefficients[2], color = ggplotColours(n=n)[1]) 
for (i in 2:n) { 
    q <- q + geom_abline(intercept = lmRegion$coefficients[1] + 
      lmRegion$coefficients[1+i], slope = lmRegion$coefficients[2] + 
       lmRegion$coefficients[7+i], color = ggplotColours(n=n)[i]) 
} 
+0

有一种方法[这里](http://stackoverflow.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph),其可以适应于多面图。 – mnel 2013-02-19 00:56:30

+0

由于某种原因(还没有弄清楚)我无法重现你的例子(第2-n行没有出现在图上)。 *然而*:据我可以告诉天真的方法('geom_smooth(method =“lm”,se = FALSE)')*应该*给你同样的情节,你正在寻找,带有方面... – 2013-02-19 01:55:26

回答

0

如果你有一个分类数据:

geom_point() 

将无法​​正常工作,

geom_boxplot() 

会工作。

ggplot(hunger, aes(x = sex, y = hunger)) + geom_boxplot() + labs(x="sex") + geom_smooth(method = "lm",se=FALSE, col = "blue"). Susy