How to use names and rownames of a dataframe for the aes of ggplot? How to use names and rownames of a dataframe for the aes of ggplot? r r

How to use names and rownames of a dataframe for the aes of 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 Enrichment1       day1_day3__sham3_strict_enrichment meclocycline     -0.8692 hour4_day1_day3__sham3_strict_enrichment meclocycline     -0.2943      day7_day14__sham3_strict_enrichment meclocycline     -0.333

This can be plotted by

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

enter image description here

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