2017-04-01 38 views
0

我試圖重現下面的圖,它顯示了y軸單獨部分的平均值和標準偏差。而不是將它們繪製在單獨的子圖中,它們共享x軸。在R或Python中垂直堆疊多個圖

this plot

我看到this suggestion使用lattice庫R,但我想用標籤到一邊去做,而不是每個圖形,如分離在我提供的例子。

+2

如果使用Python + Matplotlib,使用次要情節就像在第四幅圖的位置:http://matplotlib.org/examples/pylab_examples/subplots_demo .html – klimaat

回答

1

R,這裏有一個ggplot2版本:

library(ggplot2) 
library(reshape2) 

# Fake data 
set.seed(2) 
n = 20 
dat = data.frame(mp=rep(seq(0,1,length=n),2), 
       group=rep(LETTERS[1:2], each=n), 
       Mean=rep(c(1,1.1),each=n)*seq(0.7,0,length=n), 
       SD=rep(c(0.1,.12), each=n) + rnorm(2*n,0,0.02)) 

dat = melt(dat, id.var=c("mp","group")) 
dat$variable = factor(dat$variable, levels=c("SD","Mean")) 

ggplot(dat, aes(mp, value, colour=group)) + 
    facet_grid(variable ~ ., scales="free", space="free", switch="y",) + 
    geom_vline(xintercept=0.6, colour="grey40", linetype="11") + 
    geom_line() + 
    geom_point() + 
    scale_y_continuous(breaks=function(x) {round(seq(0,max(x),length=5)[-5],1)}) + 
    expand_limits(y=c(0,0.2)) + 
    labs(x="Mixing Parameter", y="") + 
    theme_bw() + 
    theme(panel.spacing.y=unit(0,"lines"), 
     strip.placement="outside", 
     strip.background=element_rect(fill=NA, colour=NA)) + 
    guides(colour=FALSE) 

enter image description here

+0

對我來說重要的是我儘可能地重新創建劇情*。你的答案在圖表和旁邊的子標籤之間有一個空格。 kliimat的答案適用於Python,如果您可以爲R做出這項工作,我很樂意將其標記爲解決方案。 –

+0

「子標籤」是否是指右側的圖例?此外,您是否希望右側的面標籤(「SD」和「平均值」或您希望它們位於軸值的外側左側, – eipi10

+0

查看更新的代碼。 – eipi10