如何为ggplot的AES使用数据帧的名称和行名? [英] How to use names and rownames of a dataframe for the aes of ggplot?

查看:105
本文介绍了如何为ggplot的AES使用数据帧的名称和行名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框enrichment_df

I have a dataframe enrichment_df that looks like this

                                         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

,我想用 https://stackoverflow.com/a/23228273/651779 中的示例制作一个重叠的条形图.一个>.我希望填充为行名,x轴为列名.我尝试用

and I want to make an overlapping barplot with the example from https://stackoverflow.com/a/23228273/651779. I want the fill to be the rownames, and the x-axis the colnames. I try to plot it with

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

如何在ggplot的AES(或aes_string)中使用行名和列名?

How can I use the rownames and colnames in the aes (or aes_string) of ggplot?

推荐答案

每当使用ggplot时,您都应该使用长格式的数据:

Whenever using ggplot you should have your data in long format:

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 )

我不确定是否要查找具有负值的position = "identity".

I'm not sure though if position = "identity" with negative values is what you are looking for.

这篇关于如何为ggplot的AES使用数据帧的名称和行名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