How to build smoother curves in R

Using R , I drew a shaded plot. If you see curves, they are not smooth. How to make them smooth? Even great stories are much smoother. Device features: windows 7, screen resolution = 1366 x 768 (max.)

Here is the plot.

plot

The following code is used to draw a graph.

 plot(NA,xlim=c(0,1),ylim=c(0,1),xlab="delta",ylab="K", xaxs="i",yaxs="i") # Empty plot a1 <- curve((x+x^7-x^2-x^4)/(1+xx^3-x^4), from=0, n=450000, add = TRUE) # First curve 

Full code is available here.

+7
source share
3 answers

I am sure that if you draw your figure, for example, as pdf, the curves are completely smooth. This is an Rgui internal display that shows the curve as non-smooth, and using copied paste, which can cause problems. It’s better to immediately put it in a file, for example:

 # Open device: pdf("D:/test.pdf") #change for appropriate file path plot(NA,xlim=c(0,1),ylim=c(0,1),xlab="delta",ylab="K", xaxs="i",yaxs="i") a1 <- curve((x+x^7-x^2-x^4)/(1+xx^3-x^4), from=0, n=450000, add = TRUE) dev.off() #close device 

Now let's look at the pdf and it looks completely normal. If you want, for example, a jpg image, use the jpeg function, etc. See ?jpeg for more details on how to save both tiff, jpeg, png or bmp, as well as arguments for image size, resolution, etc. .d.

(note that the terms used here, etc. may not be entirely correct, I am not completely familiar with this terminology, someone more intelligent can edit if necessary).

+5
source

At the moment, it seems that your plot is jagged not because of the curve itself, but because of how your screen is displayed.

@Hemmo suggested fixing this solution using a vector graphics format instead of a raster format. This is the best solution, but if you desperately need to use a raster format, you can use anti-aliasing.

Smoothing means that the plot is drawn with gray fluff around the lines, so they look like they are curved for the human eye. You will see this easily if you zoom in using anti-aliasing. Till:

 png("no-alias.png") # Your code dev.off() 

enter image description here

cairographics offers anti-aliasing. So use it as a png option:

 png("alias.png", type="cairo") # Your code again dev.off() 

enter image description here

+8
source

I'm not sure about your somthness problem (this is not clear in the plot you are showing), but you can improve it by doing cubic (or Hermite) spline interpolation of your points. Here are a few options using spline and splinefun .

enter image description here

 layout(matrix(c(1,2,3),nrow=3,byrow=TRUE)) plot(NA,xlim=c(0,1),ylim=c(0,0.2),xlab="delta",ylab="K", xaxs="i",yaxs="i", main='orginal plot with 45000 points') # Empty plot a1 <- curve((x+x^7-x^2-x^4)/(1+xx^3-x^4), from=0, n=45000, add = TRUE) x <- seq(0,1,length.out=1000) y <- (x+x^7-x^2-x^4)/(1+xx^3-x^4) f <- splinefun(x, y) plot(NA,xlim=c(0,1),ylim=c(0,0.2),xlab="delta",ylab="K", xaxs="i",yaxs="i", main='splinefun plot with 1000 points') curve(f(x),0, 1, col = "green", lwd = 1.5,add=TRUE) plot(NA,xlim=c(0,1),ylim=c(0,0.2),xlab="delta",ylab="K", xaxs="i",yaxs="i", main='spline plot with 1000 points') lines(spline(x,y), col = 2) 
+2
source

All Articles