This question probably has a simple solution, still the thing is I've written a code to plot mortality in 2 different groups and that is, death in obese patients vs not obese. Now their are 2 groups t1 and t2 (obese vs normal BMI). The graphs works just fine with this code, I'ts just that I wanted the lines to look smoother and not so jagged. I've tried stat_smooth but just cant get it to work.

Im guessing that the code is longer then necessary.

allmortality<- read.table(header=TRUE, sep=";", text = 


  ggplot(allmortality, aes(x=ar_year)) + 
  geom_point(aes(y = t2_all_estimate, size=4)) +
  geom_line(aes(y = t2_all_estimate, group=1)) + 
  geom_point(aes(y = t1_all_estimate, size=4)) +
  geom_line(aes(y = t1_all_estimate, group=1)) +
  theme_bw() +  
  theme( legend.position="none",
         axis.text.x = element_text(angle = 45, hjust = 1),
         plot.background = element_blank()
         ,panel.grid.major = element_blank()
         ,panel.grid.minor = element_blank()
         ,panel.border = element_blank()) +
  theme(axis.line = element_line(color = 'black')) +
  ylab("Mortality") +
  xlab("Year") +
  ggtitle("mortality rates in obese vs not obese patients") 

enter image description here

  • 2
    $\begingroup$ The aim is to create a smooth line that goes through the points. Since the connected dots fluctuate much from one period to another, it would be nice to show a moving average (For each group; control and treatment). Test and allmortality data are the same, treatment_estimate is t1(obese) and control_estimate is t2(normal BMI). $\endgroup$
    – Heala45
    Commented Aug 2, 2014 at 13:57
  • $\begingroup$ Is it particularly important that the lines go through those points? A smoothed line like @AndreSilva's might make a better predictive model for cases outside your sample. $\endgroup$ Commented Aug 2, 2014 at 21:45
  • $\begingroup$ Possible duplicate of stackoverflow.com/questions/16789502/… $\endgroup$ Commented Aug 2, 2014 at 22:22
  • $\begingroup$ When you say you've tried stat.smooth but can't get it to work, what have you tried and what happened when you did? $\endgroup$
    – Glen_b
    Commented Aug 3, 2014 at 0:51

1 Answer 1


If your entire data follows the pattern from your sample here you could try polynomial regression method with a second order (quadratic) polynomial function.

See the example below:

ggplot(allmortality,aes(x=ar_year)) + 
stat_smooth(aes(y = t1_all_estimate, group=1, colour="Obese"     ), method=lm, formula = y ~ poly(x,2), level=0.95) + #tweak the signifance level suitable for your study
stat_smooth(aes(y = t2_all_estimate, group=1, colour="Normal BMI"), method=lm, formula = y ~ poly(x,2), level=0.95) +
geom_point (aes(y = t1_all_estimate, colour = "Obese"    ), size=4) +
geom_point (aes(y = t2_all_estimate, colour ="Normal BMI"), size=4) +
scale_colour_manual("Treatment", breaks = c("Obese", "Normal BMI"), values = c("blue","red")) +
theme_bw() +  
theme(axis.text.x = element_text(angle = 45, hjust = 1),
      axis.text = element_text(size=12), 
      axis.title.x = element_text(vjust=-0.1),
      axis.title.y = element_text(vjust=+1.1),
      plot.background = element_blank(),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      panel.border = element_blank()) +
theme(axis.line = element_line(color = 'black')) +
ylab("Mortality (nº of patients)") +
xlab("Year") +
ggtitle("Mortality in obese vs not obese patients")

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.