制作此数据的分组条形图

时间:2016-10-22 13:05:04

标签: r plot graph

我在这个论坛上搜索,但我找到的每个分组条形图都基于列上的分组。但是我已经使用Java来组合我的数据,但我不知道如何在分组条形图中绘制这些数据: 我如何创建矩阵(我将省略所有代码以保持简单):

MAT2 = matrix(data, ncol=2)
MAT2 = cbind(MAT2,kolommen)
colnames(MAT2) <- c("mpk6","mpk3","group")  

给了我:

       mpk6 mpk3 group                 
     [1,] "9"  "0"  "membrane"            
     [2,] "91" "64" "plastid"             
     [3,] "12" "0"  "extracellular region"
     [4,] "74" "21" "thylakoid"           
     [5,] "6"  "0"  "vacuole"             
     [6,] "21" "33" "cytosol"             
     [7,] "4"  "0"  "plasma membrane"     
     [8,] "9"  "12" "peroxisome"          
     [9,] "0"  "0"  "mitochondrion"       
    [10,] "22" "13" "nucleolus"           
    [11,] "0"  "0"  "ribosome"            
    [12,] "27" "31" "cell wall"  

但是当我使用条形图绘制时,我得到以下结果: enter image description here
这些条形基于mpk6和mpk3进行分组,但是我想分组为&#34;膜&#34;,&#34; plastid&#34;,&#34;细胞外区域&#34;等:
enter image description here
我不知道是否有可能,但如果它只是删除高度0的条形图会很好,所以图形中没有那么多的空白(但这可能会毁掉整个分组的想法)条形图,所以如果有最佳选择的建议,欢迎他们!)

1 个答案:

答案 0 :(得分:1)

假设您的数据位于名为A的data.frame中,或者可以强制转换为data.frame,这是我使用tidyrdplyr和{{1库:

ggplot2

空白不再可见,但每个条的宽度都是固定的,无论有多少不同的mpk-n测量与之相关

bar-plot of data supplied with variable width bars

另一种选择是如果你希望条的宽度相同(但图中有更多的空格)

A <- gather(A, mpk, n, mpk6:mpk3)

ggplot(A %>% filter(n > 0) %>% mutate(group=droplevels(group)),
       aes(x=group, y=n, group=mpk, fill=mpk)) + 
geom_bar(position='dode', stat='identity') +
coord_flip()

alternative bar-plot of data with bar-width constant but whitespace appears when groups have different numbers of members as the distance between groups is constant