[英]how to use geom_smooth in ggplot 2 for a subset of values in a data frame
[英]How to use geom_smooth() on a subset of variables?
我有兩組變量,將它們繪制在不同的圖上會產生以下結果:
因此,我現在有兩個圖:一個下半部為空,另一個上半部為空。 我想將這些結合起來以形成一個圖形,並為兩組變量提供兩個灰色帶。 我進行了很多搜索,但仍然無法確切地知道該怎么做。 我當前的代碼是:
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")
)
但這僅繪制其中一條曲線,如下所示:
如何獲得兩組變量的兩條線(以及置信區間)?
編輯:添加與此相關的另一個子問題:如果可能的話,我是否有辦法將圖例也“分為”兩組?
我看到您看到了關於coord_cartesian
。 但是,設置該limits
和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
從gridExtra包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.