如何在R中绘制更平滑的曲线

时间:2023-02-09 17:41: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)

使用R,我绘制了一个阴影图。如果看到曲线,它们就不平滑了。如何让它们流畅?即使是excel也可以绘制更加平滑的曲线。设备功能:Windows 7,屏幕分辨率= 1366 x 768(最大)

Here is the plot.

这是情节。

如何在R中绘制更平滑的曲线

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

完整代码可在此处获得

3 个解决方案

#1


5  

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:

我相当肯定,如果你将你的数字绘制为例如pdf,曲线是完全平滑的。这是Rgui的内部显示,它将曲线显示为不平滑,并且使用复制粘贴可能会导致问题。最好直接绘制到文件中,如下所示:

# 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.

现在看一下pdf,它看起来完全没问题。如果你想要例如jpg图像,使用函数jpeg等,请参阅?jpeg以获取更多详细信息如何保存为tiff,jpeg,png或bmp,以及图像大小,分辨率等参数。

(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


8  

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.

@Hemmo建议使用矢量图形格式而不是光栅格式来修复此解决方案。这是最好的解决方案,但如果您迫切需要使用栅格格式,则可以使用消除锯齿功能。

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")
# Your code
dev.off()

如何在R中绘制更平滑的曲线

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

cairographics提供抗锯齿功能。所以使用它作为png的选项:

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

如何在R中绘制更平滑的曲线

#3


2  

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.

我不确定你的问题是什么(在你所展示的情节中不清楚),但你可以通过对你的点进行三次(或Hermite)样条插值来改善。这里有一些使用spline和splinefun的选项。

如何在R中绘制更平滑的曲线

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)

#1


5  

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:

我相当肯定,如果你将你的数字绘制为例如pdf,曲线是完全平滑的。这是Rgui的内部显示,它将曲线显示为不平滑,并且使用复制粘贴可能会导致问题。最好直接绘制到文件中,如下所示:

# 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.

现在看一下pdf,它看起来完全没问题。如果你想要例如jpg图像,使用函数jpeg等,请参阅?jpeg以获取更多详细信息如何保存为tiff,jpeg,png或bmp,以及图像大小,分辨率等参数。

(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


8  

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.

@Hemmo建议使用矢量图形格式而不是光栅格式来修复此解决方案。这是最好的解决方案,但如果您迫切需要使用栅格格式,则可以使用消除锯齿功能。

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")
# Your code
dev.off()

如何在R中绘制更平滑的曲线

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

cairographics提供抗锯齿功能。所以使用它作为png的选项:

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

如何在R中绘制更平滑的曲线

#3


2  

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.

我不确定你的问题是什么(在你所展示的情节中不清楚),但你可以通过对你的点进行三次(或Hermite)样条插值来改善。这里有一些使用spline和splinefun的选项。

如何在R中绘制更平滑的曲线

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)