gpt4 book ai didi

R:如何将数据聚合成百分比而不丢失 ggplot2 中堆积条形图的数据?

转载 作者:行者123 更新时间:2023-12-02 03:28:14 24 4
gpt4 key购买 nike

我想按位置和底物(参见下面的示例数据)将我的“核型”分子数据总结为百分比,以便在 ggplot2 中创建堆栈条形图。

我已经弄清楚如何使用“dcast”获得每种核型的总数,但无法弄清楚如何获得三种核型(即“BB”、“BD”、“DD”)中每一种的百分比.

数据的格式应能在“ggplot2”中制作堆积条形图。

示例数据:

library(reshape2)
Karotype.Data <- structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle", "Steninge"
), class = "factor"), Substrate = structure(c(1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
2L, 2L, 2L, 2L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle",
"Steninge"), class = "factor"), Karyotype = structure(c(1L, 3L,
4L, 4L, 3L, 3L, 4L, 4L, 4L, 3L, 1L, 4L, 3L, 4L, 4L, 3L, 1L, 4L,
3L, 3L, 4L, 3L, 4L, 3L, 3L), .Label = c("", "BB", "BD", "DD"), class = "factor")), .Names = c("Location",
"Substrate", "Karyotype"), row.names = c(135L, 136L, 137L, 138L,
139L, 165L, 166L, 167L, 168L, 169L, 236L, 237L, 238L, 239L, 240L,
326L, 327L, 328L, 329L, 330L, 426L, 427L, 428L, 429L, 430L), class = "data.frame")

## Summary count for each karoytype ##
Karyotype.Summary <- dcast(Karotype.Data , Location + Substrate ~ Karyotype, value.var="Karyotype", length)

最佳答案

您可以使用dplyr 包:

library(dplyr)
z.counts <- Karotype.Data %>%
group_by(Location,Substrate,Karyotype) %>%
summarize(freq=n())

z.freq <- z.counts %>%
group_by(Location,Substrate) %>%
mutate(freq=freq/sum(freq)*100)

这里,数据保留为长格式,因此可以直接使用 ggplot 构建条形图:

library(ggplot2)
ggplot(z.freq) +
aes(x=Karyotype,y=freq) +
facet_grid(Location~Substrate) +
geom_bar(stat='identity')

enter image description here

关于R:如何将数据聚合成百分比而不丢失 ggplot2 中堆积条形图的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29080645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com