dput(df)
structure(list(Month = structure(c(15248, 15522), class = "Date"),
Value = c(1, 3)), .Names = c("Month", "Value"), row.names = 1:2, class = "data.frame")
ggplot(df, aes(Month, Value)) +
geom_bar(fill = "orange", size = .3, stat = "identity", position = "identity") +
geom_smooth(data = df, aes(Month, Value, group = 1), method = "lm",
size = 2, color = "red") +
scale_x_date(breaks = "1 month", labels = date_format("%b-%Y"),
limits = as.Date(c('2011-01-01','2013-01-01')))
Bars in the ggplot goes over the other dates as well. I do get this warning message:
Warning message:
In qt((1 - level)/2, df) : NaNs produced
Is there a way to place the bin to the belonging date, rather than crossing over to other dates?
Simplified your code for plot. There is no need to write data frame name, x and y values in each geom.
To change bar width, argument
width=ingeom_bar()can be used.You get error message because your data frame contains only two values (you can’t do regression with just two values). If there really are only two values, then replace
with