Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot smoother curves in R

Tags:

plot

r

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.

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

like image 699
Ashni Goyal Avatar asked Mar 16 '13 09:03

Ashni Goyal


People also ask

How do you add a smooth line to a scatter plot in R?

A scatter plot can be created using the function plot(x, y). The function lm() will be used to fit linear models between y and x. A regression line will be added on the plot using the function abline(), which takes the output of lm() as an argument. You can also add a smoothing line using the function loess().

How do you smooth out a scatter plot?

Scatterplots may be smoothed by fitting a line to the data points in a diagram. This line attempts to display the non-random component of the association between the variables in a 2D scatter plot.

What is a smooth curve in graphing?

A smooth curve is a curve which is a smooth function, where the word "curve" is interpreted in the analytic geometry context. In particular, a smooth curve is a continuous map from a one-dimensional space to an. -dimensional space which on its domain has continuous derivatives up to a desired order.


2 Answers

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

enter image description here

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

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

enter image description here

like image 86
sebastian-c Avatar answered Sep 17 '22 05:09

sebastian-c


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

like image 22
Jouni Helske Avatar answered Sep 18 '22 05:09

Jouni Helske