2013-01-04 86 views
1
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'))) 

ggplot中的酒吧也超过了其他日期。我得到这个警告信息:ggplot酒吧正越过其他不属于的日期

Warning message: 
In qt((1 - level)/2, df) : NaNs produced 

有没有办法将bin放到所属日期,而不是交叉到其他日期?

回答

2

简化您的绘图代码。不需要在每个geom中写入数据框名称,x和y值。

要更改条宽,可以使用参数width=geom_bar()

ggplot(df, aes(Month, Value)) + 
    geom_bar(fill="orange",stat="identity",width=15)+ 
    geom_smooth(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'))) 

由于您的数据框只包含两个值(不能仅使用两个值进行回归),您会收到错误消息。如果真的有只有两个值,然后用

geom_line(size=2, color="red") 
+0

非常感谢你 – user1471980

+0

我想一个解决方案,将是有条件的将是巨大的替代

geom_smooth(method="lm", size=2, color="red") 

。 –