I wanted the bar chart to be a combination of stacked bars for the individual meals, and then a smoothed trendline of the total carbs for the day. The original data has columns for date, each meal, and a final total. Using "subset" to control the columns, the following R code
library(ggplot2)
library(plyr)
library(reshape2)
df <- melt( data, "Date")
df$Date <- as.Date(df$Date, "%Y-%m-%d ")
colnames( df) <- c( "Date", "Meal", "carbs")
plot <- ggplot( data=subset( df, Meal %in% c("Breakfast", "Lunch", "Snack1", "Snack2", "Dinner")), aes(x=Date, y=carbs, fill=Meal)) + geom_bar( stat="identity") + geom_smooth( data=subset( df, Meal %in% c("Total")), n=10) + scale_fill_brewer(palette="Set1")
The result is:
I had to do some reading to get this right, since googling found no examples to copy.
Comments