Home > Blockchain >  after adding geom_text_repel, "Error: Invalid input: time_trans works with objects of class POS
after adding geom_text_repel, "Error: Invalid input: time_trans works with objects of class POS

Time:03-18

I have tried adding a label to one set of points (lnrmssd) on my ggplot chart, but when I used the geom_text_repel function the graph will not appear. If I remove it the graph appears as it should. I have tried a few different variations from tutiorals but each time I have had the same result.

Here is the line of code giving me issues:

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

I am receiving this error message:

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

When I have tried other variations of the geom_text_repel code I have not recieved any error messages but still the plot does not appear.

Here is a copy of my full code:

---
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

Here is the data I am using:

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

How can I alter the code to successfull add a label, rounded to two decimal points, to the points of lnrmssd?

Any help would be much appreciated.

CodePudding user response:

You haven't mapped the x variable to geom_text_repel. Since all of your layers have the same x mapping, you should include them in the initial ggplot call to avoid repetition. Same with the data argument:

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))

enter image description here

Note though that there is a lot of repetition in your code, and it would be best to pivot your data into long format, particularly if you want a legend:

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')

enter image description here

  • Related