ggplot 按多列分组

发布于 2025-01-10 19:34:44 字数 1544 浏览 0 评论 0原文

我的数据看起来像这样(我只包含前 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2025-01-17 19:34:44

这是使用来自 ggforcegeom_mark_ellipse 作为椭圆的一种可能性。要使用多个组(即 matrdate)来绘制椭圆,我们可以使用 interaction 将两列组合成一个新因子。

library(tidyverse)
library(ggforce)

df %>%
  ggplot(aes(x = date, y = age )) +
  geom_point(aes(shape = factor(sex), colour = factor(pod), size = 5)) +
  geom_mark_ellipse(aes(color = pod, group=interaction(date, matr), label = matr)) +
  scale_y_continuous(expand = c(0, 25)) +
  scale_x_discrete(expand = c(0, 1.35))

输出

在此处输入图像描述

数据

df <- 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, -20L), class = c("tbl_df", "tbl", 
"data.frame"))

Here is one possibility using geom_mark_ellipse from ggforce for the ellipses. To have multiple groups (i.e., matr and date) to draw the ellipses, we can use interaction to combine the two columns into a new factor.

library(tidyverse)
library(ggforce)

df %>%
  ggplot(aes(x = date, y = age )) +
  geom_point(aes(shape = factor(sex), colour = factor(pod), size = 5)) +
  geom_mark_ellipse(aes(color = pod, group=interaction(date, matr), label = matr)) +
  scale_y_continuous(expand = c(0, 25)) +
  scale_x_discrete(expand = c(0, 1.35))

Output

enter image description here

Data

df <- 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, -20L), class = c("tbl_df", "tbl", 
"data.frame"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文