ggplot2 geom_area overlay area plots in front of each other

前端 未结 2 552
情歌与酒
情歌与酒 2021-01-18 20:52

I am trying to make an area plot with the different areas are overlaid on one another rather than stacked.

I have a dataframe that looks like this:

         


        
相关标签:
2条回答
  • 2021-01-18 21:09

    Using tidyverse:

    library(forcats)
    p + geom_area(aes(colour = variable, 
    fill= fct_reorder(variable, value, .desc = TRUE)), position = 'identity') 
    

    Remove .desc = TRUE if it does the opposite of what you want.

    0 讨论(0)
  • 2021-01-18 21:10

    As Nathan wrote you have to use geom_area(position = "identity", ...)

    But before this you should reorder the levels of variable:

    df$variable <- factor(df$variable, unique(df[order(df$value, decreasing = T),"variable"]) )
    

    or

    df$variable <- reorder(df$variable, df$value, function(x) -max(x) )
    
    0 讨论(0)
提交回复
热议问题