Search code examples
rplotoutliers

Label the outlier in plot()


I want to plot data which has 1 outlier. I want such a plot where observation number of outlier is labeled. For this I have data:

res
           x            x            x            x            x 
 -0.39123009  -0.02907481   0.01003539   0.65495527 -93.81415653

I am trying:

plot(res, type = "o")
cv <- -50
abline(h = -50, lty = 2)

text(res, labels = ifelse(res > cv, names(res), ""), cex = 1, col = 4)  # add labels

Scatterplot appears but there no outlier labelling. How can I add labels according to observation number?

Is it because of repetition of "x"?


Solution

  • It looks like an issue due to the data structure. I took your data and transformed to multiple rows instead of one row. Here your data transformed and the code for plot:

    #Data
    df <- structure(list(V1 = c(-0.39123009, -0.02907481, 0.01003539, 0.65495527, 
    -93.81415653)), class = "data.frame", row.names = c("x", "x.1", 
    "x.2", "x.3", "x.4"))
    

    Code:

    #Plot
    plot(df$V1,type="o")
    cv <- -50
    abline(h = -50, lty = 2)
    text(df$V1,labels=ifelse(df$V1>cv,rownames(df),""),cex=1,col=4)
    

    Output:

    enter image description here