ggplot 按多列分组
我的数据看起来像这样(我只包含前 20 行来显示数据的分布,但大约有 135 行):
> dput(id)
structure(list(date = c("7/27/1992", "7/27/1992", "7/27/1992",
"8/1/1992", "7/1/1994", "7/1/1994", "7/1/1994", "8/7/2003", "8/7/2003",
"8/7/2003", "8/7/2003", "7/21/2004", "7/21/2004", "7/26/2004",
"7/26/2004", "7/5/2005", "7/5/2005", "7/9/2005", "7/9/2005",
"7/9/2005"), event.id = c(8L, 8L, 8L, 10L, 11L, 11L, 11L, 14L,
14L, 15L, 15L, 17L, 17L, 18L, 18L, 20L, 20L, 21L, 21L, 21L), id = c("L5", "L58",
"L73", "L21", "L5", "L58", "L73", "L5", "L73", "L7", "L57", "L21",
"L47", "L54", "L100", "J27", "J31", "J16", "J26", "J36"), sex = structure(c(1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 1L), .Label = c("0", "1"), class = "factor"),
age = c(28L, 12L, 6L, 42L, 30L, 14L, 8L, 39L, 17L, 42L, 26L,
54L, 30L, 27L, 3L, 14L, 10L, 33L, 14L, 6L), matr = c("L9",
"L9", "L9", "L21", "L9", "L9", "L9", "L9", "L9", "L37", "L45",
"L21", "L21", "L35", "L35", "J4", "J4", "J7", "J7", "J7"),
matralive = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), pod = c("L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "J", "J",
"J", "J", "J")), row.names = c(NA, -134L), class = c("tbl_df", "tbl", "data.frame"))
我的目标是使用 ggplot 创建一个看起来像这样的图表:
我不知道如何生成它 - 我想我必须按 pod 和 matriline 进行分组,但我不确定如何去做。
非常感谢任何帮助 - 非常感谢!
I have data that look like this (I'm only including the first twenty rows to show the spread of data but there are about 135 rows):
> dput(id)
structure(list(date = c("7/27/1992", "7/27/1992", "7/27/1992",
"8/1/1992", "7/1/1994", "7/1/1994", "7/1/1994", "8/7/2003", "8/7/2003",
"8/7/2003", "8/7/2003", "7/21/2004", "7/21/2004", "7/26/2004",
"7/26/2004", "7/5/2005", "7/5/2005", "7/9/2005", "7/9/2005",
"7/9/2005"), event.id = c(8L, 8L, 8L, 10L, 11L, 11L, 11L, 14L,
14L, 15L, 15L, 17L, 17L, 18L, 18L, 20L, 20L, 21L, 21L, 21L), id = c("L5", "L58",
"L73", "L21", "L5", "L58", "L73", "L5", "L73", "L7", "L57", "L21",
"L47", "L54", "L100", "J27", "J31", "J16", "J26", "J36"), sex = structure(c(1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 1L), .Label = c("0", "1"), class = "factor"),
age = c(28L, 12L, 6L, 42L, 30L, 14L, 8L, 39L, 17L, 42L, 26L,
54L, 30L, 27L, 3L, 14L, 10L, 33L, 14L, 6L), matr = c("L9",
"L9", "L9", "L21", "L9", "L9", "L9", "L9", "L9", "L37", "L45",
"L21", "L21", "L35", "L35", "J4", "J4", "J7", "J7", "J7"),
matralive = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), pod = c("L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "J", "J",
"J", "J", "J")), row.names = c(NA, -134L), class = c("tbl_df", "tbl", "data.frame"))
My goal is to create a graph that looks something like this, using ggplot:
I'm stuck as to how to generate this - I imagine I will have to group by pod and matriline, but am unsure how to go about this.
Any help is super appreciated - thank you so much!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用来自
ggforce
的geom_mark_ellipse
作为椭圆的一种可能性。要使用多个组(即matr
和date
)来绘制椭圆,我们可以使用interaction
将两列组合成一个新因子。输出
数据
Here is one possibility using
geom_mark_ellipse
fromggforce
for the ellipses. To have multiple groups (i.e.,matr
anddate
) to draw the ellipses, we can useinteraction
to combine the two columns into a new factor.Output
Data