[英]Understanding time series plot and ggplot2 in R
我正在嘗試在 R 中創建一個簡單的條形圖,它對按月分組的日期和金額進行分組。
雖然我對 R 有基本的了解,但我很難理解它為什么會這樣。
到目前為止,這是我的進步:
如您所見,我使用了兩個簡單的變量:amount_net 用於金額,而month_year_begindate 用於時間序列。 這是我的數據示例:
> head(df)
amount_net month_year_begindate
1 3306.11 2017-10-01
2 5471.28 2018-07-01
3 4584.86 2018-07-01
4 5923.76 2018-07-01
5 3830.59 2018-07-01
6 4035.04 2018-07-01
這是我用來創建圖表的代碼:
require(ggplot2)
require(scales)
g <- ggplot(data = df)
g + geom_bar(aes(x = month_year_begindate , y = amount_net), stat="identity",
colour = "dodgerblue3") +
scale_x_date(date_breaks = "1 month", labels = date_format("%B de %Y")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1))
但是,我無法理解兩個基本的事情:
limits = as.Date(c('2019-01-01', '2019-05-01'))
我會看到相同的行為(開始和結束的值顯示為空)。任何人都可以幫助我理解和解決這個問題?
在我看來,問題可能是由於條形width
過大造成的。 使用您的玩具數據查看此示例(請注意,我按日期對其進行了總結):
library(data.table)
dt[, month_year_begindate := lubridate::ymd(month_year_begindate)]
ggplot(dt[, .(amount_net = sum(amount_net)), by = month_year_begindate]) +
geom_bar(aes(x = month_year_begindate , y = amount_net),
stat="identity",
fill = "dodgerblue3",
width = 5) +
scale_x_date(date_breaks = "1 month", labels = date_format("%B de %Y")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.