我正在尝试在ggplot中绘制以下图表。
我遇到了两个问题
以下是数据的结构方式:
> gap_analysis_plot_data
income_range control total variable value
1 0% to 30% MFI Households 29590 matched_units_to_hh 7525
2 0% to 30% MFI Units 10665 matched_units_to_hh 7525
3 31% to 50% MFI Households 19430 matched_units_to_hh 3255
4 31% to 50% MFI Units 15440 matched_units_to_hh 3255
5 0% to 30% MFI Households 29590 paying_too_much 22065
6 0% to 30% MFI Units 10665 paying_too_much 3095
7 31% to 50% MFI Households 19430 paying_too_much 14980
8 31% to 50% MFI Units 15440 paying_too_much 5910
9 0% to 30% MFI Households 29590 vacant_units 0
10 0% to 30% MFI Units 10665 vacant_units 45
11 31% to 50% MFI Households 19430 vacant_units 0
12 31% to 50% MFI Units 15440 vacant_units 690
13 0% to 30% MFI Households 29590 getting_a_deal 0
14 0% to 30% MFI Units 10665 getting_a_deal 0
15 31% to 50% MFI Households 19430 getting_a_deal 1195
16 31% to 50% MFI Units 15440 getting_a_deal 5585
17 0% to 30% MFI Households 29590 existing_gap 0
18 0% to 30% MFI Units 10665 existing_gap 18925
19 31% to 50% MFI Households 19430 existing_gap 0
20 31% to 50% MFI Units 15440 existing_gap 3990
这是我到目前为止生成以下图形的代码。
p <- gap_analysis_plot_data %>% ggplot(aes(x = control, y = value, fill = variable)) +
geom_col() +
facet_wrap(~income_range) +
labs(title = 'Affordability gap', subtitle = gtitle, x = "", y = "Number of households or units\n") +
scale_y_continuous(label = comma) +
scale_fill_manual(values = group.colors,
name = "",
breaks = c('existing_gap', 'getting_a_deal', 'vacant_units', 'paying_too_much', 'matched_units_to_hh'),
labels = c('Existing gap', 'Renting down\n(getting a deal)', 'Vacant units', 'Renting up\n(paying too much)', 'Matched units')
)
> print(p)
如果您对如何添加直接标签也不太复杂的话,我也很好奇。请注意,标签名称略有不同。