我正在努力实现的目标
我有两个数据框,希望将它们重叠/叠加在组变量上。变量按性别和年龄段分组,每个数据帧中都有计数。我仅使用tidyverse
和ggplot2
。
ae_att_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label =
c("Female", "Male", "Not Specified"), class = "factor"),
AgeBand = structure(1:10, .Label = c("0 yrs", "1-4 yrs", "10-14
yrs", "15- 19 yrs", "20-24 yrs", "25-
29 yrs", "30-34 yrs", "35-39 yrs", "40-44 yrs",
"45-49 yrs", "5-9 yrs", "50-54 yrs", "55-59 yrs",
"60-64 yrs", "65-69 yrs", "70-74 yrs", "75-79 yrs",
"80-84 yrs", "85+ yrs"), class = "factor"), N =
c(4708L, 7065L, 1914L, 2292L, 4612L, 5968L, 5620L,
4007L, 2802L, 2429L)), row.names = c(NA, -10L), class
= c("grouped_df", "tbl_df", "tbl", "data.frame"), vars =
"Gender", drop = TRUE, indices = list(0:9), group_sizes = 10L,
biggest_group_size
= 10L, labels = structure(list(
Gender = structure(1L, .Label = c("Female", "Male", "Not Specified"),
class = "factor")), row.names = c(NA, -1L), class =
"data.frame", vars = "Gender", drop = TRUE))
ae_adm_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label =
c("Female", "Male", "Not Specified"), class
= "factor"),
AgeBand = structure(1:10, .Label = c("0 yrs", "1-4 yrs",
"10-14 yrs", "15-19 yrs", "20-24 yrs", "25-29 yrs",
"30-34 yrs", "35-39 yrs", "40-44 yrs", "45-49 yrs",
"5-9 yrs", "50-54 yrs", "55-59 yrs", "60-64 yrs", "65-
69 yrs", "70-74 yrs", "75-79 yrs", "80-84 yrs", "85+
yrs"), class = "factor"), N = c(4352L, 6229L, 2145L,
2328L, 3963L, 4769L, 4475L, 3342L, 2544L, 2141L)),
row.names = c(NA, -10L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"),
vars = "Gender", drop = TRUE, indices = list(0:9),
group_sizes = 10L, biggest_group_size = 10L, labels =
structure(list(Gender = structure(1L, .Label = c("Female",
"Male", "Not Specified"),
class = "factor")), row.names = c(NA, -1L), class =
"data.frame", vars = "Gender", drop = TRUE))
我遇到的问题
当我尝试使用'ggplot2'进行绘制时,它抛出 pretty_breaks 的错误。
pretty_breaks(15)中的错误:找不到函数“ pretty_breaks”。
这是我尝试过的代码:
ggplot(NULL, aes(x = AgeBand, y = N)) +
geom_bar(data = ae_att_df, stat = 'identity', position = 'dodge', aes(fill =
Gender))+
geom_bar(data = ae_adm_df, stat = 'identity',position =position_dodge(0.9),
width = 0.6, aes(fill = Gender)) + xlab("Age
Group") +
ylab("ED Attendances and Admissions") + ggtitle(paste("Unscheduled ED
Attendance and
Admission")) +
theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
scale_y_continuous(breaks = pretty_breaks(15)) +
scale_fill_manual("legend", values = c("Female Admissions" = "coral3",
"Female Attendances" = "lightcoral",
"Male Admissions" = "steelblue4",
"Male Attendances" = "lightblue2"))
我的数据框的生动化应如下所示:
但是,我已经尝试了比例尺库,但是得到了一个空图表。
答案 0 :(得分:1)
函数pretty_breaks
位于scales
包中。
所以你可以尝试
scale_y_continuous(breaks = scales::pretty_breaks(15))
或使用
开头加载整个程序包library(scales)
答案 1 :(得分:1)
首先,我们需要一些代表性的示例数据:
{{ range .post }}
{{ // how can i get index number? }}
{{ .Id }}
{{ .Name}}
{{ end}}
注意到数据set.seed(123)
ae_adm_df <- data.frame("Gender" = rep(c("Female Admitted", "Male Admitted"), each = 4),
"AgeBand" = rep(c("0 yrs", "1-4yrs", "10-14yrs", "15-19yrs"), 2),
"N" = sample(2000:8000, 8))
# Gender AgeBand N
# 1 Female Admitted 0 yrs 3725
# 2 Female Admitted 1-4yrs 6729
# 3 Female Admitted 10-14yrs 4453
# 4 Female Admitted 15-19yrs 7296
# 5 Male Admitted 0 yrs 7639
# 6 Male Admitted 1-4yrs 2273
# 7 Male Admitted 10-14yrs 5165
# 8 Male Admitted 15-19yrs 7349
ae_att_df <- data.frame("Gender" = rep(c("Female Not Admitted", "Male Not Admitted"), each = 4),
"AgeBand" = rep(c("0yrs", "1-4yrs", "10-14yrs", "15-19yrs"), 2),
"N" = sample(1000:3000, 8))
# Gender AgeBand N
# 1 Female Not Admitted 0yrs 2103
# 2 Female Not Admitted 1-4yrs 1913
# 3 Female Not Admitted 10-14yrs 2912
# 4 Female Not Admitted 15-19yrs 1905
# 5 Male Not Admitted 0yrs 2353
# 6 Male Not Admitted 1-4yrs 2142
# 7 Male Not Admitted 10-14yrs 1205
# 8 Male Not Admitted 15-19yrs 2794
中Gender
的级别与ae_adm_df
中Gender
的级别不同。这很重要。
现在我们可以绘图。不要在ae_att_df
中包含aes()
,因为没有提供ggplot()
。您需要同时为两个data
提供aes(x = AgeBand, fill = Gender, y = N)
。
geom_bar()