0

The command you suggested serves the purpose to some extent But I am still looking for a command/function to give smoother curves. I am giving a reproducible code below which is drawing rather sharp edges or more crudely: the line joining the points is too sharp especially at the moment it starts from one point to another. Sorry for non-technical language as I am still unfamiliar with the field. Thanks for the help provided on this issue. Please suggest the modifications in the code below to get smoother curves.

xdata1<-c(6:11)
ydata1<-c(-0.75132894, -1.71909555,  -0.62653171, 0.49512191, 0.29201836, 0.31094460)
plot(NULL,NULL,xlim=xlims,ylim=ylims,axes=FALSE, ann=FALSE) 
axis(1,cex.axis=0.7,mgp=c(3, .3, 0))
axis(2, las=1,cex.axis=0.7,at=c(-2,-1,0,1,2), mgp=c(3, .7, 0))
mtext(side = 1, text =expression('Year'), line = 1,font=15)
mtext(side = 2, text = expression('Variable'), line = 1.5,font=15)
lines(smooth.spline(xdata1,ydata1, df=5), col='red',type="l", pch=22, lty=1, lwd=1)
#######Updated Query Finishes Here

I have to plot very small magnitude data against time steps of 1. Please see the sample data below:

xdata<-c(1:48)
ydata<- c(0.325563413,0.401913414,0.221939845,0.19881055,
         -0.05918293,-1.108143815,-0.220563332,-0.148715078,
         -0.14998762,0.131610695,0.249923598,0.246891873,
          0.656812019,0.524436114,0.23875397,0.200695075,
         -0.015974087,-0.611863249,0.121994831,-0.143103421,
         -0.142109609,0.101451935,0.160242421,0.232404601,
          0.348305745,0.231109382,0.334988321,0.263046902,-0.058154333,
       -1.032276818,-0.352068888,-0.13082767,-0.134611511,0.116967421,
          0.268706409,0.232776855,0.39515544,0.540317537,0.424281195,
          0.3061158,-0.210735495,0.023705618,0.473338271,0.270527033,
         -0.165394174,0.268773501,0.202437269,0.305577906)

Please help me in plotting a smooth line without highlighting individual points for the data.

Thanks in advance,

Dason
  • 60,663
  • 9
  • 131
  • 148
aaron
  • 6,339
  • 12
  • 54
  • 80

2 Answers2

6

Not sure what "without highlighting individual points" means, but here's one way to get a smooth line:

plot(xdata,ydata)
lines(smooth.spline(xdata,ydata, df=10), col = "red")

See also: ?loess.smooth, apropos("smooth"). Searching for "[r] smooth plot" finds How to fit a smooth curve to my data in R? ...

enter image description here

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

Specifying the type to "l" in plot will just give you lines.

plot(xdata, ydata, type = "l")
Dason
  • 60,663
  • 9
  • 131
  • 148
  • Hi, thanks for help. Please see the updated query and kindly help me with the same – aaron Mar 07 '12 at 19:59
  • Hi, nothing is wrong but the data I have is giving me straight lines from which I wish to create curved lines connecting the points. Please see the sample data and reproducible code that I had posted. – aaron Mar 07 '12 at 21:59
  • e.g. a sharp 'V' like sign is generated in sample code posted above. From this data I wish to create curved lines so that 'V' sign has a curve at its base. Please consider xlims<-c(1,60) and ylims<-c(-2,2) while running the code. Thanks... – aaron Mar 07 '12 at 22:07
  • It sounds like you're trying to replicate what Excel does by putting an interpolating curve through the points. Please don't, it adds no value compared to just joining them with straight lines. It's also possibly misleading by lending the curve an artificial air of precision. – Hong Ooi Mar 08 '12 at 01:12