after adding geom_text_repel, "Error: Invalid input: time_trans works with objects of class POSIXct only"

after adding geom_text_repel, "Error: Invalid input: time_trans works with objects of class POSIXct only"

我曾尝试为我的 ggplot 图表上的一组点 (lnrmssd) 添加标签,但是当我使用 geom_text_repel 函数时,图表不会出现。如果我删除它,图表会按原样显示。我尝试了一些与教程不同的变体,但每次我都得到相同的结果。

这是给我带来问题的代码行:

geom_text_repel(data = hrv, aes(lnrmssd, label = lnrmssd))+

我收到此错误消息:

Error: Invalid input: time_trans works with objects of class POSIXct only

当我尝试了 geom_text_repel 代码的其他变体时,我没有收到任何错误消息,但仍然没有出现情节。

这是我的完整代码的副本:

---
title: "Add Label"
author: "AG"
date: '2022-03-17'
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)



library(tidyverse)
library(readxl)
library(here)
library(knitr)
library(caTools)
library(httpuv)
library(zoo)
library(RcppRoll)
library(dplyr)
library(smooth)
library(magrittr)
library(reshape2)
library(ggforce)
library(ggrepel)
library(kableExtra)


###data wrangle

hrv <- read_excel(here("hrvdata.xlsx"))
hrv <- na.omit(hrv)

hrv <- hrv %>% mutate("HRVSD" = sd(lnrmssd, na.rm = T),
    "HRVRM" = rollmean(lnrmssd,7, na.pad = T, align = 'right'),
      urmssd = round(HRVRM + 1.5 * HRVSD, 3),
             lrmssd = round(HRVRM - 1.5 * HRVSD, 3),
             l2rmssd = round(HRVRM - .75 * HRVSD, 3))


###Theme
 theme_HRVmarkdown <-  theme(
    axis.text.y = element_text(colour="grey20",size=80,angle=0,hjust=1,vjust=0,face="plain"),  
    axis.text.x = element_text(colour="grey20",size=60,angle=90,hjust=.5,vjust=.5,face="plain"),
    axis.title.x = element_text(colour="black",size=70,angle=0,hjust=.5,vjust=0,face="plain"),
    axis.title.y = element_text(colour="black",size=70,angle=90,hjust=.5,vjust=.5,face="plain"),
    legend.text = element_text(size = 100),
    strip.text = element_text(size = 100, face='bold'),
    strip.background = element_rect(fill = 'azure2', colour=NA),
    legend.key.width = unit(15, 'line'),
    legend.spacing.x = unit(6, 'cm'),
    legend.title = element_text(size=90),
    legend.title.align = 0.5,
    panel.background = element_rect(fill = "azure2",
                                colour = "azure2",
                                size = 0.5, linetype = "solid")


)
###CREATE PLOT
  

ggplot()+
   geom_line(hrv, mapping = aes(x=date, y=lnrmssd), colour="grey", size=2.5)+
  geom_line(hrv, mapping = aes(x=date, y=HRVRM), colour="black", size=2.5)+
  geom_line(hrv, mapping = aes(x=date, y=urmssd), colour="green", size=2.5, linetype='dashed')+
  geom_line(hrv, mapping = aes(x=date, y=lrmssd), colour="red", size=2.5, linetype='dashed')+
    geom_line(hrv, mapping = aes(x=date, y=l2rmssd), colour="orange", size=2.5, linetype='dashed')+
 
   geom_point(hrv, mapping = aes(x=date, y=lnrmssd), colour="grey", size=3)+
  geom_point(hrv, mapping = aes(x=date, y=HRVRM), colour="black", size=3)+
  geom_point(hrv, mapping = aes(x=date, y=urmssd), colour="green", size=3)+
  geom_point(hrv, mapping = aes(x=date, y=lrmssd), colour="red", size=3)+
    geom_point(hrv, mapping = aes(x=date, y=l2rmssd), colour="orange", size=3)+
      
### Trying to add label 
geom_text_repel(data = hrv, aes(lnrmssd, label = lnrmssd))+
                   
    geom_hline(yintercept = 1.1, alpha=0.9, colour='black')+
    theme_minimal()+
    theme_HRVmarkdown

这是我使用的数据:

date reading lnrmssd
2022-02-17 68.9077 4.232768
2022-02-18 62.4076 4.133895
2022-02-19 70.7072 4.258547
2022-02-21 81.9841 4.406525
2022-02-22 74.8368 4.315310
2022-02-23 84.9140 4.441639
2022-02-24 72.4620 4.283062
2022-02-25 79.0891 4.370575

如何更改代码以成功添加一个标签,四舍五入到小数点后两位,到 lnrmssd 的点数?

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

您还没有将 x 变量映射到 geom_text_repel。由于所有层都具有相同的 x 映射,因此您应该将它们包含在初始 ggplot 调用中以避免重复。与数据参数相同:

ggplot(data = hrv, mapping = aes(x = date)) +
  geom_line(aes(y = lnrmssd), colour = "grey", size = 2.5)+
  geom_line(aes(y = HRVRM), colour = "black", size = 2.5)+
  geom_line(aes(y = urmssd), colour = "green", size = 2.5, linetype = 'dashed') +
  geom_line(aes(y = lrmssd), colour = "red", size = 2.5, linetype = 'dashed') +
  geom_line(aes(y = l2rmssd), colour = "orange", size = 2.5, linetype = 'dashed') +
  geom_point(aes(y = lnrmssd), colour = "grey", size = 3) +
  geom_point(aes(y = HRVRM), colour = "black", size = 3) +
  geom_point(aes(y = urmssd), colour = "green", size = 3) +
  geom_point(aes(y = lrmssd), colour = "red", size = 3) +
  geom_point(aes(y = l2rmssd), colour = "orange", size = 3) +
  geom_text_repel(aes(x = date, lnrmssd, label = lnrmssd))

请注意,虽然您的代码中有很多重复,但最好将数据转换为长格式,特别是如果您想要图例:

ggplot(data = tidyr::pivot_longer(hrv, -c(1,2, 4)), 
       mapping = aes(x = date, y = value, color = name)) +
  geom_line(aes(linetype = name), size = 2.5) +
  geom_point() +
  scale_color_manual(values = c('black', 'orange', 'grey', 'red', 'green')) +
  scale_linetype_manual(values = c(1, 2, 1, 2, 2)) +
  geom_text_repel(aes(x = date, y = lnrmssd, label = round(lnrmssd, 2)), 
                  data = hrv[c(1, 3)], inherit.aes = FALSE,
                  color = 'black')