我有两组变量,并将它们绘制在不同的图表上会产生以下结果:
所以,我现在有两张图:一张是空的下半部分,另一张是空的上半部分。我想将它们组合成一个单独的图形,两组变量有两个灰色条带。我搜索了很多,但我仍然无法弄清楚如何做到这一点。我目前的代码是:
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")
)
但是这只绘制了一条曲线,如图所示:
如何获得两组变量的两条线(以及置信区间)?
编辑:添加与此相关的另一个子问题:如果可以,我是否有办法将图例“划分”为两组?
答案 0 :(得分: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
将它们放在一起时才能实现。