重新格式化数据并创建热图

发布于 2025-02-06 21:58:06 字数 2828 浏览 2 评论 0 原文

去这里向后工作。

我想在R中创建一个看起来像这样的热图(对PowerPoint中的Janky手工制作绘画表示歉意):

- 列是年:1960年代,1970年代,1970年代,1980年代,1990年代,然后每个人,然后2000年/之后的年;

- 排是鲸鱼(鲸鱼在我的数据中的“ ID”下,请参见下文)

- 根据家庭谱系(下面标记为“矩阵”,在下面标记为“矩阵”)

- 盒子在典型的热图中被遮蔽,在其中更暗的地方。颜色=每年计数鲸鱼的数量更大(通常为1-5次)

- 理想情况下,矩阵以数字顺序下降-IE,L1,L2,L3等。在每个基质中,鲸鱼IDS在数字中下降订单-L1,L2,L3

等之后),每一行都是鲸鱼ID,每个盒子的值都是鲸鱼在那十年/年发生的次数。

我的数据当前看起来像这样(简洁地将数据缩短到10行):

    > dput(id)
    structure(list(date = structure(c(8243, 8243, 8243, 8248, 8947, 
    8947, 8947, 12271, 12271, 12271), class = "Date"), year = c(1992L, 
    1992L, 1992L, 1992L, 1994L, 1994L, 1994L, 2003L, 2003L, 2003L),
    event.id = c(8L, 8L, 8L, 10L, 11L, 11L, 11L, 14L, 14L, 15L),
    id = structure(c(51L,55L, 59L, 46L, 51L, 55L, 59L, 51L, 59L, 57L),
    .Label = c("J11", "J16", "J17", "J2", "J22", "J26", "J27", "J30", "J31", "J35"),
    class = "factor"), matriline = structure(c(20L, 20L, 20L, 11L, 20L, 20L, 20L, 20L, 
    20L, 15L), .Label = c("J2","J4", "J7", "J9", "K11", "K18", "K4", "K8",
    "L12", "L2"), class = "factor"), pod = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
    3L, 3L), .Label = c("J", "K", "L"), class = "factor")), row.names = c(NA, 
    -136L), class = c("tbl_df", "tbl", "data.frame"))

有人可以帮助我1)将我的数据转换为可行的格式2)创建热图吗?

非常感谢您!

编辑 新代码:

socialmap<- id %>%
mutate(x = case_when(year < 1960 ~ "Pre-1960", 
                   year %in% 1960:1969 ~ "1960-1969",
                   year %in% 1970:1979 ~ "1970-1979",
                   year %in% 1980:1989 ~ "1980-1989",
                   year %in% 1990:1999 ~ "1990-1999",
                   TRUE ~ year)) %>%
mutate(y = paste(matr, id)) %>%
group_by(x, y, .drop  = FALSE) %>%
summarize(count = n()) %>%
arrange(y) %>%
tidyr::separate(y, into = c("ym", "yid"), sep = " ", remove = FALSE)

socialmap$count=as.integer(socialmap$count) #don't want decimals in     
#count scale - but this didnt seem to fix it
socialmap$x <- factor(socialmap$x, levels = c("Pre-1960", "1960-    
1969", "1970-1979", "1980-1989", "1990-1999", 2000:2020)) #data go up 
to 2020
ggplot(data = socialmap, aes(x, yid, fill = count)) +
geom_tile() +
scale_fill_gradient(low = "blue", high = "red") +
scale_x_discrete(position = "top") +
scale_y_discrete(limits=rev) +
labs(x = NULL, y = NULL) +
facet_wrap( ~ ym, strip.position = "left", dir = "v") +
theme(panel.spacing = unit(0, "lines"),
    strip.background = element_blank(),
    strip.placement = "outside")

新图形:

Going to work backwards here.

I want to create a heat map in R that looks like this (apologies for my janky handmade drawing in PowerPoint):

enter image description here

-Where columns are years: 1960s, 1970s, 1980s, 1990s, and then each individual year at/after 2000;

-Rows are whales (whales are under "id" in my data, see below)

-Whales are grouped according to family lineage (labeled as "matriline," below)

-The boxes are shaded in a typical heat map-fashion, where darker colors=greater number of times the whale was counted each year (typically 1-5 times)

-And ideally, the matrilines descend in numeric order - ie, L1, L2, L3, etc. Within each matriline, the whale IDs descend in numeric order - L1, L2, L3 etc (a whale ID can be the same as a matriline sometimes)

I imagine my data would need to have columns for matriline, whale ID, 1960s, 1970s, 1980s, 1990s, 2000 (and then each year after), and each row would be a whale ID, and each box value would be the number of times the whale occurred in that decade/year.

My data currently look like this (shortened data to 10 rows for brevity):

    > dput(id)
    structure(list(date = structure(c(8243, 8243, 8243, 8248, 8947, 
    8947, 8947, 12271, 12271, 12271), class = "Date"), year = c(1992L, 
    1992L, 1992L, 1992L, 1994L, 1994L, 1994L, 2003L, 2003L, 2003L),
    event.id = c(8L, 8L, 8L, 10L, 11L, 11L, 11L, 14L, 14L, 15L),
    id = structure(c(51L,55L, 59L, 46L, 51L, 55L, 59L, 51L, 59L, 57L),
    .Label = c("J11", "J16", "J17", "J2", "J22", "J26", "J27", "J30", "J31", "J35"),
    class = "factor"), matriline = structure(c(20L, 20L, 20L, 11L, 20L, 20L, 20L, 20L, 
    20L, 15L), .Label = c("J2","J4", "J7", "J9", "K11", "K18", "K4", "K8",
    "L12", "L2"), class = "factor"), pod = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
    3L, 3L), .Label = c("J", "K", "L"), class = "factor")), row.names = c(NA, 
    -136L), class = c("tbl_df", "tbl", "data.frame"))

