根据季度时间序列绘制堆积条形图
Plotting a stacked bar chart for according to a quarterly time series
我在 6 年的时间里一直在处理抗生素使用数据,并设法绘制了一个条形图,其中包含每年每个季度的抗生素使用总量。但是,我现在需要做同样的事情,但每个季度使用的每种抗生素 class 的总数。我想通过制作堆积条形图来做到这一点(每个四分之一条形图将显示该季度使用的每个 class 的 x 数量)。
数据集中的一些示例数据是;
Date Antibiotic.Total Total.Cephalosporin Total.AminoglycosideTotal.B.Lactam
1 14/05/2013 0.60 0.60 0.00 0.00
2 03/07/2013 1.20 1.20 0.00 0.00
3 02/08/2013 4.62 0.00 2.82 1.80
4 02/08/2013 0.60 0.60 0.00 0.00
5 20/08/2013 1.20 1.20 0.00 0.00
6 14/09/2013 1.20 1.20 0.00 0.00
7 21/10/2013 0.90 0.90 0.00 0.00
8 14/11/2013 0.60 0.60 0.00 0.00
9 14/11/2013 2.25 2.25 0.00 0.00
10 01/05/2014 0.60 0.60 0.00 0.00
11 29/08/2014 0.50 0.50 0.00 0.00
12 11/09/2014 6.00 4.00 2.00 0.00
13 11/09/2014 1.20 0.80 0.40 0.00
14 11/09/2014 0.60 0.60 0.00 0.00
15 02/10/2014 20.00 0.00 0.00 0.00
16 20/10/2014 2.58 0.00 1.98 0.00
17 20/10/2014 5.00 5.00 0.00 0.00
18 02/04/2015 1.20 1.20 0.00 0.00
19 24/04/2015 31.50 0.00 17.50 14.00
20 29/04/2015 0.45 0.45 0.00 0.00
21 30/04/2015 31.50 0.00 17.50 14.00
22 03/05/2015 18.00 0.00 10.00 8.00
23 03/05/2015 45.00 0.00 25.00 20.00
我已经能够使用以下示例代码绘制每季度的抗生素总量;
datapractice %>%
mutate(Q = lubridate::quarter(datepractice, with_year = T)) %>%
group_by(Q) %>%
summarize(Antibiotic_by_Q = sum(Antibiotic.Total)) %>%
ggplot(aes(Q, Antibiotic_by_Q)) + geom_bar(stat = "identity", fill =
"steelblue") + scale_x_yearqtr(format = "%Y") + ylab("Antibiotic Total
(Grams)") + xlab("Date (Quarters/Year)") +
labs(title = "Antibiotic Use Over a 6 Year Period"
我试图通过更改以 summarize( 开头的代码行来更改上述代码,以显示以下内容
`summarize(Antibiotics_by_Q = sum(Total.Cephalosporin,
Total.Aminoglycoside, Total.B.Lactam)) %>%`
唉...这没用:(
p.s。有关季度抗生素总量图,请参见附图...
任何帮助尝试格式化我已经制作的条形图以将数据显示为堆叠条形图的任何帮助将不胜感激! :)
我不确定以下是问题的要求,但诀窍是 summarise_if
列是数字,然后是 reshape from wide to long format。
我删除了自定义颜色,因为现在有 4 种颜色。
library(tidyverse)
library(zoo)
library(lubridate)
datapractice %>%
mutate(Date = dmy(Date),
Q = quarter(Date, with_year = TRUE)) %>%
group_by(Q) %>%
summarise_if(is.numeric, sum, na.rm = TRUE) %>%
gather(Key, Total, -Q) %>%
ggplot(aes(Q, Total, fill = Key)) +
geom_bar(stat = "identity") +
scale_x_yearqtr(format = "%Y") +
ylab("Antibiotic Total (Grams)") +
xlab("Date (Quarters/Year)") +
labs(title = "Antibiotic Use Over a 6 Year Period")
我在 6 年的时间里一直在处理抗生素使用数据,并设法绘制了一个条形图,其中包含每年每个季度的抗生素使用总量。但是,我现在需要做同样的事情,但每个季度使用的每种抗生素 class 的总数。我想通过制作堆积条形图来做到这一点(每个四分之一条形图将显示该季度使用的每个 class 的 x 数量)。
数据集中的一些示例数据是;
Date Antibiotic.Total Total.Cephalosporin Total.AminoglycosideTotal.B.Lactam
1 14/05/2013 0.60 0.60 0.00 0.00
2 03/07/2013 1.20 1.20 0.00 0.00
3 02/08/2013 4.62 0.00 2.82 1.80
4 02/08/2013 0.60 0.60 0.00 0.00
5 20/08/2013 1.20 1.20 0.00 0.00
6 14/09/2013 1.20 1.20 0.00 0.00
7 21/10/2013 0.90 0.90 0.00 0.00
8 14/11/2013 0.60 0.60 0.00 0.00
9 14/11/2013 2.25 2.25 0.00 0.00
10 01/05/2014 0.60 0.60 0.00 0.00
11 29/08/2014 0.50 0.50 0.00 0.00
12 11/09/2014 6.00 4.00 2.00 0.00
13 11/09/2014 1.20 0.80 0.40 0.00
14 11/09/2014 0.60 0.60 0.00 0.00
15 02/10/2014 20.00 0.00 0.00 0.00
16 20/10/2014 2.58 0.00 1.98 0.00
17 20/10/2014 5.00 5.00 0.00 0.00
18 02/04/2015 1.20 1.20 0.00 0.00
19 24/04/2015 31.50 0.00 17.50 14.00
20 29/04/2015 0.45 0.45 0.00 0.00
21 30/04/2015 31.50 0.00 17.50 14.00
22 03/05/2015 18.00 0.00 10.00 8.00
23 03/05/2015 45.00 0.00 25.00 20.00
我已经能够使用以下示例代码绘制每季度的抗生素总量;
datapractice %>%
mutate(Q = lubridate::quarter(datepractice, with_year = T)) %>%
group_by(Q) %>%
summarize(Antibiotic_by_Q = sum(Antibiotic.Total)) %>%
ggplot(aes(Q, Antibiotic_by_Q)) + geom_bar(stat = "identity", fill =
"steelblue") + scale_x_yearqtr(format = "%Y") + ylab("Antibiotic Total
(Grams)") + xlab("Date (Quarters/Year)") +
labs(title = "Antibiotic Use Over a 6 Year Period"
我试图通过更改以 summarize( 开头的代码行来更改上述代码,以显示以下内容
`summarize(Antibiotics_by_Q = sum(Total.Cephalosporin,
Total.Aminoglycoside, Total.B.Lactam)) %>%`
唉...这没用:(
p.s。有关季度抗生素总量图,请参见附图...
任何帮助尝试格式化我已经制作的条形图以将数据显示为堆叠条形图的任何帮助将不胜感激! :)
我不确定以下是问题的要求,但诀窍是 summarise_if
列是数字,然后是 reshape from wide to long format。
我删除了自定义颜色,因为现在有 4 种颜色。
library(tidyverse)
library(zoo)
library(lubridate)
datapractice %>%
mutate(Date = dmy(Date),
Q = quarter(Date, with_year = TRUE)) %>%
group_by(Q) %>%
summarise_if(is.numeric, sum, na.rm = TRUE) %>%
gather(Key, Total, -Q) %>%
ggplot(aes(Q, Total, fill = Key)) +
geom_bar(stat = "identity") +
scale_x_yearqtr(format = "%Y") +
ylab("Antibiotic Total (Grams)") +
xlab("Date (Quarters/Year)") +
labs(title = "Antibiotic Use Over a 6 Year Period")