To format the comma, you need to enable the scales library for label=comma . The “error” you were discussing is actually just a warning because you used both ylim and scale_y_continuous . The second call cancels the first. Instead, you can set restrictions and specify labels separated by commas in one call to scale_y_continuous :
library(scales) ggplot(df, aes(x = Date, y = Cost))+ geom_line(lwd = 0.5) + geom_line(aes(y = Cost_7), col = 'red', linetype = 3, lwd = 1) + geom_line(aes(y = Cost_30), col = 'blue', linetype = 5, lwd = 0.75) + xlim(c(left, right)) + xlab("") + scale_y_continuous(label=comma, limits=c(min(df$Cost[df$Date > left]), max(df$Cost[df$Date > left])))
Another option would be to melt your data into a long format before the graphics, which will reduce the amount of code needed and simplify aesthetic comparisons:
library(reshape2) ggplot(melt(df, id.var="Date"), aes(x = Date, y = value, color=variable, linetype=variable))+ geom_line() + xlim(c(left, right)) + labs(x="", y="Cost") + scale_y_continuous(label=comma, limits=c(min(df$Cost[df$Date > left]), max(df$Cost[df$Date > left])))
In any case, to put y values in thousands or millions, you can divide y values by 1,000 or 1,000,000. I used dollar_format() below, but I think you will also need to divide by 10 if you use unit_format (per @joran suggestion). For instance:
div=1000 ggplot(melt(df, id.var="Date"), aes(x = Date, y = value/div, color=variable, linetype=variable))+ geom_line() + xlim(c(left, right)) + labs(x="", y="Cost (Thousands)") + scale_y_continuous(label=dollar_format(), limits=c(min(df$Cost[df$Date > left]), max(df$Cost[df$Date > left]))/div)
Use scale_color_manual and scale_linetype_manual to set custom colors and line types, if necessary.
