如何将徒手画的红色圆圈添加到ggplot2图形中?

编程入门 行业动态 更新时间:2024-10-26 23:29:15
本文介绍了如何将徒手画的红色圆圈添加到ggplot2图形中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 去年我发布了,但令我沮丧的是我无法找到将它们添加到ggplot2图表的方法。我知道如何包,它提供了 geom_freehand 图层,这个图层在ggplot2中被忽略了。

例如,如果您想在上面的图中圈出前两个最活跃的月份,可以按照以下代码进行操作:

  

就这样,情节现在值得在Meta上发布堆栈溢出

$ b

geom_freehand 图层会采用其他选项来自定义圆圈,包括 radius 和美食小号。你也可以让这个圆圈不是红的,就好像你曾经想做的事一样。

p < - ggplot (数据= top_2,半径= .5)p + geom_freehand(data = top_2,noisiness = 10)$(gem_freehand)(answers_per_month,aes(month,n))+ geom_line() p + b $ bp + geom_freehand(data = top_2,noisiness = 1)p + geom_freehand(data = top_2,color =blue)

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 my plots:

Freehand red circles are of course necessary in any plot on Meta Stack Overflow, but to my dismay I could not find a way to add them to a ggplot2 graph. I know how to add a circle, but such an artificial construct has no personality and would never pass muster on Meta.

As a reproducible example, consider this plot of my own answering activity over time, created using the stackr package:

# 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()

This plot is informative enough, but it has no soul. How can I add freehand red circles to it?

解决方案

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)

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")

更多推荐

如何将徒手画的红色圆圈添加到ggplot2图形中?

本文发布于:2023-08-02 03:47:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1274361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆圈   如何将   图形   红色

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!