我正在尝试绘制我拥有的两个数据框,但没有得到条形图。
这是我拥有的数据帧的示例:
ae_attendances_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male", "Not Specified"
), class = "factor"), AgeBand = structure(c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .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"), Attend = c(4708L,
7065L, 1914L, 2292L, 4612L, 4968L, 5620L, 4007L, 2802L, 2429L,
2652L, 1908L, 1474L, 1414L, 1462L, 1388L, 1468L, 1586L, 4149L,
6720L, 10781L, 2911L, 1492L, 2666L, 3565L, 3163L, 2880L, 3007L,
4000L, 5167L), Attendances_Gender = c("Female_not_admitted",
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted",
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted",
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted",
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted",
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted",
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted",
"Male_not_admitted", "Male_not_admitted", "Male_not_admitted",
"Male_not_admitted", "Male_not_admitted", "Male_not_admitted",
"Male_not_admitted", "Male_not_admitted", "Male_not_admitted",
"Male_not_admitted", "Male_not_admitted")), row.names = c(NA,
-30L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Gender", drop = TRUE, indices = list(
0:18, 19:29), group_sizes = c(19L, 11L), biggest_group_size = 19L, labels = structure(list(
Gender = structure(1:2, .Label = c("Female", "Male", "Not Specified"
), class = "factor")), row.names = c(NA, -2L), class = "data.frame", vars = "Gender", drop = TRUE))
第二个数据帧:
ae_admissions_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male", "Not Specified"
), class = "factor"), AgeBand = structure(c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .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"), Admitted = c(4352L,
5229L, 1145L, 1328L, 2963L, 3769L, 3475L, 2342L, 1544L, 1141L,
1856L, 1022L, 1072L, 1005L, 1062L, 1000L, 801L, 736L, 1030L,
4727L, 8072L, 1947L, 1002L, 1615L, 2231L, 1883L, 1653L, 1602L,
1572L, 3054L), Admissions_Gender = c("Female_admitted", "Female_admitted",
"Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted",
"Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted",
"Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted",
"Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted",
"Female_admitted", "Male_admitted", "Male_admitted", "Male_admitted",
"Male_admitted", "Male_admitted", "Male_admitted", "Male_admitted",
"Male_admitted", "Male_admitted", "Male_admitted", "Male_admitted"
)), row.names = c(NA, -30L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = "Gender", drop = TRUE, indices = list(
0:18, 19:29), group_sizes = c(19L, 11L), biggest_group_size = 19L, labels = structure(list(
Gender = structure(1:2, .Label = c("Female", "Male", "Not Specified"
), class = "factor")), row.names = c(NA, -2L), class = "data.frame", vars = "Gender", drop = TRUE))
这是我的代码:
ggplot() +
geom_bar(data = ae_attendances_df, aes(x = AgeBand, fill = Attendances_Gender, y = Attend),
stat = 'identity', position = 'dodge') +
geom_bar(data = ae_admissions_df, aes(x = AgeBand, fill = Admissions_Gender, y = Admitted),
stat = 'identity', position = position_dodge(0.9), width = 0.6) +
xlab("Age Group") +
ylab("ED Attendances and Admissions") +
ggtitle("Unscheduled ED Attendance and Admission") +
theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
scale_y_continuous(breaks = scales::pretty_breaks(15)) +
scale_fill_manual("", values = c("Female Admitted" = "coral3",
"Female Not Admitted" = "lightcoral",
"Male Admitted" = "steelblue4",
"Male Not Admitted" = "lightblue2"))
我得到的不是我想要的,也无法获得所需的东西:
但这是我想要得到的,显然有些东西行不通:
答案 0 :(得分:1)
我认为问题出在您使用scale_fill_manual()
时,请尝试以下操作:
ggplot() +
geom_bar(data = ae_attendances_df, aes(x = AgeBand, fill = Attendances_Gender, y = Attend),
stat = 'identity', position = 'dodge') +
geom_bar(data = ae_admissions_df, aes(x = AgeBand, fill = Admissions_Gender, y = Admitted),
stat = 'identity', position = position_dodge(0.9), width = 0.6) +
xlab("Age Group") +
ylab("ED Attendances and Admissions") +
ggtitle("Unscheduled ED Attendance and Admission") +
theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
scale_y_continuous(breaks = scales::pretty_breaks(15)) +
scale_fill_manual(values = c("coral3", "lightcoral"))
也就是说,您无需为scale_fill
提供名称,它将根据组的顺序自动检测到该名称。