2016-10-03 38 views
0

我有一个有不同范围和长度为n的向量的多列(假设为n)的数据框。我想为每个变量在每个箱形图下方显示不同的x轴。我试过facet_gridfacet_wrap,但它给出了公共x轴。 这是我曾尝试:在R中的盒子图和额外的点

d <- data.frame(matrix(rnorm(10000), ncol = 20)) 

point_var <- rnorm(20) 
plot.data <- gather(d, variable, value) 
plot.data$test_data <- rep(point_var, each = nrow(d)) 

ggplot(plot.data, aes(x=variable, y=value)) + 
geom_boxplot() + 
geom_point(aes(x=factor(variable), y = test_data), color = "red") + 
coord_flip() + 
xlab("Variables") + 
theme(legend.position="none") 
+0

有什么问题吗? –

+0

我想为每个地块绘制不同的x轴 – Rajan

+0

也许facet_wrap(〜yourvar,scales ='free_x')? – lbusett

回答

1

如果你可以用具有x轴的情节上面的文字,并具有位图的顺序生活混乱的,这可能工作:

library(grid) 
p = ggplot(plot.data, aes(x = 0, y=value)) + 
    geom_boxplot() + 
    geom_point(aes(x = 0, y = test_data), color = "red") + 
    facet_wrap(~variable, scales = "free_y", switch = "y") + 
    xlab("Variables") + 
    theme(legend.position="none") + theme_bw() + theme(axis.text.x=element_blank()) 

    print(p, vp=viewport(angle=270, width = unit(.75, "npc"), height = unit(.75, "npc"))) 

我实际上只是在不翻转坐标的情况下创建图形,以便scales = 'free_y'工作,切换条形标签的位置,然后旋转图形。

如果你不喜欢上面的图表(这是可以理解的),我会考虑创建一个单一的地块列表,然后把它们放在一起grid.arrange

HTH,

洛伦佐