2014-04-23 20 views
9

我有一個數據幀enrichment_df看起來像這樣如何使用ggplot的aes的數據框的名稱和rownames?

          meclocycline pimozide isocorydine alvespimycin 
day1_day3__sham3_strict_enrichment    -0.869 0.855  -0.859  0.539 
hour4_day1_day3__sham3_strict_enrichment  -0.294 0.268  -0.539  -0.120 
day7_day14__sham3_strict_enrichment   -0.333 0.404  0.297  0.233 
day90__sham3_strict_enrichment     -0.511 -0.657  -0.519  0.184 
day14__sham3_strict_enrichment     -0.239 -0.420  0.513  -0.422 
day7__sham3_strict_enrichment     -0.394 -0.380  -0.408  0.337 

,我想打一個重疊barplot從https://stackoverflow.com/a/23228273/651779的例子。我想填充是rownames,x軸是colnames。我嘗試用

ggplot(enrichment_df, aes_string(names(enrichment_df), fill = rownames(enrichment_df))) + 
geom_bar(position = 'identity', alpha = .3) 

然而繪製它,這給錯誤object 'day1_day3__sham3_strict_enrichment' not found

我如何使用rownames和colnames在ggplot的AES(或aes_string)?

回答

14

每當使用ggplot你應該在長格式的數據:

enrichment_df[ "day" ] <- rownames(enrichment_df) 
df.molten <- melt(enrichment_df, id.vars="day", value.name="Enrichment", variable.name="Antibiotics") 

head(df.molten) 
             day Antibiotics Enrichment 
1  day1_day3__sham3_strict_enrichment meclocycline  -0.869 
2 hour4_day1_day3__sham3_strict_enrichment meclocycline  -0.294 
3  day7_day14__sham3_strict_enrichment meclocycline  -0.333 

這可以通過

ggplot(df.molten, aes(x = Antibiotics, y = Enrichment, fill = day)) + 
    geom_bar(position = "identity", stat = "identity", alpha = .3) 

enter image description here

繪製我不知道但如果position = "identity"負值是你在找什麼。

相關問題