barplot中的ggplot额外栏

时间:2012-05-01 12:27:43

标签: r ggplot2

我的数据是

> data
    width          variable value
1     200   mappedEnsemblTx   110
2     400   mappedEnsemblTx   392
3     600   mappedEnsemblTx  1055
4     800   mappedEnsemblTx  2615
5    1000   mappedEnsemblTx  5201
101   200 UnmappedEnsemblTx  3927
102   400 UnmappedEnsemblTx  5175
103   600 UnmappedEnsemblTx  3220
104   800 UnmappedEnsemblTx  4856
105  1000 UnmappedEnsemblTx  7130  

当我绘制这个时,我可以在我的情节中看到三个或者可能是四个条,而不是两个条。 最小代码如下:

 library(ggplot2)


g = ggplot(dat.melt,aes(x=factor(width),y=value,fill=variable), format(scientific=FALSE)) 
g = g + geom_bar(stat='identity',position='dodge') 
g <- g + theme_bw()
g = g + opts(axis.text.x=theme_text(angle=-90, hjust=0, size = 16), axis.text.y=theme_text(size=16))
g <- g + opts(axis.title.x = theme_text(size=18), axis.title.y = theme_text(size=18, angle=90) )
g = g + scale_x_discrete('Total widths of the Ensembl Transcripts') + scale_y_continuous(name = 'Count') 
g = g + coord_cartesian(ylim = c(0,7000))

由于

1 个答案:

答案 0 :(得分:4)

使用

data.melt <- structure(list(width = c(200L, 400L, 600L, 800L, 1000L, 200L, 
400L, 600L, 800L, 1000L), variable = structure(c(1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("mappedEnsemblTx", "UnmappedEnsemblTx"
), class = "factor"), value = c(110L, 392L, 1055L, 2615L, 5201L, 
3927L, 5175L, 3220L, 4856L, 7130L)), .Names = c("width", "variable", 
"value"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "101", "102", "103", "104", "105"))

和你的例子

g <- ggplot(data.melt,aes(x=factor(width),y=value,fill=variable),
            format(scientific=FALSE)) 
g <- g + geom_bar(stat='identity',position='dodge') 
g <- g + theme_bw()
g <- g + opts(axis.text.x=theme_text(angle=-90, hjust=0, size = 16), 
              axis.text.y=theme_text(size=16))
g <- g + opts(axis.title.x = theme_text(size=18),
              axis.title.y = theme_text(size=18, angle=90) )
g <- g + scale_x_discrete('Total widths of the Ensembl Transcripts') +
         scale_y_continuous(name = 'Count') 
g <- g + coord_cartesian(ylim = c(0,7000))
g

我明白了:

barplot example

这是我所期待的; 5组2条。 5个群组,因为您在因子width中有5个variable值和2个等级。

那是:

R> sessionInfo()
R version 2.15.0 Patched (2012-04-16 r59049)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
 [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
 [5] LC_MONETARY=en_GB.utf8    LC_MESSAGES=en_GB.utf8   
 [7] LC_PAPER=C                LC_NAME=C                
 [9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.0

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.0       
 [5] MASS_7.3-17        memoise_0.1        munsell_0.3        plyr_1.7.1        
 [9] proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1     scales_0.2.0      
[13] stringr_0.6        tools_2.15.0