gpt4 book ai didi

r - 将文本添加到 ggplot 中的 Axis 标签

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

我根据下表绘制了一个图表。

        BoatPhs      fit        se    lower    upper
1 Before 3.685875 0.3287521 3.038621 4.333130
2 After0-20NTA 3.317189 0.6254079 2.085872 4.548506
3 After0-20TAA 5.579384 0.5696270 4.457890 6.700878
4 After0-20TAP 3.932360 0.4304098 3.084960 4.779760
5 After20-40NTA 4.522714 0.7771793 2.992586 6.052842
6 After20-40TAA 4.505207 0.5500699 3.422217 5.588196
7 After20-40TAP 3.602183 0.3880538 2.838174 4.366192
8 ApproachNTA 4.039599 0.5688482 2.919638 5.159560
9 ApproachTAA 4.421112 0.5176408 3.401969 5.440255
10 ApproachTAP 4.497809 0.3978328 3.714547 5.281071

船相是 x Axis ,拟合绘制在 y Axis 上。目前我的 Axis 刻度为 Before、ApproachNTA、ApproachTAA、ApproachTAP、After0-20NTA 等。

我最初将这些标签旋转了 90 度,以便更好地查看它们,但我不喜欢它的外观。我目前已经重命名了标签(使用下面的代码),所以现在在 Axis 刻度上显示的是 x Axis 上的“Before NTA TAA TAP NTA TAA TAP NTA TAA TAP”。

我希望添加一些文本以便对标签进行分组。比如NTA TAA TAP第一组下的“Approach”,第二组下的“After0-20”和第三组下的“After20-40”,但我不确定在剧情之外该怎么做。尝试使用注释功能,但它只允许绘图区域内的文本。

如有任何帮助,我们将不胜感激。

干杯

# Speed plot
Spdplot <- ggplot(y, aes(x=BoatPhs, y=fit, colour=BP, ymax=max(fit)*1.05)) +
geom_point(position = position_dodge(width = 0.5, height = 0), size = 4) +
geom_errorbar(aes(ymin=fit-se, ymax=fit+se), position = position_dodge(width = 0.5, height = 0), width = 0.5, size = 0.75) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))


#run Spdplot
Spdplot




#sets x values in results order + relables x + y axes
Spdplot + scale_x_discrete(limits=c("Before","ApproachNTA","ApproachTAA","ApproachTAP","After0-20NTA","After0-20TAA","After0-20TAP", "After20-40NTA","After20-40TAA","After20-40TAP"),
labels=c("Before" = "Before" ,"ApproachNTA" ="NTA","ApproachTAA" = "TAA","ApproachTAP" = "TAP",
"After0-20NTA" = "NTA","After0-20TAA" = "TAA","After0-20TAP" = "TAP",
"After20-40NTA" = "NTA","After20-40TAA" = "TAA","After20-40TAP" = "TAP")) +
xlab("Boat phase") + ylab("Group travel speed (km/hr)") +
annotate("text", x=6, y=6.2, label="***", size = 6) +
annotate("text", x=4, y=4.95, label="*", size = 6) +
theme(axis.title.x = element_text(face="bold", size = 16, vjust= -0.5), axis.title.y = element_text(face="bold", size = 16, vjust= 1),
axis.text.x = element_text(size=14, face="bold"), axis.text.y = element_text(face="bold", size=14), legend.text=element_text(size=14)) +
theme(legend.title = element_blank()) +
scale_colour_manual(values=c("#3399FF","#FF0000","#33CC33","#000000"),
breaks=c("Before", "Approach", "After0-20", "After20-40"),
labels=c("Before", "Approach", "After0-20", "After20-40"))

enter image description here

最佳答案

如果您为 ggplot 提供所需的数据,可以避免使用 annotatescale_x_discrete 中的手动标记来避免很多麻烦。在这里,我重新创建了您的数据集,然后为您的绘图添加了基本信息。

# Recreate your data
y <- read.table(textConnection('BoatPhs fit se lower upper
1 Before 3.685875 0.3287521 3.038621 4.333130
2 After0-20NTA 3.317189 0.6254079 2.085872 4.548506
3 After0-20TAA 5.579384 0.5696270 4.457890 6.700878
4 After0-20TAP 3.932360 0.4304098 3.084960 4.779760
5 After20-40NTA 4.522714 0.7771793 2.992586 6.052842
6 After20-40TAA 4.505207 0.5500699 3.422217 5.588196
7 After20-40TAP 3.602183 0.3880538 2.838174 4.366192
8 ApproachNTA 4.039599 0.5688482 2.919638 5.159560
9 ApproachTAA 4.421112 0.5176408 3.401969 5.440255
10 ApproachTAP 4.497809 0.3978328 3.714547 5.281071'))

# Separate the boat phase from the time period
y$boat.phase <- gsub('.*(NTA|TAA|TAP)','\\1',y$BoatPhs)
y$period <- gsub('(.*)(NTA|TAA|TAP)','\\1',y$BoatPhs)

# Give the time period an ordering.
y$period <- factor(y$period,
levels = c("Before", "Approach", "After0-20", "After20-40"),
ordered=TRUE)

# Add the stars manually, but these could be calculated programatically.
y$stars <- c('', '', '***', '', '', '', '', '', '', '*')

现在,当您使用 ggplot 时,您可以将星星作为美学传递,而无需手动标记任何内容。要“分组”你的 x Axis 标签,你可以使用 facets:

ggplot(y, aes(x=boat.phase, y=fit, ymax=fit+se, ymin=fit-se)) + 
geom_point() + geom_errorbar(width = 0.5, size = 0.75) +
geom_text(aes(y=fit+se+0.1, label=stars), size=6) +
facet_grid( ~ period, scales="free_x", space="free_x") +
xlab("Boat phase") + ylab("Group travel speed (km/hr)") +
theme_bw()

enter image description here

着色在这里是多余的,但如果您愿意,可以添加它。我还会在下一个示例中添加您的自定义主题。

ggplot(y, aes(x=boat.phase, y=fit, ymax=fit+se, ymin=fit-se, colour=period)) + 
geom_point() + geom_errorbar(width = 0.5, size = 0.75) +
geom_text(aes(y=fit+se+0.1, label=stars), size=6) +
facet_grid( ~ period, scales="free_x", space="free_x") +
xlab("Boat phase") + ylab("Group travel speed (km/hr)") +
scale_colour_manual(values=c("#000000", "#33CC33", "#3399FF","#FF0000")) +
theme(axis.title = element_text(face="bold", size = 16),
axis.title.x = element_text(vjust= -0.5),
axis.title.y = element_text(vjust= 1),
axis.text = element_text(size=14, face="bold"),
panel.grid = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))

enter image description here

关于r - 将文本添加到 ggplot 中的 Axis 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111809/

25 4 0