R箱图和条形图并排在1图中

时间:2011-12-05 14:48:13

标签: r boxplot stripchart

是否可以在同一图中将箱线图和条形图相邻?如果我运行此代码,条带图将覆盖箱图。我真正想要的是他们彼此相邻。以帽子方式,将形成x-上有10列的图形。这可能吗?

boxplot(doubles[1:5,])
stripchart(doubles[6:10,],add=TRUE,vertical=TRUE, pch=19)

enter image description here

2 个答案:

答案 0 :(得分:5)

你的数据的一些例子会很好,但最简单的选择可能是:

 #random data corresponding to your 5 columns    
 x <- data.frame(V = rnorm(100), W = rnorm(100), X = rnorm(100), Y = rnorm(100), 
     Z = rnorm(100))
 #remove axis with 'axes=F', define wider x-limits with 'xlim' 
 stripchart(x[1:5,],vertical=TRUE, pch=19,xlim=c(1,6),axes=F)
 #add boxplots next to stripchart, decrease width with 'boxwex'
 boxplot(x[1:5,],add=T,at=1.5:5.5,boxwex=0.25,axes=F)
 #add custom x axis
 axis(1,at=1.25:5.25,labels=names(x))

enter image description here

答案 1 :(得分:4)

使用ggplot2

library(ggplot2)
qplot(treatment, decrease, data = OrchardSprays) + 
  scale_y_log10() + 
  geom_boxplot() + 
  geom_point(colour = 'blue', alpha = 0.5)

enter image description here