gpt4 book ai didi

R图: label by group

转载 作者:行者123 更新时间:2023-12-01 09:20:26 25 4
gpt4 key购买 nike

我正在处理的数据是聚类数据,一组内有多个观察结果,我生成了一个毛毛虫图并希望为每个组(zipid)而不是每一行进行标记,我当前的图形和代码如下所示:

  text = hosp_new[,c("zipid")]
ggplot(hosp_new, aes(x = id, y = oe, colour = zipid, shape = group)) +
# theme(panel.grid.major = element_blank()) +
geom_point(size=1) +
scale_shape_manual(values = c(1, 2, 4)) +
geom_errorbar(aes(ymin = low_ci, ymax = high_ci)) +
geom_smooth(method = lm, se = FALSE) +
scale_linetype_manual(values = linetype) +
geom_segment(aes(x = start_id, xend = end_id, y = region_oe, yend = region_oe, linetype = "4", size = 1.2)) +
geom_ribbon(aes(ymin = region_low_ci, ymax = region_high_ci), alpha=0.2, linetype = "blank") +
geom_hline(aes(yintercept = 1, alpha = 0.2, colour = "red", size = 1), show.legend = "FALSE") +
scale_size_identity() +
scale_x_continuous(name = "hospital id", breaks = seq(0,210, by = 10)) +
scale_y_continuous(name = "O:E ratio", breaks = seq(0,7, by = 1)) +
geom_text(aes(label = text), position = position_stack(vjust = 10.0), size = 2)

毛毛虫图:

caterpillar plot

每种颜色代表一个区域,我只想要每个区域一个标签,但不知道如何删除该图中的重复标签。有什么想法吗?

最佳答案

关键是让 geom_text 为每个 zipid 只返回一个值,而不是多个值。如果我们希望每个 zipid 标签位于其组的中间,那么我们可以使用 id 的平均值作为每个标签的 x 坐标。在下面的代码中,我们使用 stat_summaryh(来自 ggstance 包)来计算标签 x 坐标的平均 id 值,并为每个 zipid 返回一个标签。

library(ggplot2)
theme_set(theme_bw())
library(ggstance)

# Fake data
set.seed(300)
dat = data.frame(id=1:100, y=cumsum(rnorm(100)),
zipid=rep(LETTERS[1:10], c(10, 5, 20, 8, 7, 12, 7, 10, 13,8)))

ggplot(dat, aes(id, y, colour=zipid)) +
geom_segment(aes(xend=id, yend=0)) +
stat_summaryh(fun.x=mean, aes(label=zipid, y=1.02*max(y)), geom="text") +
guides(colour=FALSE)

enter image description here

您还可以使用分面,如 @user20650 所提到的。在下面的代码中,panel.spacing.x=unit(0,'pt') 删除分面面板之间的空间,而 expand=c(0,0.5) 添加每个面板侧面有 0.5 个单位的填充。这些共同确保刻度线之间的间距恒定,甚至跨面也是如此。

ggplot(dat, aes(id, y, colour=zipid)) +
geom_segment(aes(xend=id, yend=0)) +
facet_grid(. ~ zipid, scales="free_x", space="free_x") +
guides(colour=FALSE) +
theme_classic() +
scale_x_continuous(breaks=0:nrow(dat),
labels=c(rbind(seq(0,100,5),'','','',''))[1:(nrow(dat)+1)],
expand=c(0,0.5)) +
theme(panel.spacing.x = unit(0,"pt"))

enter image description here

关于R图: label by group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46027258/

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