在条形图中将类似的图组合在一起

时间:2014-10-25 07:05:54

标签: r data-visualization

我想要在条形图上更紧密地组合数据组

stripchart(refined$lowfeq ~ refined$taxa, 
           vertical=T, las=2, pch=c(20,20,1,1,0,0,0,11,11,6,17), col=boxcol, 
           xaxt="n", ylab="Frequency (kHz)")
axis(1, las=2, cex.axis = 0.9, font.axis = 3, at=c(1:11), labels=1:11)`

enter image description here

我想将以下群组放在一起: 1和2,3和4,5-7,8和9. 10和11分开。

我试图用这个来解决这个问题但是没有成功。这些空间已经存在,但最后3个地块缺失了。

stripchart(refined$lowfeq ~ refined$taxa, 
           vertical=T, las=2, pch=c(20,20,1,1,0,0,0,11,11,6,17), col=boxcol, 
           xaxt="n",  ylab="Frequency (kHz)",at=c(1,2,4,5,7,8,9,11,12,14,15))
axis(1, las=2, cex.axis = 0.9, font.axis = 3, at=c(1,2,4,5,7,8,9,11,12,14,15), labels=1:11)`

enter image description here

如何在不丢失最后3个地块的情况下获得空间?

我的目标是看起来像这里的盒子图,但即使我遵循它也无法得到它:

http://www.r-bloggers.com/box-plot-with-r-tutorial/

2 个答案:

答案 0 :(得分:3)

修改xlim应该得到你想要的东西:

library(RColorBrewer)

refined <- data.frame(lowfeq=runif(300, 0.3, 0.7),
                      taxa=sample(1:11, 300, replace=TRUE))

boxcol <- brewer.pal(11, "Set3")

stripchart(refined$lowfeq ~ refined$taxa, xlim=c(1,15),
           vertical=T, las=2, pch=c(20,20,1,1,0,0,0,11,11,6,17), col=boxcol, 
           xaxt="n",  ylab="Frequency (kHz)", at=c(1,2,4,5,7,8,9,11,12,14,15))
axis(1, las=2, cex.axis = 0.9, font.axis = 3, at=c(1,2,4,5,7,8,9,11,12,14,15), labels=1:11)

enter image description here

答案 1 :(得分:0)

可以使用像ggplot代码这样的表来在轴中添加空格:

> refined
   taxa lowfreq group
1     1       2     a
2     1       3     a
3     1       6     a
4     1       5     a
5     1       8     a
6     1       9     a
7     2       2     a
8     2       3     a
9     2       5     a
10    2       6     a
11    2       4     a
12    2       5     a
13    2       1     a
14    3       2     b
15    3       6     b
16    3       5     b
17    3       8     b
18    3       4     b
19    3       5     b
20    _       0  <NA>

ggplot(refined)+geom_point(aes(x=taxa, y=lowfreq))+scale_x_discrete(breaks=c('1','2','_','3'))

enter image description here

或者可以简单地为不同的组提供不同的颜色(我认为这是一种更好的方法):

ggplot(refined[1:19,])+geom_point(aes(x=taxa, y=lowfreq, color=group), size=5)

enter image description here

相关问题