简体   繁体   English

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

[英]How to plot smoother curves in R

Using R , I have drawn a hatched plot. 使用R ,我绘制了一个阴影图。 If you see the curves, they are not smooth. 如果看到曲线,它们就不平滑了。 How to make them smooth ? 如何让它们流畅? Even excel plots far more smoother curves. 即使是excel也可以绘制更平滑的曲线。 Device features: windows 7, screen resolution=1366 x 768 (max) 设备功能:Windows 7,屏幕分辨率= 1366 x 768(最大)

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 完整代码可在此处获得

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. @Hemmo建议使用矢量图形格式而不是光栅格式来修复此解决方案。 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")
# Your code
dev.off()

在此输入图像描述

cairographics offers anti-aliasing. cairographics提供抗锯齿功能。 So using it as an option to png : 所以用它作为png一个选项:

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

在此输入图像描述

I'm fairly certain that if you plot your figure for example as pdf, the curves are completely smooth. 我相当肯定,如果你将你的数字绘制为例如pdf,曲线是完全平滑的。 It's the Rgui's internal display which shows the curve as non-smooth, and using copy paste on that might cause problems. 这是Rgui的内部显示,它将曲线显示为不平滑,并且使用复制粘贴可能会导致问题。 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. 现在看一下pdf,它看起来完全没问题。 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. 如果你想要例如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). (注意,这里使用的设备等术语可能不完全正确,我对这个术语并不完全熟悉,如果合适的话,更聪明的人可以编辑)。

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. 我不确定你的问题是什么(在你所展示的情节中不清楚),但你可以通过对你的点进行三次(或Hermite)样条插值来改善。 Here some options using spline and splinefun . 这里有一些使用splinesplinefun选项。

在此输入图像描述

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM