 <p>The images of your charts helped a lot in understanding what you trying to do. Using ddply with summarize from the plyr package does the same calculation as tapply but returns the result in a data frame that ggplot can use directly. Given that different data is used in the two examples, the code below seems to reproduce your chart in R:</p> <pre><code> library(plyr) df3 &lt;- ddply(df2,.(sex, smoker), summarize, BMI_mean=mean(BMI)) ggplot(df3,aes(as.numeric(smoker), BMI_mean, color=sex)) + geom_line() + scale_x_discrete("Current Sig Smoker Y/N", labels=levels(df3$smoker)) + labs(y="Mean Body Mass Index (kg/(M*M)", color="SEX") </code></pre> <p><img src="https://i.stack.imgur.com/JPg2b.png" alt="enter image description here"></p>