-->

## How to plot smoother curves in R

2020-08-27 01:09发布

# 问题:

Using R , I have drawn a hatched plot. If you see the curves, they are not smooth. How to make them smooth ? Even excel plots far more smoother curves. Device features: windows 7, screen resolution=1366 x 768 (max)

Here is the plot.

Following code is used to draw the plot.

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+x-x^3-x^4), from=0, n=450000, add = TRUE) # First curve

Full code is available here

# 回答1:

I'm fairly certain that if you plot your figure for example as pdf, the curves are completely smooth. It's the Rgui's internal display which shows the curve as non-smooth, and using copy paste on that might cause problems. It's better to directly plot into the file, like this:

# 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+x-x^3-x^4), from=0, n=450000, add = TRUE)
dev.off() #close device

Now look at the pdf, and it looks completely fine. If you want for example jpg image, use function jpeg etc, see ?jpeg for more details how to save as tiff, jpeg, png or bmp, and arguments for image size, resolution and such.

(note, the terms device etc used here might not be totally correct, I'm not completely familiar with this terminology, someone more clever can edit if appropriate).

# 回答2:

At the moment, it looks like your plot is jagged not because of the curve itself, but because of how your screen displays it.

@Hemmo proposed to fix this solution by 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.

Anti-aliasing means that the plot is drawn with some grey fuzz around the lines so they look like they're curved to the human eye. You'll see this easily if you zoom in on an anti-aliased image. For now:

png("no-alias.png")
dev.off()

cairographics offers anti-aliasing. So using it as an option to png:

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

# 回答3:

I am not sure of your problem somthness(it is not clear in the plot you show), but you can ameliorate by doing cubic (or Hermite) spline interpolation of your points. Here some options using spline and splinefun.

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+x-x^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+x-x^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)

Ta的文章 更多文章

0条评论