# Smoother lines for ggplot2

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


• 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). – Frank49 Aug 2 '14 at 13:57
• 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. – Nick Stauner Aug 2 '14 at 21:45
• Possible duplicate of stackoverflow.com/questions/16789502/… – shadowtalker Aug 2 '14 at 22:22
• 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? – Glen_b Aug 3 '14 at 0:51

## 1 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")