2017-04-01 108 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