Cobain
Cobain

Reputation: 195

Smooth curved graphs in R

I have a line graph which is currently displaying straight direct lines between each point on the graph.

Graph with straight lines

Is there a way of making this so that the line is a smooth curve rather than simply 'join-the-dots'?

Here is the code extract I'm using to generate the above graph:

  plot(dataPlotsX,dataPlotsY, type="o", col="red", ann=FALSE)

  title(main=plotTitle, col.main="red", font.main=4)
  title(xlab="Time (hours)", col.lab="red")
  title(ylab="Discharge (m^3/s per 10mm)", col.lab="red")

For reference;

dataPlotsX = c(0, 1, 2, 3, 4, 5, 6) 
dataPlotsY = c(0.000000, 5.772690, 17.303517, 12.981276,  2.886345,  0.000000,  0.000000)

Many thanks

Cobain

Upvotes: 2

Views: 495

Answers (1)

digEmAll
digEmAll

Reputation: 57210

You can interpolate using a spline, e.g. :

# run this code just after your example
# to add a spline interpolating the points
s <- smooth.spline(dataPlotsX,dataPlotsY)
smooth <- predict(s,seq.int(from=min(dataPlotsX),to=max(dataPlotsX),length.out=10000))
lines(smooth,type="l",col='blue')

enter image description here

Upvotes: 3

Related Questions