Добавьте в свой ggplot следующее:
scale_x_date(date_breaks = "years")
это увеличит масштаб оси X до нескольких лет, делая ее более читабельной.
Если вы имели в виду, что выЕсли вы хотите «агрегировать» данные в квартальные / годовые суммы, вы могли бы сделать что-то вроде этого:
library('dplyr') # for piping, group_by, etc.
library('lubridate') # for working with dates
data %>%
mutate(Year = year(date) %>% # generates a new column with the Year
group_by(Year) %>%
summarise(antibiotic=sum(antibiotic, na.rm = TRUE) %>% # aggregates the values over a year
ggplot(aes(Year,Value)) + geom_bar(stat = "identity", colour = "steelblue3") +
ylab("Antibiotic Total (Grams)")
Вот также решение для квартальных данных, основанное на пакете zoo
, как упомянуто Chelmy88, но с dplyr
:
data=structure(list(date = structure(c(7L, 7L, 5L, 9L, 9L, 8L, 8L, 12L,
12L, 12L, 3L, 3L, 11L, 1L, 2L, 4L, 6L, 10L, 13L, 13L),.Label = c("01/10/2013", "04/10/2013", "05/08/2013", "08/10/2013", "10/05/2013", "11/10/2013",
"13/04/2013", "17/06/2013", "22/05/2013", "22/10/2013", "24/09/2013",
"29/06/2013", "29/10/2013"), class = "factor"),
antibiotic = c(5, 0, 7.2, 5, 5, 7.2, 5, 5, 7.2, 2.25, 0, 5, 0.5, 7.2, 4, 0.5, 10, 0.5, 7.2, 5)),class = "data.frame", row.names = c(NA, -20L))
library(dplyr) # for piping, group_by, etc.
library(zoo) # for working with dates
data %>%
mutate(quarter = as.yearqtr(as.character(date), format = "%d/%m/%Y")) %>% # generates a new column with the Year
group_by(quarter) %>%
summarise(antibiotic=sum(antibiotic, na.rm = TRUE)) %>% # aggregates the values over a year
ggplot(aes(quarter,antibiotic)) + geom_bar(stat = "identity", colour = "steelblue3") +
ylab("Antibiotic Total (Grams)")