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

前端 未结 1 489
Happy的楠姐
Happy的楠姐 2021-02-14 07:49

I have a dataframe enrichment_df that looks like this

                                         meclocycline pimozide isocorydine alvespimycin
day1_d         


        
相关标签:
1条回答
  • 2021-02-14 08:21

    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
    

    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.

    0 讨论(0)
提交回复
热议问题