如何使日期与 ggplot() 中的数据点对齐
所以我遇到了一个问题,当我在 scale_x_date(..., date_breaks = "days")
中指定时,我只能让数据点与 x 轴标签对齐,这歪曲了数据。有人可以指出为什么他们不对齐以及如何解决它吗?
tidy_sales$Week_End <- as.Date(tidy_sales$Week_End, origin = 2022-02-06)
g1 <- ggplot(tidy_sales, aes(Week_End, y = `Value($)`)) +
geom_line(aes(color = variable)) +
geom_point(aes(col = variable))
g1 + scale_x_date(date_labels="%b-%d", date_breaks = "week",
limits = c(min(tidy_sales$Week_End),
max = max(tidy_sales$Week_End))) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
dput(tidy_sales)
structure(list(Week_End = structure(c(19043, 19036, 19029, 19050,
19043, 19036, 19029, 19050, 19043, 19036, 19029, 19050), class = "Date"),
variable = c("Total_Expenses ($)", "Total_Expenses ($)",
"Total_Expenses ($)", "Total_Expenses ($)", "Total_Sales($)",
"Total_Sales($)", "Total_Sales($)", "Total_Sales($)", "Actual_Revenue ($)",
"Actual_Revenue ($)", "Actual_Revenue ($)", "Actual_Revenue ($)"
), `Value($)` = c(6020.94, 7233.68, 5153.67, 5086, 13847.58,
9928.14, 15053.49, 14111.2, 12588.71, 9025.58, 13684.99,
12828.36)), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
So I have ran into a problem where I can only get my data points to align with the x-axis labels when I specify in scale_x_date(..., date_breaks = "days")
which misrepresents the data. Could someone please give some indication why they aren't aligned and how to fix it?
tidy_sales$Week_End <- as.Date(tidy_sales$Week_End, origin = 2022-02-06)
g1 <- ggplot(tidy_sales, aes(Week_End, y = `Value($)`)) +
geom_line(aes(color = variable)) +
geom_point(aes(col = variable))
g1 + scale_x_date(date_labels="%b-%d", date_breaks = "week",
limits = c(min(tidy_sales$Week_End),
max = max(tidy_sales$Week_End))) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
dput(tidy_sales)
structure(list(Week_End = structure(c(19043, 19036, 19029, 19050,
19043, 19036, 19029, 19050, 19043, 19036, 19029, 19050), class = "Date"),
variable = c("Total_Expenses ($)", "Total_Expenses ($)",
"Total_Expenses ($)", "Total_Expenses ($)", "Total_Sales($)",
"Total_Sales($)", "Total_Sales($)", "Total_Sales($)", "Actual_Revenue ($)",
"Actual_Revenue ($)", "Actual_Revenue ($)", "Actual_Revenue ($)"
), `Value($)` = c(6020.94, 7233.68, 5153.67, 5086, 13847.58,
9928.14, 15053.49, 14111.2, 12588.71, 9025.58, 13684.99,
12828.36)), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论