Ggplot2 geom_line () and anti-aliasing

I am trying to create a GGPLOT2 smoothed line graph that looks more like this

enter image description here

Source: http://www.esrl.noaa.gov/psd/enso/mei/

and less:

enter image description here

Source: https://dl.dropboxusercontent.com/u/16400709/StackOverflow/Rplot02.png

My data is available in Dropbox .

After looking at previous posts, I used the following code:

#MEI Line Graph

d4 <- read.csv("https://dl.dropboxusercontent.com/u/16400709/StackOverflow/Data_MEI.csv")
head(d4,n=20)

MEI<-ggplot(d4,aes(x=d4$Date, y=d4$MEI,group=1))+geom_line()

MEI+stat_smooth(method ="auto",level=0.95)

      

It seems to me that I need to reduce the amount of anti-aliasing, but I have yet to figure out how to achieve this.

d4s<-SMA(d4$MEI,n=8)
plot.ts(d4s)

      

SMA () works well, but I cannot get it to work with ggplot Any hints would be appreciated!

+3


source to share


1 answer


Keep in mind that the MEI is 2 months long, so it already has anti-aliasing built in. Assuming you are using MEI data published by NOAA ESRL, you should be able to create the same plot.

First of all, you need to configure the system, as you will be working with the watch:

# set things up  ----
working.dir = file.path('/code/R/StackOverflow/')
setwd(working.dir)
Sys.setenv(TZ='GMT')

      

load your data and read it in

d.in <- read.csv("MEI.txt")

      

The next step is to format the dates correctly.

d.in$Date <- as.POSIXct(d.in$Date,
                        format = "%d/%m/%Y", 
                        tz = "GMT")

      

and because we need to figure out where things cross the x-axis, we will have to work in decimal dates. Use the Epoch value:

d <- data.frame(x = as.numeric(format(d.in$Date,
                                      '%s')),
                y = d.in$MEI)

      

We can now find the zero crossings. We'll use Beroe's example for this .



rx <- do.call("rbind",
              sapply(1:(nrow(d)-1), function(i){
                f <- lm(x~y, d[i:(i+1),])
                if (f$qr$rank < 2) return(NULL)
                r <- predict(f, newdata=data.frame(y=0))
                if(d[i,]$x < r & r < d[i+1,]$x)
                  return(data.frame(x=r,y=0))
                else return(NULL)
              }))

      

and stick with what at the end of the original data:

d2 <- rbind(d,rx)

      

now convert back to dates:

d2$date <- as.POSIXct(d2$x,
                      origin = "1960-01-01",
                      format = "%s",
                      tz = "GMT")

      

now we can make the plot:

require(ggplot2)
ggplot(d2,aes(x = date,
              y = y)) + 
  geom_area(data=subset(d2, y<=0), fill="blue") + 
  geom_area(data=subset(d2, y>=0), fill="red") + 
  scale_y_continuous(name = "MEI")

      

and that gives you this:

enter image description here

Now do you really need to iron it out?

+10


source







All Articles