How can I add freehand red circles to a ggplot2 graph?

前端 未结 1 987
栀梦
栀梦 2021-01-30 20:17

Last year I posted an analysis of user activity to Meta Stack Overflow, including a series of ggplot2 graphs. However, Wooble greatly shamed me by pointing out a fatal flaw with

1条回答
  •  无人及你
    2021-01-30 20:56

    You can use my ggfreehand package, which provides the geom_freehand layer that was so carelessly omitted from ggplot2.

    For example, if you wanted to circle the top two most active months in the plot above, you could follow the code with:

    top_2_months <- answers_per_month %>% top_n(2)
    
    library(ggfreehand)
    ggplot(answers_per_month, aes(month, n)) + geom_line() +
        geom_freehand(data = top_2_months)
    

    with freehand

    And just like that, the plot is now worthy of being posted on Meta Stack Overflow.

    The geom_freehand layer takes additional options to customize the circle, including radius and noisiness. You could also make the circle not red, as though that were something you would ever want to do.

    p <- ggplot(answers_per_month, aes(month, n)) + geom_line()
    
    p + geom_freehand(data = top_2, radius = .5)
    p + geom_freehand(data = top_2, noisiness = 10)
    p + geom_freehand(data = top_2, noisiness = 1)
    p + geom_freehand(data = top_2, color = "blue")
    

    enter image description here

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