Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关系分类变量的图形表示_Python_R_Ggplot2 - Fatal编程技术网

Python 关系分类变量的图形表示

Python 关系分类变量的图形表示,python,r,ggplot2,Python,R,Ggplot2,我正在寻找一些更好地说明分类变量之间关系的想法 对于我下面的可复制数据: t1 <- data.frame(A = c("Apple", "Rose, Apple", "Country"), B = c("Fruit", "Plant", "Peru, Japan")) 你可以看到苹果与水果和植物有关。是否有一个良好的图形解决方案,以热图格式彩色显示各个变量 我会这样想: library(data.table) dt <- data.table

我正在寻找一些更好地说明分类变量之间关系的想法

对于我下面的可复制数据:

t1 <- data.frame(A = c("Apple", "Rose, Apple", "Country"), 
                 B = c("Fruit", "Plant", "Peru, Japan"))

你可以看到苹果与水果和植物有关。是否有一个良好的图形解决方案,以热图格式彩色显示各个变量

我会这样想:

library(data.table)

dt <- data.table(type = as.factor(c("Apple", "Rose", "Apple", "Rose", "Apple")),
                 type2 = as.factor(c("Fruit", "Plant", "Plant", "Tree", "Tree")))
ggplot(data = dt2,
       aes(x = type, fill = type2)) +
  geom_bar(position = "fill")
然后我们得到了一些统计数据(计数和相对百分比):


这就像有一个“馅饼”,上面有多少行具有相同的
类型
-
类型2
组合,但至少可以看出哪些类型比其他类型更相关。

至少对我来说,你的问题相当模糊;热图是网格/矩阵布局中数值数据的可视化表示。根据您的示例数据,您希望在这样的热图中显示哪些数值?如果你能提供一个模拟的情节,也许会有帮助。您的数据结构似乎也有点奇怪秘鲁、日本是A列中的
国家
的示例,但A列中的
苹果
是B列中的
水果
的示例。因此,A列和B列中的条目似乎被互换了。那是故意的吗?
dt2 <- dt[ , .(count = .N), by = .(type, type2)]

dt2[ , percentage.count := count / sum(count) * 100 , by = "type"]

dt2

    type type2 count percentage.count
1: Apple Fruit     1         33.33333
2:  Rose Plant     1         50.00000
3: Apple Plant     1         33.33333
4:  Rose  Tree     1         50.00000
5: Apple  Tree     1         33.33333
ggplot(data = dt2,
       aes(x = type, fill = type2)) +
  geom_bar(position = "fill")