3

How can I smoothen the chart line using ggplot2

I am trying to plot a graph for this function

exposure_funct_0.25 <- function(x) {
  exp(-0.25*x)
} 

exposure_funct_0.5 <- function(x) {
  exp(-0.5*x)
} 

 ggplot(data.frame(x = c(0,15)), aes(x=x)) + 
         geom_function( fun = exposure_funct_0.25, linetype = 2 )  + 
        geom_function(fun = exposure_funct_0.5, linetype = 1)

However, the code resulted in "broken lines" chart like this :

enter image description here

I tried to add +geom_smooth() but this one does not seem to work with my situation. Specifying n= 201 or higher does not help, unfortunately.

Is there any way to make the line smooth like this smoothened line?

  • 2
    What do you mean by 'broken lines'? – d.b Sep 13 '20 at 16:18
  • 2
    You can increase the number of points to interpolate along the x axis. The default is 101, try `n = 201`. – Rui Barradas Sep 13 '20 at 16:28
  • 3
    are you referring to the pixelation/steps in the lower curve for x > 10? It's going to depend a lot on the details (resolution, size etc.) of the image ... how did you save this image? I'm having trouble reproducing this. – Ben Bolker Sep 13 '20 at 16:33
  • Hi Ben, yes I am referring to the pixelation/steps in the lower curve. The issue is quite new to me and I do not know what caused it... How can I check the details you mentioned? – quachtoanlong Sep 14 '20 at 07:21
  • Hi Rui, I tried to increased the n but it does not work... I saw the quality of the image improved but the lags are still there. I increased the n to 9999 but it does not result in the smooth line like I need – quachtoanlong Sep 14 '20 at 07:36
  • 3
    Could be an anti-aliasing issue (if you are using RStudio in windows for example, see [here](https://github.com/rstudio/rstudio/issues/4067)). I outputted the same image on my machine to png & pdf. The former has the same pixelated line as what you have here, while the latter is perfectly smooth. – Z.Lin Sep 14 '20 at 11:43
  • Thank you Z.Lin. I tried to export as PDF and it is smooth. I need to put the chart to a publication, so I will need to figure out how to do that then. But at least now I have an option. – quachtoanlong Sep 14 '20 at 15:01

0 Answers0