【问题标题】:geom_abline does not seem to respect groups in facet_grid [ggplot2]geom_abline 似乎不尊重 facet_grid [ggplot2] 中的组
【发布时间】:2013-11-15 04:01:19
【问题描述】:

只是想了解 geom_abline 如何处理 ggplot 中的构面。

我有一个学生考试成绩数据集。这些在具有 4 列的数据表 dt 中:

student: unique student ID
cohort:  grouping factor for students (A, B, … H)
subject: subject of the test (English, Math, Science)
score:   the test score for that student in that subject

目标是比较同类群组。下面的 sn-p 创建了一个示例数据集。

library(data.table)
## cohorts: list of cohorts with number of students in each
cohorts <- data.table(name=toupper(letters[1:8]),size=as.numeric(c(8,25,16,30,10,27,13,32)))
## base: assign students to cohorts
base    <- data.table(student=c(1:sum(cohorts$size)),cohort=rep(cohorts$name,cohorts$size))
## scores for each subject
english <- data.table(base,subject="English", score=rnorm(nrow(base), mean=45, sd=50))
math    <- data.table(base,subject="Math",    score=rnorm(nrow(base), mean=55, sd=25))
science <- data.table(base,subject="Science", score=rnorm(nrow(base), mean=70, sd=25))
## combine
dt      <- rbind(english,math,science)
## clip scores to (0,100)
dt$score<- (dt$score>=0) * dt$score
dt$score<- (dt$score<=100)*dt$score + (dt$score>100)*100

以下显示了 95% CL 的同类群组的平均分数,按主题分面,并包括一条(蓝色,虚线)参考线(使用 geom_abline)。

library(ggplot2)
library(Hmisc)
ggp <- ggplot(dt,aes(x=cohort, y=score)) + ylim(0,100)
ggp <- ggp + stat_summary(fun.data="mean_cl_normal")
ggp <- ggp + geom_abline(aes(slope=0,intercept=mean(score)),color="blue",linetype="dashed")
ggp <- ggp + facet_grid(subject~.)
ggp

问题是参考线(来自 geom_abline)在所有方面都是相同的(= 所有学生和所有科目的总平均分)。所以 stat_summary 似乎尊重 facet_grid 中隐含的分组(例如,按主题),但 abline 没有。 谁能解释一下原因?

注意:我意识到这个问题可以通过创建一个单独的组均值表并将其用作 geom_abline 中的数据源(如下)来解决,但是为什么需要这样做?

means <- dt[,list(mean.score=mean(score)),by="subject"]
ggp <- ggplot(dt,aes(x=cohort, y=score)) + ylim(0,100)
ggp <- ggp + stat_summary(fun.data="mean_cl_normal")
ggp <- ggp + geom_abline(data=means, aes(slope=0,intercept=mean.score),color="blue",linetype="dashed")
ggp <- ggp + facet_grid(subject~.)
ggp

【问题讨论】:

  • 我不知道答案,但我认为您的问题与在aes 中使用mean 有关。您将许多 y 值汇总为一个值,但我认为 geom_* 函数不能这样工作。尝试用geom_point(aes(y=mean(score)),color="blue") 替换您的geom_abline 调用,并将其与geom_point(aes(y=score),color="blue") 进行比较。这可能有助于您的调试过程。您可能还想查看geom_hline 文档中的最后一个示例。

标签: r ggplot2


【解决方案1】:

这应该做你想做的事。 stat_* 函数对每个方面使用不同的数据集合。我认为geom_* 函数的aes 中的任何表达式都旨在用于每个y 值的转换。

ggplot(dt,aes(x=cohort, y=score)) +
       stat_summary(fun.data="mean_cl_normal") + 
       stat_smooth(formula=y~1,aes(group=1),method="lm",se=FALSE) +
       facet_grid(subject~.) + ylim(0,100)

【讨论】:

  • 这对我所做的事情来说是一个巨大的进步。在 stat_smooth(...) 中设置 se=TRUE(默认值),不仅显示组(主题)平均值,还显示每个组的 95% CL。这不仅可以将群组相互比较,还可以与受试者的平均值进行比较,这是 geom_abline 无法完成的。使用公式=y~1 很巧妙……
【解决方案2】:

正如 golbasche 所说,我可能会做更多这样的事情:

dt <- dt[,avg_score := mean(score),by = subject]

ggplot(dt,aes(x=cohort, y=score)) + 
    facet_grid(subject~.) + 
    stat_summary(fun.data="mean_cl_normal") +
    geom_hline(aes(yintercept = avg_score),color = "blue",linetype = "dashed") + 
    ylim(0,100)

【讨论】:

    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多