1
votes

Ajouter des lignes verticales à plusieurs datetimes de points aberrants à l'aide de ggplot2

J'ai un dataframe comme celui-ci

library(ggplot2)
library(ggrepel)
ggplot(data = df, aes(PDATETIME,DELTA ))+ 
  ggtitle("Outlier Analysis") + 
  theme(axis.text.x = element_text(angle=90, vjust=1),plot.title = element_text(size = rel(1))) + 
  geom_point(colour="black") +
  geom_vline(aes(xintercept=df$PDATETIME[which(df$Type %in%  "Outlier")],linetype=4, colour="black")) + 
  geom_text_repel(aes(PDATETIME, DELTA, 
                      label = Type),
                  size =4,
                  fontface = 'bold',
                  color = 'red',
                  box.padding = 0.5,
                  point.padding = 0.5,
                  segment.color = 'darkblue',
                  segment.size = 0.5,
                  arrow = arrow(length = unit(0.01, 'npc'))) +
  xlab("PDATETIME")+ 
  ylab("DELTA")

J'essaye de dessiner des lignes verticales aux points aberrants en utilisant ggplot2

PDATETIME <- c("2017-02-23 06:08:39","2017-02-25 15:31:50","2017-03-06 17:11:57","2017-03-15 01:23:51",
               "2017-03-16 15:54:35","2017-03-16 23:48:14","2017-03-18 02:57:41","2017-03-20 05:12:33")
DELTA <- c(2.5,8,3.5,4.5,5.5,8.3,3.3,4)
Type <- c(NA,"Outlier",NA,NA,NA,"Outlier",NA,NA)

df <- data.frame(PDATETIME,DELTA,Type) 
df$PDATETIME <- as.POSIXct(df$PDATETIME,format="%Y-%m-%d %H:%M:%S")

Cela lance une erreur " Erreur: Une variable continue ne peut pas être mappée au type de ligne "

Les points aberrants sont à 2017-02-25 15:31:50, 2017-03-16 23:48:14

Que me manque-t-il ici? Quelqu'un pourrait-il m'indiquer la bonne direction?


0 commentaires

3 Réponses :


1
votes

Vous devez déplacer type de ligne et couleur hors de aes:

geom_vline(aes(xintercept=df$PDATETIME[which(df$Type %in%  "Outlier")]),linetype=4, colour="black")

Vous ne voudriez que linetype et / ou color à l'intérieur de aes () si vous voulez qu'ils varient selon une variable, comme df $ type .


0 commentaires

2
votes

Nous n'avons pas besoin de aes dans geom_vline , essayez:

geom_vline(xintercept = df$PDATETIME[ which(df$Type %in%  "Outlier") ], linetype = 4, colour = "black")


0 commentaires

2
votes

type de ligne et couleur ne varient pas, vous pouvez donc le déplacer hors de aes . Je vous recommande également de modifier votre code en:

geom_vline(data = df[which(df$Type %in% "Outlier"),], 
           aes(xintercept = PDATETIME), 
           linetype = 4, colour = "black") 


0 commentaires