首页 文章

如何在ggplot2图表中添加手绘红色圆圈?

提问于
浏览
48

去年我发布了an analysis of user activity to Meta Stack Overflow,包括一系列ggplot2图表 . 然而,Wooble通过指出我的阴谋致命的缺陷使我大为羞愧:

enter image description here

手绘红色圆圈are of course necessary in any plot on Meta Stack Overflow,但令我沮丧的是,我找不到将它们添加到ggplot2图表的方法 . 我知道怎么回事add a circle,但是这样一个人造的构造没有个性,也永远不会在Meta上获得通过 .

作为一个可重现的示例,请考虑使用stackr包创建的我自己的回答活动随时间变化的情节:

# devtools::install_github("dgrtwo/stackr")
library(ggplot2)
library(dplyr)
library(lubridate)
library(stackr)

answers <- stack_users(712603, "answers", num_pages = 10, pagesize = 100)
answers_per_month <- answers %>%
    mutate(month = round_date(creation_date, "month")) %>%
    count(month)

ggplot(answers_per_month, aes(month, n)) + geom_line()

without freehand

这个情节足够丰富,但它没有灵魂 . 如何添加手绘红色圆圈呢?

1 回答

  • 53

    你可以使用我的ggfreehand包,它提供了从ggplot2中不小心省略的 geom_freehand 层 .

    例如,如果您想在上图中圈出前两个最活跃的月份,则可以使用以下代码:

    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

    就像那样,情节现在值得在Meta Stack Overflow上发布 .

    geom_freehand 图层采用其他选项来自定义圆圈,包括 radiusnoisiness . 你也可以让圆圈不是红色,好像这是你想做的事情 .

    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

相关问题