绘制季度/年度的每日数据

时间:2019-09-19 10:40:45

标签: r date

我正在尝试绘制数年间抗生素使用模式的每日数据,但希望能够将这些每日数据以季度/年的形式绘制以压缩该图。我已经尝试了许多在这里找到的解决方案,但似乎无法弄清楚。我是R的新手,所以总是很感谢任何帮助!

我已经能够将数据集中(d / m / Y)中的日期转换为日期类,并针对每个日期针对抗生素的使用绘制日期。

我用来生成一段时间使用图的代码如下:

data001 <- read.csv("001.csv")
date001 <- as.Date(data001$Date, "%d/%m/%Y")

ggplot(data001, aes(date001, Antibiotic.Total)) + geom_bar(stat = "identity", colour = "steelblue3") + scale_x_date(date_breaks = "3 months", date_labels = "%m/%y") + ylab("Antibiotic Total (Grams)")

我正在使用的示例在这里;

1   13/04/2013  5.000
2   13/04/2013  0.000
3   10/05/2013  7.200
4   22/05/2013  5.000
5   22/05/2013  5.000
6   17/06/2013  7.200
7   17/06/2013  5.000
8   29/06/2013  5.000
9   29/06/2013  7.200
10  29/06/2013  2.250
11  05/08/2013  0.000
12  05/08/2013  5.000
13  24/09/2013  0.500
14  01/10/2013  7.200
15  04/10/2013  4.000
16  08/10/2013  0.500
17  11/10/2013  10.000
18  22/10/2013  0.500
19  29/10/2013  7.200
20  29/10/2013  5.000

我希望有条形图,每个条形显示我拥有的6年数据中每个季度使用的抗生素总量:)

3 个答案:

答案 0 :(得分:0)

将以下内容添加到您的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)")

这也是Chelmy88提到的基于zoo包,但带有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)")

答案 1 :(得分:0)

这是一种实现方法,请参见代码中的注释:

library(zoo) #install the package if needed, provide the function as.yearqtr

#Load the data, these are the example data you gave,

data=structure(list(V1 = 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"),
V2 = 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))

#Set column name
colnames(data)=c("date","antibiotic")

#Add a column containing the year and quarter
data$quarter=as.yearqtr(data$date, format = "%d/%m/%Y")

#Looks like:
head(data)
#        date antibiotic quarter
#1 13/04/2013        5.0 2013 Q2
#2 13/04/2013        0.0 2013 Q2
#3 10/05/2013        7.2 2013 Q2
#4 22/05/2013        5.0 2013 Q2
#5 22/05/2013        5.0 2013 Q2
#6 17/06/2013        7.2 2013 Q2

#Now sum data for each quarter (if you ant the mean use it in the FUN argement
aggregated_data=aggregate(data$antibiotic,by=list(data$quarter),FUN=sum)

#And now you have a nice output per quarter:
head(aggregated_data)
#  Group.1     x
#1 2013 Q2 48.85
#2 2013 Q3  5.50
#3 2013 Q4 34.40

现在您可以使用aggregated_data进行绘图了

答案 2 :(得分:0)

您可以使用tidyverse将四分之一的抗生素总数求和,然后作图。

library(tidyverse)
data001 %>%
  mutate(Q = lubridate::quarter(date001, with_year = T)) %>% # Add a column with the quarter
  group_by(Q) %>% # group by that column
  summarize(Antibiotic_by_Q = sum(Antibiotic.Total)) %>% # summarize by quarter by taking the mean
  ggplot(aes(Q, Antibiotic_by_Q)) + geom_bar(stat = "identity", fill = "steelblue3") + ylab("Antibiotic Total (Grams)") # Graph by the new variables, x is no longer a date so remove scale_x_date