gpt4 book ai didi

r - 结合气泡图和表格的嵌套图形

转载 作者:行者123 更新时间:2023-12-05 02:35:22 25 4
gpt4 key购买 nike

尊敬的社区成员:

this article发现下图(图3):

figure

它是一种嵌套气泡图与表格相结合的类型,用于不同分类等级的层次结构。它从门延伸到类再到目,显示每个分类的相对丰度 (%)。

在方法部分,我能找到的唯一对 R 代码的引用是在某些分析中使用 vegan 包。我找不到关于如何创建图形的描述。

我想知道如何为我的数据重新创建此类图。有人可以指导我正确的方向吗?

最佳答案

像这样的东西应该是一个不错的起点……我想。它并不完美,但应该可以让您按照自己的意愿行事。

library(reshape)
library(ggplot2)
library(data.table)
#library(ggExtra)
library(gridExtra)

# base code was taken from here and modified
# https://stackoverflow.com/questions/54638898/how-to-plot-proportion-data-with-a-bubble-plot-in-r
#
# code also taken from here (to reduce spacing)
# https://stackoverflow.com/questions/15556068/removing-all-the-space-between-two-ggplots-combined-with-grid-arrange

Species <- as.character(c(1:12))
name1 <-as.numeric(c(0.17,0.011,0.022,0.003,0.51,0.1,0.01,0.03,0.004,0.06,0.07,0.01))
name2<-as.numeric(c(0.197,0.005,0.027,0.01,0.337,0.157,0.008,0.038,0.017,0.17,0.032,0.002))
data<-as.data.frame(cbind(Species,name1,name2))
data$name1 <- as.numeric(as.character(data$name1));
data$name2 <- as.numeric(as.character(data$name2))
data2<-melt(data)

# gets a roughly similar sort of plot
p <- ggplot2::ggplot(data2,aes(x=variable, y=factor(Species, levels=unique(Species))))+
geom_point(aes(size=value))+
labs(y="Prey Items",x="Species")+
theme_classic() +
scale_size_area( limits = c(0,1),max_size = 20) +

#geom_richtext(position='identity',aes(label=Species), fill="grey", hjust = +2.5)
geom_richtext(position='identity',
aes(x='name1',label=factor(Species),
width = unit(0.01, "npc")),
hjust = 3.0,
fill="grey") +

# remove the y axis and the x axis ticks
theme(axis.line =element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y=element_blank(),
plot.margin=unit(c(1,1,1,1), "cm"),
legend.position = "none")

# shows the first graph
p

# create new data with additional variables (more species??)
Species <- as.character(c(13:18))
name1 <-as.numeric(c(0.17,0.011,0.022,0.003,0.51,0.1))
name2<-as.numeric(c(0.197,0.005,0.027,0.01,0.337,0.157))
data4<-as.data.frame(cbind(Species,name1,name2))
data4$name1 <- as.numeric(as.character(data4$name1));
data4$name2 <- as.numeric(as.character(data4$name2))
data4<-melt(data4)

# combine the data for a full set
data5 = rbind(data2, data4)

# plot the second figure
p2 <- ggplot2::ggplot(data5,aes(x=variable, y=factor(Species, levels=unique(Species))))+
geom_point(aes(size=value))+
labs(y="Prey Items",x="Species")+
theme_classic() +
scale_size_area( limits = c(0,1),max_size = 20) +

#geom_richtext(position='identity',aes(label=Species), fill="grey", hjust = +2.5)
geom_richtext(position='identity',
aes(x='name1',label=factor(Species),
width = unit(0.01, "npc")),
hjust = 3.0,
fill="grey") +

# remove the y axis and the x axis ticks
# the plot margin changes the horizzontal space between the second plot and first
theme(axis.line =element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y=element_blank(),
plot.margin=unit(c(1,1,1,-3), "cm"))


# shows the second graph
p2

# combine the plots into one (expand the display if it looks iffy in R/Rstudio)
grid.arrange(p,p2, ncol=2)

关于r - 结合气泡图和表格的嵌套图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70590780/

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