gpt4 book ai didi

R:geom_point - 如何在图形顶部显示统计信息

转载 作者:行者123 更新时间:2023-12-04 10:23:04 25 4
gpt4 key购买 nike

我用 geom_point 做了一个图来自 ggplot2 (只是显示其中的一部分)。颜色代表 3 个类别。黑条是平均的(与问题无关)。

Part of the geom_point plot

数据结构如下(存储在列表中):

                     V1  V2     V3
1 L. brevis 5 class1
3 L. sp. 13 class1
4 L. rhamnosus 14 class1
5 L. lindneri 17 class1
6 L. plantarum 17 class1
7 L. acidophilus 18 class1
8 L. acidophilus 18 class1
10 L. plantarum 18 class1
... ... .. ...

哪里 V2是数据点在 y 轴上的位置和 V3是类(颜色)。

现在我想在图的顶部显示三个类中每个类的百分比(或者甚至可以作为饼图 :-) )。我为图像上的“嗜酸乳杆菌”做了一个例子(66.7%/33.3%)。

理想情况下解释组的图例也是由 R 生成的,但我可以手动完成。

我怎么做?

忘记在“嗜酸乳杆菌”列顶部添加第 3 组的 0%... 抱歉。

编辑:这里是 ggplot2 代码:
p <- ggplot(myData, aes(x=V1, y=V2)) +
geom_point(aes(color=V3, fill=V3), size=2.5, cex=5, shape=21, stroke=1) +
scale_color_manual(values=colBorder, labels=c("Class I","Class II","Class III","This study")) +
scale_fill_manual(values=col, labels=c("Class I","Class II","Class III","This study")) +
theme_bw() +
theme(axis.text.x=element_text(angle=50,hjust=1,face="italic", color="black"), text = element_text(size=12),
axis.text.y=element_text(color="black"), panel.grid.major = element_line(color="gray85",size=.15), panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(), axis.ticks = element_line(size = 0.3), panel.border = element_rect(fill=NA, colour = "black", size=0.3)) +
stat_summary(aes(shape="mean"), fun.y=mean, size = 6, shape=95, colour="black", geom="point") +
guides(fill=guide_legend(title="Class", order=1), color=guide_legend(title="Class",order=1), shape=guide_legend(title="Blup", order=2))

最佳答案

选项 A:辅助轴
您可以使用辅助 x 轴(ggplot2 v2.2.0 的新功能)执行此操作,但很难使用 x 轴上的分类变量,因为它不适用于 scale_x_discrete() , 仅 scale_x_continuous() .因此,您必须将因子转换为整数,基于此进行绘图,然后覆盖主 x 轴上的标签。
例如:

set.seed(123)
df <- iris[sample.int(nrow(iris),size=300,replace=TRUE),]

# Assume we are grouping by species
# Some group-level stats -- how about count and mean/sdev of sepal length
library(dplyr)
df_stats <- df %>%
group_by(Species) %>%
summarize(stat_txt = paste0(c('N=','avg=','sdev='),
c(n(),round(mean(Sepal.Length),2),round(sd(Sepal.Length),3) ),
collapse='\n') )

library(ggplot2)
ggplot(data = df,
aes(x = as.integer(Species),
y = Sepal.Length)) +
geom_point() +
stat_summary(aes(shape="mean"), fun.y=mean, size = 6, shape=95,
colour="black", geom="point") +
theme_bw() +
scale_x_continuous(breaks=1:length(levels(df$Species)),
limits = c(0,length(levels(df$Species))+1),
labels = levels(df$Species),
minor_breaks=NULL,
sec.axis=sec_axis(~.,
breaks=1:length(levels(df$Species)),
labels=df_stats$stat_txt)) +
xlab('Species') +
theme(axis.text.x = element_text(hjust=0))
enter image description here
选项 B: grid.arrange您的统计数据作为主图表顶部的单独图表。
这更简单一些,但两个图表并没有完全对齐,可能是因为顶部图表的轴上的刻度和标签被抑制了。
library(ggplot2)
library(gridExtra)
p <-
ggplot(data = df,
aes(x = Species,
y = Sepal.Length)) +
geom_point() +
stat_summary(aes(shape="mean"), fun.y=mean, size = 6, shape=95,
colour="black", geom="point") +
theme_bw() +
theme(axis.text.x = element_text(angle=45, hjust=1, vjust=1))
annot <-
ggplot(data=df_stats, aes(x=Species, y = 0)) +
geom_text(aes(label=stat_txt), hjust=0) +
theme_minimal() +
scale_x_discrete(breaks=NULL) +
scale_y_continuous(breaks=NULL) +
xlab(NULL) + ylab('')

grid.arrange(annot, p, heights=c(1,8))
enter image description here

关于R:geom_point - 如何在图形顶部显示统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166893/

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