1

我有两组变量,将它们绘制在不同的图表上会产生以下结果: 在此处输入图像描述

所以,我现在有两张图:一张下半部为空,另一张上半部为空。我想将这些组合成一个图表,两组变量有两个灰色带。我已经搜索了很多,但我仍然无法弄清楚如何做到这一点。我目前的代码是:

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")
                )

但这仅绘制了其中一条曲线,如下所示: 在此处输入图像描述

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

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

4

1 回答 1

2

我看到你看到了我关于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")
                )

大概你这样做是因为你已经决定刻面不是你想要的......?至于拆分图例,只有在单独的数据子集上制作两个单独的图,然后使用gridExtra包将它们放在一起grid.arrange时,这才是可能的。

于 2013-10-03T22:41:53.877 回答