【问题标题】:Triangular plot for categorical variable分类变量的三角图
【发布时间】:2019-12-12 15:31:38
【问题描述】:

这里有一个问题:为分类变量的三重组合绘制值的最佳方法是什么?

这是我在 R 中得到的:

library(tidyverse)
library(ggtern)

df_person <- tibble( name = c( 'Alice', 'Bob', 'Carla', 'Dave', 'Eve' ) ) %>%
  rowid_to_column( 'id_person' )

# generate all trios of persons (5 choose 3)  
df <- df_person %>% select( name ) %>%
  map_df( function(x) { combn(x, 3, paste, collapse = '_') } ) %>%
  separate( name, c('person1', 'person2', 'person3') ) %>%
  mutate_all(~ as.factor(.) )
# assign a value to each trio
df$val <- runif( nrow(df) )

# generate ticks and labels for axes
axis <- df_person %>% mutate( fct = as.factor(name) ) %>%
  mutate( tick = as.numeric(fct) / 5 )

ggtern( df, aes(x = as.numeric(person1),
                y = as.numeric(person2),
                z = as.numeric(person3),
                color = val) ) +
  geom_point() +
  scale_T_continuous( breaks = axis$tick, labels = axis$name ) +
  scale_L_continuous( breaks = axis$tick, labels = axis$name ) +
  scale_R_continuous( breaks = axis$tick, labels = axis$name ) +
  labs( x = 'person1', y = 'person2', z = 'person3' )

这给出了一个相当奇怪的结果:

我希望有十个点位于网格线的交汇处(因为这些是分类变量)。

理想情况下,我想生成一个类似热图的图,即三角形图块而不是点。

非常感谢任何帮助!

【问题讨论】:

    标签: r ggplot2 data-visualization ggtern


    【解决方案1】:

    好的,经过对ternary plots 的一些研究,我现在明白这不是它们的使用方式。

    这种图在考虑三个变量的不同贡献的情况下是有意义的,这些变量的总和总是相同的值。

    对于我的特定用例,我最好使用多面条形图:

    这仍然不完美,因为图中的某些组合从未出现在数据中(例如(Alice、Carla、Carla)),但它可以完成工作。

    如果有人知道这个用例的更好的可视化,我会非常感兴趣。

    【讨论】:

      猜你喜欢
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 2013-10-20
      相关资源
      最近更新 更多