在geom_text_repel中对齐标签

时间:2019-10-15 06:40:28

标签: r ggplot2 ggrepel

问题

我正尝试从ggrepel示例中复制文本对齐方式和间距,如hjust = 0面板中所示: enter image description here

我的情节

困难在于,我已将多个字符串连接到单个标签中,以使线段不会使绘图看起来过于混乱。这些较大的标签似乎不能很好地相互配合: enter image description here

数据

nest_data <- structure(list(rate = c(27, 25, 24, 23, 22, 21, 20, 19, 18, 17, 
16, 15, 10, 8, 7.7, 5), country_groups = c("Hungary", "Denmark\n Norway\n Sweden", 
"Finland\n Greece\n Iceland", "Ireland\n Poland\n Portugal", 
"Italy\n Slovenia", "Belgium\n Czech Republic\n Latvia\n Lithuania\n Netherlands\n Spain", 
"Austria\n Estonia\n France\n Slovak Republic\n United Kingdom", 
"Chile\n Germany", "Turkey", "Israel\n Luxembourg", "Mexico", 
"New Zealand", "Australia\n Korea", "Japan", "Switzerland", "Canada"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-16L))

我的尝试

(nest_data_vat_plot <- ggplot(nest_data, aes(y = rate, x = 1, label = country_groups)) +
    geom_point(color = "red") +
    theme(
      axis.line.x  = element_blank(),
      axis.ticks.x = element_blank(),
      axis.text.x  = element_blank(),
      axis.title.x = element_blank(),
      plot.title   = element_text(hjust = 0),
      panel.background = element_blank()
    ) +
    xlim(1, 1.1) +
    geom_text_repel(
      nudge_x      = 0.035,
      direction    = "y",
      hjust        = 0
    ) + 
    scale_y_continuous(breaks = seq(2.5, 30, 2.5)) + 
    labs(
      title = "Consumption tax rates",
      caption = "Source: OECD Tax Stats \n2019 data presented \nRef: oecd_vat_gst_rates.R",
      x = "",
      y = ""
    ))

0 个答案:

没有答案