去这里向后工作。
我想在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):

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

发布评论
评论(1)
看起来您的示例数据是DPLYR格式的,因此我将向您展示如何在DPLYR中执行此操作。我创建了更多示例数据,因此结果更有趣 - 检查下面的数据代码块。一般过程是首先根据年份,矩阵和ID创建一个分组变量,然后按每个组总结计数。然后使用
ggplot2 :: geom_tile()
映射。要具有层次结构的Y轴,您可以首先将ID分开(即链中的最后一步):
强迫您的情节通过将X轴胁迫以所有您想要的级别来构成全年:
然后 到Matrilines的组IDS:
数据:
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):
Then force your plot to include all years by coercing your x-axis to factor with all levels you want:
And plot using faceting to group ids by matrilines:
Data: