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のバーは、他の日付にも適用されます。この警告メッセージが表示されます:

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

他の日付にクロスオーバーするのではなく、ビンを所属する日付に配置する方法はありますか?

4

1 に答える 1

2

プロット用のコードを簡略化しました。各geomにデータフレーム名、xおよびy値を書き込む必要はありません。

バーの幅を変更するには、引数width=ingeom_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')))

データフレームに含まれる値が2つしかないため、エラーメッセージが表示されます(2つの値だけで回帰を行うことはできません)。値が実際に2つしかない場合は、

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

geom_line(size=2, color="red")
于 2013-01-04T19:37:31.730 に答える