Solved – Smoother lines for ggplot2

data visualizationggplot2rsmoothing

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 = 
                  "ar_year;t1_all_estimate;t2_all_estimate
                  ar2001-2002;208.0960242;170.924898
                  ar2003-2004;217.48718;151.6087781
                  ar2005-2006;205.9649097;141.4196023
                  ar2007-2008;212.2112923;135.2787361
                  ar2009-2010;205.628018;136.4582058
                  ar2011-2012;166.5654204;146.9776943")

require(ggplot2)

  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),
         axis.text=element_text(size=12),
         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

Best Answer

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