Gianluca
Gianluca

Reputation: 6657

Remove outliers from line graph

I want to draw a line graph where there is a clear outlier. The following dataset is a subset of the one I'm using:

          Date PCROI
717 2014-06-28    38
718 2014-06-29    41
719 2014-06-30    36
720 2014-07-01   734
721 2014-07-02    60
722 2014-07-03    78
723 2014-07-04    90
724 2014-07-05    68
725 2014-07-06    55
726 2014-07-07    57

If I plot this using ggplot2 the graph is not that bad..

ggplot(data = example, aes(x = Date, y = PCROI)) + geom_line()

Plot

Unfortunately the time series I'm working with is much longer than the median of PCROI is 51. How can I adapt the chart without excluding the value from the dataset?

Upvotes: 1

Views: 2124

Answers (1)

tkmckenzie
tkmckenzie

Reputation: 1363

You can use the subset command if you have some criteria for outliers (e.g., if PRCOI is greater than 100) and if you're fine with interpolating the deleted point:

example <- read.table(header = TRUE, text = "          Date PCROI
717 2014-06-28    38
718 2014-06-29    41
719 2014-06-30    36
720 2014-07-01   734
721 2014-07-02    60
722 2014-07-03    78
723 2014-07-04    90
724 2014-07-05    68
725 2014-07-06    55
726 2014-07-07    57")
example$Date <- as.Date(example$Date)

ggplot(subset(example, PCROI <= 100), aes(Date, PCROI)) + geom_line()

Upvotes: 1

Related Questions