gpt4 book ai didi

r - 如何对齐峰标签?

转载 作者:行者123 更新时间:2023-12-04 00:12:50 24 4
gpt4 key购买 nike

我正在尝试用 R 中的峰值绘制数据。绘图本身进展顺利,但在标记相关峰值时遇到了问题。我目前的标签系统,详细如下,将峰值标签奇怪地移到一边,导致线条相互交叉。有没有办法将标签与峰本身对齐,或者以其他方式在美学上组织它们?

以下代码重现了我的问题,使用 this data .

library(ggplot2)
library(ggpmisc)
library(ggrepel)

x=read.csv("data.csv")

colnames(x)=c("wv", "abs")

ggplot(x, aes(x=wv, y=abs)) + geom_line() + xlab(bquote('Wavenumbers ('~cm^-1*')')) + ylab("Absorbance (A.U.)") + scale_x_reverse(limits=c(2275,1975), expand=c(0,0)) + ylim(-0.01,0.29) + stat_peaks(colour = "black", span = 11, geom ="text_repel", direction = "y", angle = 90, ignore_threshold = 0.09, size = 3, x.label.fmt = "%.2f", vjust = 1, hjust = 0, segment.color = "red") + ggtitle("FTIR - Carbon Monoxide, Fundamentals")

Problematic Labels

最佳答案

hjust = 0.5 应该会更好。使用 hjust = 0 会使您的标签稍微向右对齐,文本的上边缘与每个峰值的中间对齐。

这是一个可重现的示例,它不依赖可能在该链接上不可用的外部数据。 (OP 数据集的应用见底部。)

library(ggpmisc)
library(ggrepel)
library(ggplot2)
x <- data.frame(wv = 2300:2000)
x$abs = abs(cos(x$wv/50) * sin(x$wv/2))
ggplot(x, aes(wv, abs)) +
geom_line() +
stat_peaks(colour = "black", span = 11,
geom ="text_repel", direction = "y",
angle = 90, ignore_threshold = 0.09,
size = 3, x.label.fmt = "%.2f",
vjust = 1, hjust = 0.5, segment.color = "red") +
scale_x_reverse(limits = c(2300,2000))

enter image description here

这里是加载原始数据:

library(readr)
x <- read_csv("~/Downloads/CO-FTIR Spectrum-1800 mTorr-2021.csv")
colnames(x)=c("wv", "abs")

这里添加了这些参数:

box.padding = 0.0, nudge_y = 0.02,

enter image description here

关于r - 如何对齐峰标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67393067/

24 4 0