Could anybody please help me 1) transform my data into a workable format to 2) create the heat map?

Thank you so much in advance!

EDIT
New code:

socialmap<- id %>%
mutate(x = case_when(year < 1960 ~ "Pre-1960", 
                   year %in% 1960:1969 ~ "1960-1969",
                   year %in% 1970:1979 ~ "1970-1979",
                   year %in% 1980:1989 ~ "1980-1989",
                   year %in% 1990:1999 ~ "1990-1999",
                   TRUE ~ year)) %>%
mutate(y = paste(matr, id)) %>%
group_by(x, y, .drop  = FALSE) %>%
summarize(count = n()) %>%
arrange(y) %>%
tidyr::separate(y, into = c("ym", "yid"), sep = " ", remove = FALSE)

socialmap$count=as.integer(socialmap$count) #don't want decimals in     
#count scale - but this didnt seem to fix it
socialmap$x <- factor(socialmap$x, levels = c("Pre-1960", "1960-    
1969", "1970-1979", "1980-1989", "1990-1999", 2000:2020)) #data go up 
to 2020
ggplot(data = socialmap, aes(x, yid, fill = count)) +
geom_tile() +
scale_fill_gradient(low = "blue", high = "red") +
scale_x_discrete(position = "top") +
scale_y_discrete(limits=rev) +
labs(x = NULL, y = NULL) +
facet_wrap( ~ ym, strip.position = "left", dir = "v") +
theme(panel.spacing = unit(0, "lines"),
    strip.background = element_blank(),
    strip.placement = "outside")

New graph:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黑寡妇 2025-02-13 21:58:06

看起来您的示例数据是DPLYR格式的,因此我将向您展示如何在DPLYR中执行此操作。我创建了更多示例数据,因此结果更有趣 - 检查下面的数据代码块。一般过程是首先根据年份,矩阵和ID创建一个分组变量,然后按每个组总结计数。然后使用 ggplot2 :: geom_tile()映射。

要具有层次结构的Y轴,您可以首先将ID分开(即链中的最后一步):

library(dplyr)
library(tidyr)
library(ggplot2)

hm <- dat %>%
  mutate(x = case_when(year < 1960 ~ "Pre-1960", 
                       year %in% 1960:1969 ~ "1960-1969",
                       year %in% 1970:1979 ~ "1970-1979",
                       year %in% 1980:1989 ~ "1980-1989",
                       year %in% 1990:1999 ~ "1990-1999",
                       TRUE ~ year)) %>%
  mutate(y = paste(matriline, id)) %>%
  group_by(x, y, .drop  = FALSE) %>%
  summarize(count = n()) %>%
  arrange(y) %>%
  tidyr::separate(y, into = c("ym", "yid"), sep = " ", remove = FALSE)

hm
   x         y       ym    yid   count
   <fct>     <chr>   <chr> <chr> <int>
 1 1960-1969 J02 J02 J02   J02       3
 2 1970-1979 J02 J02 J02   J02       4
 3 1980-1989 J02 J02 J02   J02       7
 4 1990-1999 J02 J02 J02   J02       1
 5 2006      J02 J02 J02   J02       2
 6 2007      J02 J02 J02   J02       2
 7 2009      J02 J02 J02   J02       2
 8 2014      J02 J02 J02   J02       1
 9 1960-1969 J02 J11 J02   J11       3
10 1970-1979 J02 J11 J02   J11       5
# ... with 485 more rows

强迫您的情节通过将X轴胁迫以所有您想要的级别来构成全年:

hm$x <- factor(hm$x, levels = c("Pre-1960", "1960-1969", "1970-1979", "1980-1989", "1990-1999", 2000:2020))

然后 到Matrilines的组IDS:

ggplot(data = hm, aes(x, yid, fill = count)) +
  geom_tile() +
  scale_fill_gradient(low = "white", high = "red") +
  scale_x_discrete(position = "top", drop = FALSE) +
  scale_y_discrete(limits=rev) +
  labs(x = NULL, y = NULL) +
  facet_wrap( ~ ym, strip.position = "left", dir = "v", ncol = 1) +
  theme(panel.spacing = unit(0.2, "lines"),
        strip.background = element_blank(),
        strip.placement = "outside",
        axis.text.x = element_text(angle = 45, hjust = -0.02))

”在此处输入图像描述”

数据:

ids <- c("J11", "J16", "J17", "J02", "J22", "J26", "J27", "J30")
matrilines <- c("J02","J04", "K11", "L20", "P90", "K100", "R22")

dat <- data.frame(year = as.character(sample(1960:2018, 1000, replace = TRUE)),
                  id = sample(ids, 1000, replace = TRUE),
                  matriline = sample(matrilines, 1000, replace = TRUE))

It looks like your example data was in a dplyr format, so I'll show you how you could do this in dplyr. I created more example data so the result was more interesting - check the data code chunk below. The general process is to first create a grouping variable based on year, matriline, and id, and then summarize counts by each group. Then use ggplot2::geom_tile() to map.

To have a hierarchical y-axis, you could first separate your ids (ie the last step in the chain):

library(dplyr)
library(tidyr)
library(ggplot2)

hm <- dat %>%
  mutate(x = case_when(year < 1960 ~ "Pre-1960", 
                       year %in% 1960:1969 ~ "1960-1969",
                       year %in% 1970:1979 ~ "1970-1979",
                       year %in% 1980:1989 ~ "1980-1989",
                       year %in% 1990:1999 ~ "1990-1999",
                       TRUE ~ year)) %>%
  mutate(y = paste(matriline, id)) %>%
  group_by(x, y, .drop  = FALSE) %>%
  summarize(count = n()) %>%
  arrange(y) %>%
  tidyr::separate(y, into = c("ym", "yid"), sep = " ", remove = FALSE)

hm
   x         y       ym    yid   count
   <fct>     <chr>   <chr> <chr> <int>
 1 1960-1969 J02 J02 J02   J02       3
 2 1970-1979 J02 J02 J02   J02       4
 3 1980-1989 J02 J02 J02   J02       7
 4 1990-1999 J02 J02 J02   J02       1
 5 2006      J02 J02 J02   J02       2
 6 2007      J02 J02 J02   J02       2
 7 2009      J02 J02 J02   J02       2
 8 2014      J02 J02 J02   J02       1
 9 1960-1969 J02 J11 J02   J11       3
10 1970-1979 J02 J11 J02   J11       5
# ... with 485 more rows

Then force your plot to include all years by coercing your x-axis to factor with all levels you want:

hm$x <- factor(hm$x, levels = c("Pre-1960", "1960-1969", "1970-1979", "1980-1989", "1990-1999", 2000:2020))

And plot using faceting to group ids by matrilines:

ggplot(data = hm, aes(x, yid, fill = count)) +
  geom_tile() +
  scale_fill_gradient(low = "white", high = "red") +
  scale_x_discrete(position = "top", drop = FALSE) +
  scale_y_discrete(limits=rev) +
  labs(x = NULL, y = NULL) +
  facet_wrap( ~ ym, strip.position = "left", dir = "v", ncol = 1) +
  theme(panel.spacing = unit(0.2, "lines"),
        strip.background = element_blank(),
        strip.placement = "outside",
        axis.text.x = element_text(angle = 45, hjust = -0.02))

enter image description here

Data:

ids <- c("J11", "J16", "J17", "J02", "J22", "J26", "J27", "J30")
matrilines <- c("J02","J04", "K11", "L20", "P90", "K100", "R22")

dat <- data.frame(year = as.character(sample(1960:2018, 1000, replace = TRUE)),
                  id = sample(ids, 1000, replace = TRUE),
                  matriline = sample(matrilines, 1000, replace = TRUE))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文