ggplot中的自适应注释-使用形状和文本发生变化的图形注释

时间:2018-07-25 23:46:35

标签: r ggplot2

我正在尝试在ggplot中绘制以下图表。

Graph

我遇到了两个问题

  1. 如何为图形添加一些对象(例如,括号符号“ {”),并使其缩放或对指定的数量做出响应?
  2. 如何在单个填充线周围添加边框?我希望“赤字”为白色并带有黑色虚线边框。

以下是数据的结构方式:

> 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)

Gap analysis from ggplot

如果您对如何添加直接标签也不太复杂的话,我也很好奇。请注意,标签名称略有不同。

0 个答案:

没有答案