三个分类变量的类热图图
Heatmap-like plot for three categorical variables
我正在处理案例形式的分类变量数据框,由三个变量(即颜色、形状和大小)及其相应的频率组成。数据框的一个例子是这样的:
Color Shape Size Freq
1 Yellow Square Big 10
2 Yellow Square Medium 6
3 Yellow Square Small 3
4 Yellow Triangle Big 4
5 Yellow Triangle Medium 6
6 Yellow Triangle Small 8
7 Red Square Big 2
8 Red Square Medium 6
9 Red Square Small 5
10Red Triangle Big 12
.......
"color" 变量是根据 "shape" 和 "size" 变量测量的,每种情况都有一个频率。
从这个数据框中,我正在努力创建一个类似热图的图,其中仅显示 "Color" 和 "Shape" 之间的关系,并使用变量 "Size" 作为权重频率最高。有点棘手,不是吗!
例如,对于 "Yellow" - "Square" 的情况,我应该只显示 "Big",因为 "big" 是频率最高的尺寸。对于每个尺码,都应该有相应的颜色(即 "red" 表示大号,"green" 表示中号,"orange" 表示小号)。
弗兰克
这个怎么样?
library(dplyr)
library(ggplot2)
df_max <- df %>%
group_by(Color, Shape) %>%
slice(which.max(Freq))
head(df_max)
# Source: local data frame [4 x 4]
# Groups: Color, Shape [4]
#
# Color Shape Size Freq
# (chr) (chr) (chr) (int)
# 1 Red Square Medium 6
# 2 Red Triangle Big 12
# 3 Yellow Square Big 10
# 4 Yellow Triangle Small 8
ggplot(df_max, aes(x = Color, y = Shape, fill = Size)) +
geom_tile()
我正在处理案例形式的分类变量数据框,由三个变量(即颜色、形状和大小)及其相应的频率组成。数据框的一个例子是这样的:
Color Shape Size Freq
1 Yellow Square Big 10
2 Yellow Square Medium 6
3 Yellow Square Small 3
4 Yellow Triangle Big 4
5 Yellow Triangle Medium 6
6 Yellow Triangle Small 8
7 Red Square Big 2
8 Red Square Medium 6
9 Red Square Small 5
10Red Triangle Big 12
.......
"color" 变量是根据 "shape" 和 "size" 变量测量的,每种情况都有一个频率。
从这个数据框中,我正在努力创建一个类似热图的图,其中仅显示 "Color" 和 "Shape" 之间的关系,并使用变量 "Size" 作为权重频率最高。有点棘手,不是吗!
例如,对于 "Yellow" - "Square" 的情况,我应该只显示 "Big",因为 "big" 是频率最高的尺寸。对于每个尺码,都应该有相应的颜色(即 "red" 表示大号,"green" 表示中号,"orange" 表示小号)。 弗兰克
这个怎么样?
library(dplyr)
library(ggplot2)
df_max <- df %>%
group_by(Color, Shape) %>%
slice(which.max(Freq))
head(df_max)
# Source: local data frame [4 x 4]
# Groups: Color, Shape [4]
#
# Color Shape Size Freq
# (chr) (chr) (chr) (int)
# 1 Red Square Medium 6
# 2 Red Triangle Big 12
# 3 Yellow Square Big 10
# 4 Yellow Triangle Small 8
ggplot(df_max, aes(x = Color, y = Shape, fill = Size)) +
geom_tile()