以下是公司按两个群组划分的净收入的一些示例数据:
data <- data.frame(dates = rep(seq(as.Date("2000/1/1"), by = "month", length.out = 48), each = 2),
revenue = rep(seq(10000, by = 1000, length.out = 48), each = 2) * rnorm(96, mean = 1, sd = 0.1),
cohort = c("Group 1", "Group 2"))
Run Code Online (Sandbox Code Playgroud)
我可以显示一年的数据,它会返回我所期望的:
start = "2000-01-01"
end = "2000-12-01"
ggplot(data, aes(fill = cohort, x = dates, y = revenue)) +
geom_bar(stat = "identity", position = position_dodge(width = NULL)) +
xlab("Month") +
ylab("Net Revenue") +
geom_text(aes(label = round(revenue, 0)), vjust = -0.5, size = 3, position = position_dodge(width = 25)) +
scale_x_date(date_breaks = "1 month", limits = as.Date(c(start, end))) +
ggtitle("Monthly Revenue by Group") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 60, hjust = 1), plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values=c("#00BFC4", "#F8766D"))
Run Code Online (Sandbox Code Playgroud)
但是如果我将日期范围扩大到两年或更长时间并重新运行图表,它会在 x 轴的两侧显示额外的月份,尽管在 y 轴上没有显示任何信息。
start = "2000-01-01"
end = "2001-12-01"
#rerun the ggplot code from above
Run Code Online (Sandbox Code Playgroud)
请注意 1999-12-01 和 2002-01-01 不存在的数据点。为什么会出现这些,我该如何删除它们?
许多(全部?)scale_*函数都expand=作为参数。在 R 图中(包括 base 和ggplot2),将轴稍微扩展一点(我相信每端 4%)是很常见的,我认为这样没有一条线/点被挤压到“框”边界上。
如果你包含expand=c(0,0),你会得到你想要的。
(顺便说一句:你的括号不匹配。在这里修正。)
ggplot(data, aes(fill = cohort, x = dates, y = revenue)) +
geom_bar(stat = "identity", position = position_dodge(width = NULL)) +
xlab("Month") +
ylab("Net Revenue") +
geom_text(aes(label = round(revenue, 0)), vjust = -0.5, size = 3, position = position_dodge(width = 25)) +
scale_x_date(date_breaks = "1 month", limits = as.Date(c(start, end)), expand = c(0, 0)) +
ggtitle("Monthly Revenue by Group") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 60, hjust = 1), plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values=c("#00BFC4", "#F8766D"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
375 次 |
| 最近记录: |