代码之家  ›  专栏  ›  技术社区  ›  Chthonic Project

如何在变量子集上使用geom_smooth()?

  •  1
  • Chthonic Project  · 技术社区  · 13 年前

    我有两组变量,将它们绘制在不同的图上会得到以下结果: enter image description here

    所以,我现在有两张图:一张是空的下半部分,另一张是空白的上半部分。我想把这些结合起来,得到一个单独的图,两组变量有两个灰色带。我搜索了很多,但仍然不知道该怎么做。我当前的代码是:

    categories <- c("A", "B", "C", "D", "E")
    
    # To stop ggplot from imposing alphabetical ordering on x-axis
    categories <- factor(categories, levels=categories, ordered=T)
    
    intensive   <- c( 0.660,  0.438,  0.515,  0.038,  0.443)
    comparative <- c( 0.361,  0.928,  0.270,  0.285,  0.311)
    wh_adverbs  <- c( 0.431,  0.454,  0.056,  0.330,  0.577)
    past_tense    <- c(0.334, 0.229, 0.668, 0.566, 0.838)
    present_tense <- c(0.659, 0.322, 0.484, 0.039, 1.000) 
    conjunctions <- c( 0.928,  0.207,  0.162, -0.299, -0.045)
    personal      <- c(0.498, 0.521, 0.332, 0.04, 0.04)
    interrogative <- c(0.266, 0.202, 0.236, 0.06, 0.06)
    sbj_objective <- c(0.913, 0.755, 0.863, 0.803, 0.913)
    possessive    <- c(0.896, 0.802, 0.960, 0.611, 0.994)
    thrd_person <- c(-0.244, -0.265, -0.410, -0.008, -0.384)
    nouns       <- c(-0.602, -0.519, -0.388, -0.244, -0.196)
    
    df1 <- data.frame(categories,
                     "Intensive Adverbs"=intensive,
                     "Comparative Adverbs"=comparative,
                     "Wh-adverbs (WRB)"=wh_adverbs,
                     "Verb: Past Tense"=past_tense,
                     "Verb: Present Tense"=present_tense,
                     "Conjunctions"=conjunctions,
                     "Personal Pronouns"=personal,
                     "Interrogative Pronouns"=interrogative,
                     "Subjective/Objective Pronouns"=sbj_objective,
                     "Possessive Pronouns"=possessive,
                     "3rd-person verbs"=thrd_person,
                     "Nouns"=nouns,
                     check.names=F
                     )
    
    df1.m <- melt(df1)
    g1 <- ggplot(df1.m, aes(group=1, categories, value, shape=variable, colour=variable))
    g1 <- g1 + geom_hline(yintercept=0, size=4, color="white")
    g1 <- g1 + geom_point(aes(shape=variable), size=2, alpha=I(0.8))
    g1 <- g1 + scale_shape_manual(values = 1:12)
    g1 <- g1 + geom_smooth()
    g1 <- g1 + scale_x_discrete("\n(a) Involved features", expand=c(0.05, 0.05))
    g1 <- g1 + coord_cartesian(ylim=(c(-1,1)))
    g1 <- g1 + scale_y_continuous(limits=c(-1,1), name="Log Odds Ratio", oob=rescale_none)
    g1 <- g1 + guides(colour=guide_legend(title=NULL), shape=guide_legend(title=NULL))
    g1 <- g1 + theme(legend.position="right",
                     legend.justification=c(0,0),
                     legend.text=element_text(size=10),
                     panel.grid.minor = element_blank(),
                     axis.text=element_text(size=10,color="black"),
                     axis.title=element_text(size=12,face="bold")
                    )
    

    但这只绘制了其中一条曲线,如图所示: enter image description here

    如何获得两组变量的两条线(以及置信区间)?

    编辑:添加另一个与此相关的子问题:如果可能的话,我有没有办法将图例“分”为两组?

    1 回复  |  直到 13 年前
        1
  •  2
  •   joran    13 年前

    我看到你看到我关于 coord_cartesian 。但是设置这两个 limits 正在交叉工作。不管怎样,也许你想要这个:

    df1.m$grp <- ifelse(df1.m$variable %in% c('3rd-person verbs','Nouns'),'grp1','grp2')
    g1 <- ggplot(df1.m, aes(group=grp, categories, value, shape=variable, colour=variable)) + 
            geom_hline(yintercept=0, size=4, color="white") + 
            geom_point(aes(shape=variable), size=2, alpha=I(0.8)) + 
            scale_shape_manual(values = 1:12) + 
            geom_smooth() + 
            scale_x_discrete("\n(a) Involved features", expand=c(0.05, 0.05)) + 
            coord_cartesian(ylim=(c(-1,1))) + 
            scale_y_continuous(name="Log Odds Ratio", oob=rescale_none) + 
            guides(colour=guide_legend(title=NULL), shape=guide_legend(title=NULL)) + 
            theme(legend.position="right",
                     legend.justification=c(0,0),
                     legend.text=element_text(size=10),
                     panel.grid.minor = element_blank(),
                     axis.text=element_text(size=10,color="black"),
                     axis.title=element_text(size=12,face="bold")
                    )
    

    大概你这样做是因为你已经决定不想面对面了。。。?至于拆分图例,只有在数据的不同子集上绘制两个单独的图,然后使用 grid.arrange 来自 额外网格 包裹

    推荐文章