【问题标题】:Ggplot with patterns and colors - legend带有图案和颜色的 Ggplot - 图例
【发布时间】:2022-06-22 20:26:59
【问题描述】:

我需要按国家和收入来源制作条形图。由于每个国家/地区都有 13 个收入组成部分,因此我使用颜色和图案来进行更清晰的区分。这里是数据的子样本:

 structure(list(country = c("Austria", "Austria", "Austria", "Austria", 
 "Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
 "Austria", "Austria", "Belgium", "Belgium", "Belgium", "Belgium", 
 "Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium", 
 "Belgium", "Belgium", "Germany"), `relative contributions` = c(1.12873709201813, 
  0.190666541457176, 0.0881932526826859, -0.0167537834495306, -0.0258312318474054, 
  0.215001374483109, -0.00148478976916522, -0.000572826422285289, 
  -0.0133617669343948, -0.00855048839002848, -0.00286591215990484, 
  -0.55317747592926, 1.43853068351746, 0.12194737046957, 0.0775384828448296, 
  -0.00403465563431382, -0.0196540541946888, -0.0183424148708582, 
  -0.0311897918581963, -0.00196828553453088, -0.00494809495285153, 
  -0.0225886180996895, -0.000150939886225387, -0.535139620304108, 
  1.23838818073273), component = c("Employee wages", "Self-employment", 
  "Capital", "Private transfers", "Unemployment", "Old-age + survivors", 
 "Sick + disability", "Education", "Social exclusion allowances", 
 "Family allowances", "Housing allowances", "Taxes", "Employee wages", 
 "Self-employment", "Capital", "Private transfers", "Unemployment", 
 "Old-age + survivors", "Sick + disability", "Education", "Social exclusion allowances", 
 "Family allowances", "Housing allowances", "Taxes", "Employee wages"
 )), row.names = c(NA, -25L), class = c("tbl_df", "tbl", "data.frame"
 ))

到目前为止,我能够制作以下情节:

 pat <- c('bricks', 'fishscales', 'verticalsaw', 'crosshatch', 'checkboard', 'octagons', 'verticalbrics', 'vertical', 'right45', 'hs_horizontal', 'horizontalsaw', 'hs_cross', 'hexagons')

ggplot(relative) + 
 geom_bar_pattern(aes(x=country, y=`relative contributions`, fill=factor(component), pattern_type=factor(component)), stat="identity", position="stack", width = 0.6, pattern='magick',  pattern_scale = 0.5, colour="black") + 
  scale_fill_manual(name="Income components", values = mycolors) +  scale_pattern_type_discrete(choices = pat, guide = 'none') + 
 scale_y_continuous(limits = c(-1,2), breaks = scales::pretty_breaks(n = 7)) + 
 theme_bw() +
 theme(axis.text.x = element_text(angle=45, hjust=1, vjust=1)) + xlab("") + ylab("") + theme(plot.title = element_text(hjust = 0.5))   `

结果如下图:

但是,图例不报告模式。如果我偏移 guide="none" 我将有两个图例,但是有没有办法让一个独特的图例(如在当前版本中)报告颜色和图案?

非常感谢您的帮助!

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    删除guide="none" 以显示图例中的模式,但将name="Income components" 添加到scale_pattern_xxx 以便合并指南。否则,秤有不同的名称,您最终会得到两个独立的图例。

    注意:我切换到scale_fill_discrete,因为没有提供mycolors

    library(ggplot2)
    library(ggpattern)
    
    ggplot(relative) +
      geom_bar_pattern(aes(x = country, y = `relative contributions`, fill = factor(component), pattern_type = factor(component)), stat = "identity", position = "stack", width = 0.6, pattern = "magick", 
                       pattern_scale = 0.5, colour = "black") +
      scale_fill_discrete(name = "Income components") +
      scale_pattern_type_discrete(name = "Income components", choices = pat) +
      scale_y_continuous(limits = c(-1, 2), breaks = scales::pretty_breaks(n = 7)) +
      theme_bw() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1)) +
      xlab("") +
      ylab("") +
      theme(plot.title = element_text(hjust = 0.5))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2013-09-02
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      相关资源
      最近更新 更多