【发布时间】:2020-05-03 11:58:26
【问题描述】:
我正在努力用 ggplot2 格式化堆积面积图的 x 轴。这是我的数据:
df <- data.frame(
Taxon = c("Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms", "Others", "Dinos", "Diatoms"),
Abundance = c(14192, 120, 440, 6000, 80, 360, 25800, 4384, 169428, 879103, 2000, 52360, 213508, 22560, 470900, 472808, 11920, 316312, 81504, 6280, 15096, 50656, 11360, 43448),
Date = c("05/01/2019", "05/01/2019", "05/01/2019", "09/03/2019", "09/03/2019", "09/03/2019", "11/04/2019", "11/04/2019", "11/04/2019", "01/05/2019", "01/05/2019", "01/05/2019", "01/06/2019", "01/06/2019", "01/06/2019", "01/07/2019", "01/07/2019", "01/07/2019", "01/08/2019", "01/08/2019", "01/08/2019", "01/09/2019","01/09/2019", "01/09/2019")
)
df %>% group_by(Date, Taxon) %>% summarise_all(sum) -> df1
ggplot(df1, aes(x=as.Date(Date, format="%d/%m/%Y"), Abundance, colour=Taxon, fill=Taxon)) + geom_area(stat="identity", position="stack") + labs(x = "", y = "") + scale_x_date(labels = date_format("%d/%m/%Y")) + theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
这给了我以下情节:
我现在的问题是:
- 如何让 x 轴标签与 df2$Date 中的日期而不是当月的第一天相对应?
- 我希望所有日期都在 x 轴上可见。我尝试使用
scale_x_continuous(labels=dates)dates <- unique(df1$Date)但这给了我一个错误。如何正确处理?
我将非常感谢任何提示!
【问题讨论】:
-
如果你想调整
date_breaks参数而不是date_labels参数的间隔(即显示标签的值) -
这能回答你的问题吗? R: ggplot display all dates on x axis
-
感谢您的帮助!乔纳森提供了我需要的东西。