【问题标题】:Two way scatter plot of categorical variables分类变量的双向散点图
【发布时间】:2016-05-25 11:02:31
【问题描述】:

我有一个看起来像这样的数据框:

Taxation=c("Partially",  "Fully", "Partially","Exempt","Partially","Exempt", "Partially",   "Partially", "Fully",   "Fully",    "Fully", "Exempt", "Exempt", "Fully",   "Exempt",   "Exempt","Exempt")
Orientation=c("Non-Profit",  "Non-Profit/Sustainable", "Non-Profit",    "Non-Profit",   "For-Profit",   "Non-Profit/Sustainable",   "Non-Profit",   "Non-Profit/Sustainable",   "Non-Profit",   "Non-Profit",   "Non-Profit/Sustainable",   "Non-Profit",   "Non-Profit",   "Non-Profit/Sustainable",   "Non-Profit/Sustainable",   "Non-Profit",   "Non-Profit/Sustainable")
Country=c("Austria","France",   "Spain",    "Ireland",  "Greece",   "Finland",  "Belgium",  "Austria",  "Belgium",  "Slovenia", "Italy",    "France", "Belgium",    "Portugal", "Netherlands"   ,"Denmark",     "Germany")
Institute=c("Inst1", "Inst2",   "Inst3", "Inst4",   "Inst5", "Inst6",   "Inst7", "Inst8",   "Inst9",    "Inst10",   "Inst11",   "Inst12",   "Inst13",   "Inst14",   "Inst15",   "Inst16",   "Inst17")
Count=rep(1,times=17)
df<-data.frame(Taxation=Taxation, Orientation=Orientation, Country=Country, Institute=Institute,Count=Count)

根据这个数据框,我进行以下计算:

with(df, table(Taxation, Orientation))

Taxation    For-Profit Non-Profit Non-Profit/Sustainable
  Exempt             0          4                      3
  Fully              0          2                      3
  Partially          1          3                      1

我的范围是制作分类变量 Taxation 和 Orientation 的双向散点图,其中将显示带有项目符号的每种可能性的数量,每个项目符号将标有原产国作为标签每个机构。它有帮助,我想重现这样的图表:

请注意,这张图表很好地显示了由不同组合产生的案例数量,并带有一个大“框”。

【问题讨论】:

    标签: r plot ggplot2 scatter-plot


    【解决方案1】:

    这是一个想法,但是没有要点,因为我无法找到一种将点和文本抖动在一起的方法。使用 ggrepel 包可以创建不重叠的文本标签,垂直和水平线将绘图分成 9 个框:

      library(ggrepel)
    
      ggplot(df, aes(x = Taxation, y = Orientation, label = Country)) +
      geom_text_repel(size = 4, segment.color = NA) +
      theme(panel.grid.major = element_blank(),
            axis.text.y = element_text(angle = 90, hjust = .5)) +
      geom_vline(xintercept = c(1.5, 2.5)) +
      geom_hline(yintercept = c(1.5, 2.5)) +
      coord_equal()
    

    【讨论】:

    • 能否请您也展示一下,如何更改 y 轴标签的位置?也就是说,使“非营利”、“营利”等看起来是垂直的而不是水平的。我试过axis.text.y= ...但没有用。
    • 谢谢,我之前没有提到过,但我能问你现在如何增加每个轴上的值和标签之间的距离。因为,您可以看到 x 轴图块 Taxation 非常接近标签“Fully”。同样,对于 y 轴。我自己试过,但没有奏效。如果有机会向我展示如何使垂直和水平线看起来像虚线,它也会有所帮助。
    • @msh855 所有这些额外的问题都曾在 SO 或其他地方被问过和回答,请做一些搜索。如果所有建议的解决方案都不适合您,那么我认为可以提出一个新问题。参见例如here。对于一般性介绍(例如,如何更改线型以及可能有关 ggplot2 工作原理的更多信息),请查看非常好的 ggplot2 documentation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 2015-09-21
    • 2021-11-12
    • 1970-01-01
    • 2019-01-29
    相关资源
    最近更新 更多