phil_t
phil_t

Reputation: 861

Remove margin on inside of R plot

I am trying to plot return against date, and I would like the line to start and end at the line border. The plot code I am using -

minX = min(hf_instl$date)
maxX = max(hf_instl$date)
plot(df$date, cumsum(df$return), type = "l", col = rgb(0, 0.447, 0.741), xlim = c(minX, maxX), ylim = c(minY, maxY), yaxt = "n", xlab = NA, ylab = NA)

What I get is this -

Plot

Any solution to this in the base plot package? ggplot2 solutions are also okay.

Upvotes: 2

Views: 410

Answers (1)

Florian
Florian

Reputation: 25425

You can use the xaxs property for that:

# create some fake data for the example
hf_instl = data.frame(date=seq(Sys.Date()-200,Sys.Date(),by='day'),return=runif(201,-1,1.5))
df = hf_instl    
minX = min(hf_instl$date)
maxX = max(hf_instl$date)
minY=0
maxY=sum(df$return)+10


# your plot
plot(df$date, cumsum(df$return), type = "l", col = rgb(0, 0.447, 0.741), 
    xlim = c(minX, maxX), ylim = c(minY, maxY), yaxt = "n", 
    xlab = NA, ylab = NA,xaxs='i')

enter image description here

Hope this helps!

Upvotes: 2

Related Questions