R geom_path lines “closing”, sometimes. How to keep them “open”?

前端 未结 1 588
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 03:35

I\'m plotting a series of financial projections with ggplot\'s geom_path().

ggplot(df,aes(year,pot,color=iRate)) + geom_path() +  theme(legend.position=\"none\"         


        
相关标签:
1条回答
  • 2021-01-21 03:59

    Thanks @aosmith for mentioning the argument group in your comment. Here is a reproducible example describing the path closing issue with the airquality dataset. Let's say you want to plot a chart of temperature along the days of each month. Keeping all months on the same plot (in the spirit of these yearly plots: arcticseaicenews).

    Using geom_path() alone leads to annoying "closing" lines between the last day of the previous month and the first day of the next month.

    library(ggplot2)
    ggplot(airquality, aes(x = Day, y = Temp)) +
        geom_path()
    

    Using geom_path() with the argument group=Month prevents these lines from appearing:

    ggplot(airquality, aes(x = Day, y = Temp, group=Month)) +
        geom_path()
    

    Of course you could also display months on different facets with facet_wrap depending on what you desire:

    ggplot(airquality, aes(x = Day, y = Temp)) +
        geom_path() + 
        facet_wrap(~Month)
    
    0 讨论(0)
提交回复
热议问题